2013-04-06 174 views
5

備份SQL Server備份和恢復

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

這段代碼成功運行,但不進行任何更改。

+0

您是如何驗證代碼確實被執行的?消息框是否觸發? – usr 2013-04-06 15:29:53

+0

文件'C:\ SRI2Works.bak'實際寫入了嗎?而且,實際上,您的恢復命令僅執行'DBCC CHECKDB('Database1')',它取代了上面一行中設置的'CommandText'。 – 2013-04-06 15:34:50

+0

@ThomasGerstendörfer好抓! OP應該使用SQL事件探查器來驗證預期的命令是否被實際發送。 – usr 2013-04-06 15:41:21

回答

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

    } 
0

從代碼中刪除以下語句:

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

,如果你想覆蓋現有的數據庫使用以下命令的CommandText:

cmd.CommandText = @"use master; RESTORE DATABASE Database1 FROM DISK = 'C:\SRI2Works.bak' WITH REPLACE"; 
6

試試這個代碼還原數據庫:

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#

+0

太棒了。節省了我的時間。和+1參考鏈接也:) – Mehmood 2016-12-26 17:31:45

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\\]的路徑上創建它的文件。