我点击了按钮
创建了一个备份我的代码是
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();
但是我收到了错误
请帮我解决这个问题
答案 0 :(得分:1)
每个物理文件都需要驻留在您尝试将备份数据库还原到的系统上的某个位置。该错误消息告诉您该文件正由另一个数据库使用。您可以查询sys.master_files以找出它们属于哪个数据库。
要解决此问题,您需要删除使用这些物理文件的数据库,或者在RESTORE语句中添加MOVE子句。对于后者,它看起来像这样:
WITH Times (RowNumber, Vehicle, StartTime)
AS
(SELECT ROW_NUMBER() OVER(ORDER BY Vehicle, StartTime),
Vehicle, StartTime, NextTime FROM dbo.AvgLoggingRunState)
SELECT CurrentTime.Vehicle,
CurrentTime.StartTime,
NextTime.StartTime AS NextTime,
COALESCE(DATEDIFF(SECOND,CurrentTime.StartTime,NextTime.StartTime),0)
AS SecondsDiffNext,
COALESCE(DATEDIFF(SECOND,CurrentTime.StartTime,CurrentTime.EndTime),0)
AS SecondsDiffEnd
FROM Times CurrentTime
-- join to the next time - note that I join both on row number and
-- on vehicles as I don't want to mix vehicles
LEFT OUTER JOIN Times NextTime
ON CurrentTime.RowNumber +1 = NextTime.RowNumber
AND CurrentTime.Vehicle = NextTime.Vehicle