如何还原SQL数据库(C#,用SMO,error说database is in use)
我写了一个C#的程序,在里面有四个按键:backup database;backup log file; verify backups; restore。前面三个都运行正常,但是在restore的时候出现error说the database is in use。但是我的程序里面没有任何sqlconnection string阿。下面是我的程序,数据库名:HighPotTest,server:localhost;没有用户名和密码,integrated security=SSPI
private void btnRestore_Click(object sender, EventArgs e)
{
Server svr = new Server("localhost");
Restore res = new Restore();
Cursor = Cursors.WaitCursor;
try
{
string strFileName = txtFile.Text.ToString();
string strDatabaseName = "HighPotTest";
res.Database = strDatabaseName;
res.Action = RestoreActionType.Database;
//use for restore of the log
//res.Action = RestoreActionType.Log;
res.Devices.AddDevice(strFileName, DeviceType.File);
//progress meter stuff
progressBar1.Value = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 10;
res.PercentCompleteNotification = 10;
res.ReplaceDatabase = true;
res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
//preform the restore
res.SqlRestore(svr);
MessageBox.Show("Restore of " + strDatabaseName + " Complete!", "SMO Demos");
}
catch (SmoException exSMO)
{
MessageBox.Show(exSMO.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
Cursor = Cursors.Default;
progressBar1.Value = 0;
}
}
参考答案:我在还原SQL2005时,使用的是SQL语句,在还原前需要将日志文件备分,然后在还原数据库文件.SMO对象我没太研究,只是做了用SMO对象删除\创建数据库.
你试试在还原数据库前备份一下日志文件看看吧.