(1)在复制数据库之前,先备份该数据库到文件。
(2)依据备份文件创建新的数据库,并Restore即可。
备份数据库可用如下Sql语句:
(
@newDbName varchar(50), --新数据库名称
@dbDataDirPath varchar(100), --数据库安装的Data文件夹目录路径
@soureDbName varchar(100), --源数据库名称
@soureBackupFilePATH varchar(100)--源数据库备份文件的路径
)
AS
declare @sql varchar(3000)
set @sql='
create database '+@newDbName+'
ON
(
name='+@soureDbName+'_Data,
filename='''+@dbDataDirPath+@newDbName+'_Data.mdf'',
SIZE = 10,
FILEGROWTH = 15%
)
LOG ON
(
name='''+@soureDbName+'_Log'',
filename='''+@dbDataDirPath+@newDbName+'_Log.LDF'',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB
)
--开始还原
RESTORE DATABASE '+@newDbName+' from disk='''+@soureBackupFilePATH+''' WITH REPLACE
'
exec(@sql)
GO
测试的代码如下:
SPParameter para1 = new SPParameter("newDbName", ParameterDirection.Input, "EASNew9");
paraList.Add(para1);
SPParameter para2 = new SPParameter("dbDataDirPath", ParameterDirection.Input, @"C:\Program Files\Microsoft SQL Server\MSSQL\Data\");
paraList.Add(para2);
SPParameter para3 = new SPParameter("soureDbName", ParameterDirection.Input, "AutoSchedulerSystem");
paraList.Add(para3);
SPParameter para4 = new SPParameter("soureBackupFilePATH", ParameterDirection.Input, @"d:\sqlDatabase\AutoSchedulerSystem");
paraList.Add(para4);
IDictionary<string, object> outParas = null;
Program.DataAccesser.GetSPAccesser(null).ExcuteNoneQuery("CopyDB", paraList, out outParas);