BACKUP DATABASE '被备份的数据库名' TO DISK = '备份文件路径';
ALTER DATABASE '被恢复的数据库名' SET OFFLINE WITH ROOLBACK IMMEDIATE;
RESTORE DATABASE '被恢复的数据库名' FROM DISK = '备份文件路径';
ALTER DATABASE '被恢复的数据库名' SET ONLINE WITH ROOLBACK IMMEDIATE;
简单几条命令就可以完成数据库的备份和恢复,在Asp.Net中,可以像调用Sql语句一样调用这四条语句,轻轻松松实现SQL SERVER数据库的备份和恢复。
例子(以Northwind为例):
1、备份Northwind数据库到C盘根目录下,以'Northwind.bak'为名:
BACKUP DATABASE 'Northwind' TO DISK = 'C:\Northwind.bak';
2、恢复Northwind数据库,C盘根目录下的'Northwind.bak'备份文件:
a、将数据库置于离线状态
ALTER DATABASE 'Northwind' SET OFFLINE WITH ROOLBACK IMMEDIATE;
b、恢复Northwind数据库
RESTORE DATABASE 'Northwind' FROM DISK = 'C:\Northwind.bak';
c、将数据库置于在线状态
ALTER DATABASE 'Northwind' SET ONLINE WITH ROOLBACK IMMEDIATE;
恢复数据库:
关键字:Alter Database 被恢复的数据库名 Set Offline with Rollback immediate;
restore database 被恢复的数据库名 from disk = '备份文件路径';
Alter Database 被恢复的数据库名 Set OnLine With rollback Immediate;
/////////////////////
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>");