数据库的备份问题
我要说的一点就是备份路径的问题,我做 了好久,后来与一个网友两个人不停的想,后来才做出来了,结果与大家分享一下,这个路径,我觉得
//数据备份
protected void btnBackDB_Click(object sender, EventArgs e)
{
Backup oBackUp = new BackupClass();
SQLServer oSqlServer = new SQLServerClass();
try
{
oSqlServer.LoginSecure = false;
oSqlServer.Connect("localhost", "sa", "123456");
oBackUp.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackUp.Database = "WeifangEducationDB";
//string url = Server.MapPath("BackDb");
//if (!Directory.Exists(url)) Directory.CreateDirectory(url);Path.Combine(url, "weifangeducationdb.bak")
,这个是网友提供的
string url = Server.MapPath("back");
//这个是我自己写的
if (!Directory.Exists(url))
{
DirectoryInfo info = Directory.CreateDirectory(url);
}
//格式化时间
string datetim = DateTime.Now.ToString("yyyyMMddHHmmss");
datetim = datetim.Replace(" ","");
datetim = datetim.Replace("-","");
datetim = datetim.Replace(":","");
datetim = datetim.Substring(0,8);
oBackUp.Files = url + @"\WeifangEducationDB_"+datetim+".bak";
Response.Write("");
oBackUp.BackupSetName = "WeifangEducationDB";
oBackUp.BackupSetDescription = "数据库备份";
oBackUp.Initialize = true;
oBackUp.SQLBackup(oSqlServer);
lblMessages.Text = "备份成功!";
this.lblMessages.Text = "备份成功!";
}
catch (Exception ex)
{
lblMessages.Text = "备份失败" + ex.Message;
}
finally
{
oSqlServer.DisConnect();
}
}