使用c#在VS中进行恢复数据库时总是显示数据库正在被使用,文件没有独立访问权的问题怎么解决,
privatevoidbutton1_Click(objectsender,EventArgse){stringpath=this.textBox2.Text;strin...
private void button1_Click(object sender, EventArgs e)
{
string path = this.textBox2.Text;
string dbname = this.textBox1.Text;
string SqlStr1 = "Server=.;database='" + this.textBox1.Text + "';Uid=sa;Pwd=123456";
string SqlStr2 = "use master restore database "+dbname+" from Disk='" + path + "'";
using (SqlConnection conn = new SqlConnection(SqlStr1))
{
conn.Open();
try
{
SqlCommand cmd = new SqlCommand(SqlStr2, conn);
cmd.Connection = conn;
//cmd.CommandText = setoff + cmd + seton;
cmd.ExecuteNonQuery();
if (MessageBox.Show("恢复成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
我们是先备份了,然后使用备份文件bak进行恢复,谢谢 展开
{
string path = this.textBox2.Text;
string dbname = this.textBox1.Text;
string SqlStr1 = "Server=.;database='" + this.textBox1.Text + "';Uid=sa;Pwd=123456";
string SqlStr2 = "use master restore database "+dbname+" from Disk='" + path + "'";
using (SqlConnection conn = new SqlConnection(SqlStr1))
{
conn.Open();
try
{
SqlCommand cmd = new SqlCommand(SqlStr2, conn);
cmd.Connection = conn;
//cmd.CommandText = setoff + cmd + seton;
cmd.ExecuteNonQuery();
if (MessageBox.Show("恢复成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
我们是先备份了,然后使用备份文件bak进行恢复,谢谢 展开
1个回答
展开全部
执行restore前先执行另一句sql:
ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
参考资料:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询