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