C#SQL Restore Open Connection无法关闭

时间:2014-03-19 15:37:41

标签: c# sql database-restore

我运行以下代码,它告诉我已经存在与数据库的开放连接。

data source=ws-server02;initial catalog=SL_M2009;
user id=sa;password=gregfhh

我检查了10次,但没有发现任何打开的东西。也许我的代码打开一个阻止恢复的连接?

错误代码在这里:

  1. http://grabilla.com/04313-7f69772a-f65c-4a3c-81c3-c118f39ea907.html
  2. http://grabilla.com/04313-4f78e5cc-27e3-4155-b92f-1da7976afdb7.html
  3. 现在我可以做些什么来完成恢复?以下是相关的代码块。

    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);
    

1 个答案:

答案 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);