>

이 코드로 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());
    }
}

그러나 실행하면 다음 오류가 발생합니다.

  • 답변 # 1

    데이터베이스를 복구하려면이 코드를 사용하십시오

    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을 실행하는 일반적인 방법입니다.

관련 자료

  • 이전 kubernetes - 기존 영구 볼륨 클레임을 재사용하는 방법
  • 다음 HDIV INVALID_PAGE_ID