SQL Server备份&;恢复

本文关键字:恢复 amp Server 备份 SQL | 更新日期: 2023-09-27 18:07:53

备份

string connectionString1 = (@"Data Source=.'SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Database=Database1;Integrated Security=True; User Instance=True");
            SqlConnection cn = new SqlConnection(connectionString1);
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;
            cmd.CommandText = @"BACKUP DATABASE Database1 TO DISK = 'C:'SRI2Works.bak'";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cn;
            reader = cmd.ExecuteReader();
            cn.Close();
            MessageBox.Show("Database Backup Successfull.");

恢复

string connectionString1 = (@"Data Source=.'SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Database=Database1;Integrated Security=True; User Instance=True");
            SqlConnection cn = new SqlConnection(connectionString1);
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;
            cmd.CommandText = @"use master; RESTORE DATABASE Database1 FROM DISK = 'C:'SRI2Works.bak'";
            cmd.CommandText = "DBCC CHECKDB ('Database1')";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cn;
            reader = cmd.ExecuteReader();
            cn.Close();
            MessageBox.Show("Database Restored Successfull.");

此代码成功运行,但没有进行任何更改。

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 DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
        SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
        bu2.ExecuteNonQuery();
        string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";
        SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
        bu3.ExecuteNonQuery();
        string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
        SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
        bu4.ExecuteNonQuery();
        MessageBox.Show("database restoration done successefully");
        con.Close();
   }
   catch (Exception ex)
   {
        MessageBox.Show(ex.ToString());
   }
}

要了解更多解释,请查看本教程:备份&使用C#还原Sql Server数据库

例如,我们有2个菜单条;一个用于备份,另一个用于恢复,所以他们执行自己的方法!试试这个:

    private void saveDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            BackupDatabase();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " 'nPlease choose the folder Sauvegardes to backup !");
        }
    }
    private void restoreDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            RestoreDatabase();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    private void BackupDatabase()
    {
        saveFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Sauvegardes";
        if (saveFileDialogBackUp.ShowDialog() == DialogResult.OK)
        {
            Con.ExecuteCmd("BACKUP DATABASE MyFooDatabase TO DISK = '" + saveFileDialogBackUp.FileName + "'");
            MessageBox.Show("Success , done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    private void RestoreDatabase()
    {
        openFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Sauvegardes";
        if (openFileDialogBackUp.ShowDialog() == DialogResult.OK)
        {
            Con.ExecuteCmd(" USE MASTER RESTORE DATABASE MyFooDatabase FROM DISK = '"+openFileDialogBackUp.FileName+"' WITH REPLACE");
            MessageBox.Show("Database Restored", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

从代码中删除以下语句:

cmd.CommandText = "DBCC CHECKDB ('Database1')";

如果您想覆盖现有数据库,请使用以下命令作为命令文本:

cmd.CommandText = @"use master; RESTORE DATABASE Database1 FROM DISK = 'C:'SRI2Works.bak' WITH REPLACE";
RESTORE DATABASE [C] FROM DISK = 'D:''Inventory.bak' WITH RECOVERY, 
MOVE 'Inventory_Data'
 TO 'C:''Program Files''Microsoft SQL Server''MSSQL''Data''c_Data.MDF', 
MOVE 'Inventory_Log' 
TO 
'C:''Program Files''Microsoft SQL Server''MSSQL''Data''c_Log.LDF', 
REPLACE, stats =1

您唯一需要注意的是,恢复数据库不是在MS SQL SERVER中创建的。在我的情况下,在运行查询后,它应该创建一个名为[C]的新数据库,并在[C:''Program Files''Microsoft SQL Server''MSSQL''Data''] 的路径上创建其文件