Problem in restoring SQL Server MDF File database
-
Here is code
SqlConnection connection = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename="D:\Working Directory\Win Application\Nakoda\Development\Bin\Database\cERP.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True"); ServerConnection srvConn = new ServerConnection(connection); // Create a new SQL Server object using the connection we created srvSql = new Server(srvConn); // If the user has chosen the file from which he wants the database to be restored if (openBackupDialog.ShowDialog() == DialogResult.OK) { //Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file. BackupDeviceItem bdi = default(BackupDeviceItem); bdi = new BackupDeviceItem(openBackupDialog.FileName, DeviceType.File); //Create another file device for the differential backup and add the Backup object. BackupDeviceItem bdid = default(BackupDeviceItem); bdid = new BackupDeviceItem(openBackupDialog.FileName, DeviceType.File); // Set the database that we want to perform the restore on string db = cmbDatabase.SelectedItem.ToString(); //rstDatabase.Database = db; //Define a Restore object variable. Restore rs = default(Restore); rs = new Restore(); //Set the NoRecovery property to true, so the transactions are not recovered. rs.NoRecovery = true; //Add the device that contains the full database backup to the Restore object. rs.Devices.Add(bdi); //Specify the database name. rs.Database = db; //Restore the full database backup with no recovery. rs.SqlRestore(srvSql);
I Got Exception Microsoft.SqlServer.Management.Smo.FailedOperationException occurred HelpLink=http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1447.4+((KJ\_RTM).100213-0103+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476 Message=Restore failed for Server '\\.\pipe\1FF7F0B1-18D8-42\tsql\query'. Source=Microsoft.SqlServer.SmoE -
Here is code
SqlConnection connection = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename="D:\Working Directory\Win Application\Nakoda\Development\Bin\Database\cERP.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True"); ServerConnection srvConn = new ServerConnection(connection); // Create a new SQL Server object using the connection we created srvSql = new Server(srvConn); // If the user has chosen the file from which he wants the database to be restored if (openBackupDialog.ShowDialog() == DialogResult.OK) { //Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file. BackupDeviceItem bdi = default(BackupDeviceItem); bdi = new BackupDeviceItem(openBackupDialog.FileName, DeviceType.File); //Create another file device for the differential backup and add the Backup object. BackupDeviceItem bdid = default(BackupDeviceItem); bdid = new BackupDeviceItem(openBackupDialog.FileName, DeviceType.File); // Set the database that we want to perform the restore on string db = cmbDatabase.SelectedItem.ToString(); //rstDatabase.Database = db; //Define a Restore object variable. Restore rs = default(Restore); rs = new Restore(); //Set the NoRecovery property to true, so the transactions are not recovered. rs.NoRecovery = true; //Add the device that contains the full database backup to the Restore object. rs.Devices.Add(bdi); //Specify the database name. rs.Database = db; //Restore the full database backup with no recovery. rs.SqlRestore(srvSql);
I Got Exception Microsoft.SqlServer.Management.Smo.FailedOperationException occurred HelpLink=http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1447.4+((KJ\_RTM).100213-0103+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476 Message=Restore failed for Server '\\.\pipe\1FF7F0B1-18D8-42\tsql\query'. Source=Microsoft.SqlServer.SmoEkirankkk2009 wrote:
Message=RESTORE cannot process database 'D:\WORKING DIRECTORY\WIN APPLICATION\NAKODA\DEVELOPMENT\BIN\DATABASE\CERP.MDF' because it is in use by this session. It is recommended that the master database be used when performing this operation.
So you are connected to the database you want to restore - fail. Do what the exception advises you to, connect to the master database and restore your database.
Never underestimate the power of human stupidity RAH