我开发了一个Windows Forms中的MIS,其中我备份了我的SQL Server 2012数据库,但是我无法恢复备份(.bak
)文件。
这是我的代码:
private void buttonRestore_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "Backup File |*.bak";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string sql = "Alter Database BOQ SET SINGLE_USER WITH ROLLBACK IMMEDIATE;";
sql += "Restore Database BOQ FROM DISK ='" + openFileDialog1.FileName + "' WITH REPLACE;";
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=BOQ;Integrated Security=True");
SqlCommand command = new SqlCommand(sql,con);
con.Open();
command.ExecuteNonQuery();
MessageBox.Show("Database Recovered Successfully!");
con.Close();
con.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但是我收到了这个错误:
master
,并在恢复数据库完成后关闭该连接,然后使用第一个连接字符串连接到您想要使用的实际数据库(即您刚刚恢复的数据库)。 - marc_s