简体   繁体   English

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

[英]I'm trying to restore a SQL Server database backup with this code :

I'm trying to restore a SQL Server database backup with this code: 我正在尝试使用以下代码还原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());
    }
}

But when I execute it I get this error: 但是当我执行它时,我得到这个错误:

在此处输入图片说明

For restoring database use this code 要还原数据库,请使用此代码

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 is a normal method to run SQL you should make it ExecuteQuery是运行SQL的常规方法,应该使它生效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM