在目前的工作中需要解决复制整个SqlServer数据库的问题,复制的内容包括数据库大纲、数据库中的存储过程、函数、表结构、主外键关系以及表中的所有数据等,也就是说copy版本与原数据库一模一样。经过一段时间的摸索,找到的一个比较简单的解决方案是:
(1)在复制数据库之前,先备份该数据库到文件。
(2)依据备份文件创建新的数据库,并Restore即可。
备份数据库可用如下Sql语句:
string
.Format(
"
backup database {0} to disk = '{1}';
"
, dbName, bakFilePath)
依据备份文件创建并Restore新数据库可以使用如下存储过程实现:
CREATE
PROCEDURE
CopyDB
(
@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
(
@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
测试的代码如下:
IList
<
SPParameter
>
paraList
=
new
List
<
SPParameter
>
();
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);
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);