2017-01-22 133 views
0

我創建按鈕備份點擊如何恢復備份程序

我對這個代碼是

con.Open(); 

string str = "USE [C:\\Users\\asus\\Documents\\Visual Studio 2012\\Projects\\bbcon_accout_software\\bb_con_accnt_soft_lib\\BbCon.mdf]"; 
string str1 = "BACKUP DATABASE [C:\\Users\\asus\\Documents\\Visual Studio 2012\\Projects\\bbcon_accout_software\\bb_con_accnt_soft_lib\\BbCon.mdf] TO DISK = 'C:\\Users\\Public\\aa.Bak' WITH FORMAT,MEDIANAME = 'Z_SQLServerBackups',NAME = 'Full Backup of Testdb';"; 

SqlCommand cmd1 = new SqlCommand(str, con); 
SqlCommand cmd2 = new SqlCommand(str1, con); 

cmd1.ExecuteNonQuery(); 
cmd2.ExecuteNonQuery(); 

MessageBox.Show("success"); 
con.Close(); 

現在我想恢復備份的數據庫,請大家幫我搜索很多問題,但我沒有得到我需要的答案 - 請幫助我執行此任務。

我嘗試使用此代碼來恢復:

string query = "RESTORE DATABASE BbCon FROM DISK='C:\\Users\\Public\\aa.Bak' WITH REPLACE"; 
con.Open(); 

using (SqlCommand cmd = new SqlCommand(query, con)) 
{ 
    cmd.ExecuteNonQuery(); 
} 

con.Close(); 

但我得到一個錯誤

enter image description here

請幫我這個

回答

1

每個物理文件的需要駐留在您嘗試恢復備份數據庫的系統上。錯誤消息告訴你這些文件正在被另一個數據庫使用。你可以查詢sys.master_files來找出它們屬於哪個數據庫。

要解決此問題,您需要刪除使用這些物理文件的數據庫或將MOVE子句添加到RESTORE語句中。對於後者,它看起來像這樣:

RESTORE DATABASE BbCon FROM DISK='C:\\Users\\Public\\aa.Bak' WITH REPLACE, 
move 'BBCon' to 'c:\temp\BBCon.mdf', 
move 'BBCon_log' to 'c:\temp\BBCon_log.ldf';