Question

I run the following code and it tells me that there is already an open connection to the database.

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

I checked 10 times but didn't find anything which is open. Maybe my code opens a connection which blocks the Restore?

The error codes are here:

  1. http://grabilla.com/04313-7f69772a-f65c-4a3c-81c3-c118f39ea907.html
  2. http://grabilla.com/04313-4f78e5cc-27e3-4155-b92f-1da7976afdb7.html

What can I do now for getting the Restore completed? Below is the relevant code block.

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);
Was it helpful?

Solution

try this code hope it will work

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top