我运行以下代码,它告诉我已经存在与数据库的开放连接。
data source=ws-server02;initial catalog=SL_M2009;
user id=sa;password=gregfhh
我检查了10次,但没有发现任何打开的东西。也许我的代码打开一个阻止恢复的连接?
错误代码在这里:
现在我可以做些什么来完成恢复?以下是相关的代码块。
string dbconnectstring = data source=ws-server02;initial catalog=SL_M2009;user id=sa; password=myownpw;
SqlConnection sqlConn = new SqlConnection(dbconnectstring);
Server sqlServer = new Server(new ServerConnection(sqlConn));
MessageBox.Show("Server Status: "
+ sqlServer.Status
+ "\r\nDatabase Name: "
+ sqlConn.Database + " Active Connections: "
+ sqlServer.GetActiveDBConnectionCount(sqlConn.Database));
Restore restoreDB = new Restore();
String file = NewestFile(dbsource);
restoreDB.Action = RestoreActionType.Database;
restoreDB.Database = sqlConn.Database;
restoreDB.Devices.AddDevice(NewestFile(dbsource), DeviceType.File);
restoreDB.ReplaceDatabase = true;
restoreDB.NoRecovery = true;
restoreDB.SqlRestore(sqlServer);
答案 0 :(得分:0)
尝试此代码希望它能够正常工作
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
MessageBox.Show("Server Status: "
+ sqlServer.Status
+ "\r\nDatabase Name: "
+ sqlConn.Database + " Active Connections: "
+ sqlServer.GetActiveDBConnectionCount(sqlConn.Database));
Restore rstDatabase = new Restore();
rstDatabase.Action = RestoreActionType.Database;
rstDatabase.Database = databaseName;
BackupDeviceItem bkpDevice = new BackupDeviceItem(backUpFile, DeviceType.File);
rstDatabase.Devices.Add(bkpDevice);
rstDatabase.ReplaceDatabase = true;
rstDatabase.SqlRestore(sqlServer);