黑马程序员技术交流社区
标题:
.数据库自动备份
[打印本页]
作者:
夏华成
时间:
2012-5-19 09:56
标题:
.数据库自动备份
以下是本人QQ空间早期日记. 都是自己实际工作中用到的(分享一下)
第一种: 网页版.这个手动备份的.
using (SqlConnection conn = new SqlConnection(cn))
{
conn.Open();
string sql1 = "DUMP TRANSACTION code WITH NO_LOG";
string sql11 = "BACKUP LOG code WITH NO_LOG";
string sql2 = "DUMP TRANSACTION code WITH NO_LOG";
string sql22 = "BACKUP LOG code WITH NO_LOG";
using (SqlCommand cmd = new SqlCommand(sql1, conn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand(sql11, conn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand("update zhiyuan set sb='..',linkzt='..'", conn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand(sql2, conn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand(sql22, conn))
{
cmd.ExecuteNonQuery();
}
}
try
{
if (File.Exists(Server.MapPath("xx.rar")))
{
File.Delete(Server.MapPath("xx.rar"));
}
// System.IO.StreamWriter s = new StreamWriter((System.IO.Stream)File.OpenWrite(MapPath("link.rar")));
}
catch (Exception ex)
{ }
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect("IP", "sa", "密码");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = "数据库";
oBackup.Files = "[" + MapPath("xx.rar") + "]";
oBackup.BackupSetName = "xx";
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
ClientScript.RegisterStartupScript(this.GetType(), "m2", "<script>alert('备份成功')</script>");
}
catch (Exception exx)
{
ClientScript.RegisterStartupScript(this.GetType(), "m2", "<script>alert('备份失败')</script>");
}
复制代码
第二种:服务器版 在sql server 代理中.作业里
DUMP TRANSACTION link WITH NO_LOG
BACKUP LOG link WITH NO_LOG
DECLARE @Num varchar(50)
SET @Num = 'D:\bak\'+ convert(varchar(50),getdate(),5)+'.bak'
backup database link to disk=@Num with format
复制代码
作者:
魏庆君
时间:
2012-5-24 03:08
备份的时候几下日志,恢复的时候就可以直接记下了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2