恢復我在C#編寫的程序使用SQL 2008SQL Server備份和從C#問題
我要備份的數據庫,並恢復它。我的備份代碼正常工作,但恢復不起作用,數據庫變成單個用戶。
收到以下錯誤:
日誌的尾部數據庫 「醫生」還沒有被備份。使用 備份日誌與NORECOVERY備份 日誌如果它包含工作,你不想 想要失去。使用WITH REPLACE或 WITH RESTORE 語句的STOPAT子句僅覆蓋日誌的內容 。 RESTORE DATABASE 正在異常終止。 不合格交易正在回滾 。估計回滾
我的代碼:
private void Backup(string strFileName)
{
try
{
string command = @"BACKUP DATABASE doctor TO DISK='"+ strFileName+"'";
SqlCommand oCommand = null;
SqlConnection oConnection = null;
oConnection = new SqlConnection("Data Source=.;Initial Catalog=doctor;Integrated Security=True");
if (oConnection.State != ConnectionState.Open)
oConnection.Open();
oCommand = new SqlCommand(command, oConnection);
oCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Restore(string strFileName)
{
try
{
string command = "ALTER DATABASE doctor SET SINGLE_USER with ROLLBACK IMMEDIATE " + "use master " + " RESTORE DATABASE doctor FROM DISK='" + strFileName + "'";
SqlCommand oCommand = null;
SqlConnection oConnection = null;
oConnection = new SqlConnection("Data Source=.;Initial Catalog=doctor;Integrated Security=True");
if (oConnection.State == ConnectionState.Closed)
{
oConnection.Open();
oCommand = new SqlCommand(command, oConnection);
oCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
接受更多的答案,請 –