我正在尝试使用以下代码恢复SQL Server数据库备份:

时间:2017-11-05 13:32:00

标签: c# sql-server

我正在尝试使用以下代码恢复SQL Server数据库备份:

 private void restoreButton_Click(object sender, EventArgs e)
 {
      string database = con.Database.ToString();

      if (con.State != ConnectionState.Open)
      {
           con.Open();
      }

      try
      {
           string sqlStmt2 = string.Format("ALTER [MangementSystemBD] [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");

           SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
           bu2.ExecuteNonQuery();

           string sqlStmt3 = "USE MASTER RESTORE [MangementSystemBD] [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";

           SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
           bu3.ExecuteNonQuery();

           string sqlStmt4 = string.Format("ALTER [MangementSystemBD] [" + database + "] SET MULTI_USER");

           SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
           bu4.ExecuteNonQuery();

           MessageBox.Show("database restoration done successfully");
           con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

但是当我执行它时,我收到了这个错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

要恢复数据库,请使用此代码

public void RestoreDatabaseBackup()
{
    ExecuteQuery("EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = 'DatabaseName");
    ExecuteQuery("USE MASTER");

    OpenFileDialog res = new OpenFileDialog();

    res.ShowDialog();

    if (res.FileName.ToString() == "")
    {
        //MessageBox.Show("Select Valid .bak File");
    }
    else
    {
        try
        {
            string s = res.FileName.ToString();

            SqlConnection sqlConnection = new SqlConnection(@"Data Source=ServerName;Initial Catalog=yourDatabaseName;Integrated Security=True;Pooling=False");
            if (sqlConnection.State == System.Data.ConnectionState.Open)
            {
                sqlConnection.Close();
            }

            sqlConnection.Open();

            ServerConnection ServerConnection = new ServerConnection(sqlConnection);
            Server srv = new Server(ServerConnection);

            //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(s, DeviceType.File);

            // Define a Restore object variable.
            Restore rs = new Restore();
            //Set the NoRecovery property to true, so the transactions are not recovered.
            rs.NoRecovery = true;
            rs.ReplaceDatabase = true;

            // Add the device that contains the full database backup to the Restore object.
            rs.Devices.Add(bdi);
            rs.NoRecovery = false;

            // Specify the database name.
            rs.Database = sqlConnection.Database.ToString();

            // Restore the full database backup with no recovery.
            sqlConnection.ChangeDatabase("master");
            rs.SqlRestore(srv);

            // Inform the user that the Full Database Restore is complete.
            srv = null;
            MessageBox.Show("Database Restore complete.");
        }
        catch (Exception ex)
        {
             MessageBox.Show(ex.Message);
        }
    }
}

ExecuteQuery是运行SQL的常用方法