如何通过C#代码还原SQL Server数据库 [英] How to restore SQL Server database through C# code
问题描述
我尝试恢复这样的数据库:
SQL = @RESTORE DATABASE MYDATABASE TO DISK ='D:\\ \\MyDATA.BAK';
CMD =新的SqlCommand(SQL,康涅狄格州);
Cmd.ExecuteNonQuery();
Cmd.Dispose();
但我总是得到错误:
消息3102,级别16,状态1,行7结果
RESTORE无法处理数据库'MyDatabase的,因为它是在使用
。通过本次会议。建议主数据库执行此操作时,可以使用
。结果
消息3013,级别16,状态1,行7结果
RESTORE DATABASE异常终止。
块引用>
解决方案您DB连接是最有可能你正试图恢复数据库。因此,有一个数据库共享锁可以防止您的数据库的恢复
试试这个
SQL = @使用母版BACKUP DATABASE MYDATABASE TO DISK ='D:\MyDATA.BAK';
或更改连接的详细信息使用主DB
I try to restore the database like this:
SQL = @"RESTORE DATABASE MyDataBase TO DISK='d:\MyDATA.BAK'"; Cmd = new SqlCommand(SQL, Conn); Cmd.ExecuteNonQuery(); Cmd.Dispose();
but I always get error:
Msg 3102, Level 16, State 1, Line 7
RESTORE cannot process database 'MyDataBase ' because it is in use by this session. It is recommended that the master database be used when performing this operation.
Msg 3013, Level 16, State 1, Line 7
RESTORE DATABASE is terminating abnormally.
解决方案Your DB connection is most likely to the database you're trying to restore. So there is a DB shared lock which prevents the restore of your db
Try this
SQL = @"USE master BACKUP DATABASE MyDataBase TO DISK='d:\MyDATA.BAK'";
Or change the connection details to use master DB
这篇关于如何通过C#代码还原SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!