在這裏,我必須表明我的C#代碼備份和SQL數據庫備份和還原SQL數據庫通過Asp.Net管理員用戶,C#
恢復在該備份工作正常,但是當我嘗試恢復從備份文件夾數據庫時,發生了一個時間錯誤,錯誤是:
RESTORE不能處理數據庫' UsersDB ',因爲它是在通過本次會議使用 。建議在執行此操作時使用主數據庫 。 RESTORE DATABASE異常終止 。
備份按鈕:
protected void btnbackup_Click(object sender, EventArgs e)
{
sqlcon.ConnectionString = (System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
string destdir = "C:\\backupdb";
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("C:\\backupdb");
}
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("backup database UsersDB to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
Response.Write("Backup database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
}
恢復按鈕:
protected void btnrestore_Click(object sender, EventArgs e)
{
try
{
sqlcon.ConnectionString = (System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
sqlcon.Open();
string destdir = "C:\\backupdb\\11082014_121403.Bak";
sqlcmd = new SqlCommand("Restore database UsersDB from disk='C:11082014_143650.Bak' ", sqlcon);
sqlcmd.ExecuteNonQuery();
Response.Write("restore database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
}
在此先感謝
感謝,給予好評:) –