恢复数据库:
关键字:Alter Database 被恢复的数据库名 Set Offline with Rollback immediate;
restore database 被恢复的数据库名 from disk = '备份文件路径';
Alter Database 被恢复的数据库名 Set OnLine With rollback Immediate;
注意,备分时没有问题,但恢复时要注意先停sqlserver
/////////////////////
string sql = "Alter Database db Set Offline with Rollback immediate;"; //db 是要备份的数据库名
sql += "restore database db from disk = '" ;
sql += Server.MapPath("").ToString() +"\\";
sql += bakname + "'"; //bakname 是备份文件名
sql += "Alter Database db Set OnLine With rollback Immediate;";
try
{
连接 master 数据库 ;
执行 sql 语句;
Response.Write("<script language=javascript>alert('数据恢复成功!');</script>");
}
catch(Exception ex)
{
Response.Write("<script language=javascript>alert('数据恢复失败!');</script>");
this.Label2.Text = ex.ToString();
}
备份数据库:
关键字:backup database 被备份的数据库名 to disk ='备份文件路径';
//////////////////////
string sql = "backup database db to disk = '" + Server.MapPath("").ToString() +"\\"
+ bakname //备份文件名
+ System.DateTime.Now.DayOfYear.ToString()
+ System.DateTime.Now.Millisecond.ToString() + ".bak'";
Database data = new Database();
data.oper_data(sql);
Response.Write("<script language=javascript>alert('备份成功!');location='restore.aspx'</script>");