Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Problem in restoring SQL Server MDF File database

Problem in restoring SQL Server MDF File database

Scheduled Pinned Locked Moved Database
databasesql-servercomsysadminsecurity
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kirankkk2009
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • K kirankkk2009

      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

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      kirankkk2009 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups