6

备份

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.");

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

4

4 回答 4

7

在还原数据库中尝试此代码:

    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());
   }
}

有关更多说明,请查看本教程:Backup & Restore Sql Server database using C#

于 2016-03-13T19:15:10.193 回答
0

例如我们有 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);
        }

    }
于 2013-04-06T15:38:58.683 回答
0

从您的代码中删除以下语句:

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

如果要覆盖现有数据库,请对 commandtext 使用以下命令:

cmd.CommandText = @"use master; RESTORE DATABASE Database1 FROM DISK = 'C:\SRI2Works.bak' WITH REPLACE";
于 2013-10-23T10:09:36.553 回答
0
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\\]

于 2016-09-28T18:20:07.357 回答