怎样用C#来备份SQL2005数据库
3个回答
展开全部
protected void btnBackUp_Click(object sender, EventArgs e)
{
string FileName = Request.MapPath(Request.ApplicationPath + "\\App_Data") + "\\DataBaseBack.bk";
if (!DbBackup(FileName))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "<script>alert('备份数据库时出错,请用数据库管理工具管理数据库备份和恢复.!');</script>");
return;
}
}
public bool DbBackup(string url)
{
bool bl = true;
SQLDMO.Backup oback = new SQLDMO.BackupClass();
SQLDMO.SQLServer oServer = new SQLDMO.SQLServerClass();
try
{
oServer.LoginSecure = false;
string pstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString().ToLower();
oServer.Connect(".", "sa", "123");
oback.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oback.Database = "IMS_rcjz"; //数据库名称
oback.Files = url;
oback.BackupSetName = "IMS_rcjzBK"; //备份数据后的名称
oback.BackupSetDescription = DateTime.Now.ToString("yyyyMMddHHmm") + "数据库备份";
oback.Initialize = true;
oback.SQLBackup(oServer);
}
catch (Exception)
{
bl = false;
throw;
}
finally {
oServer.DisConnect();
}
return bl;
}
{
string FileName = Request.MapPath(Request.ApplicationPath + "\\App_Data") + "\\DataBaseBack.bk";
if (!DbBackup(FileName))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "<script>alert('备份数据库时出错,请用数据库管理工具管理数据库备份和恢复.!');</script>");
return;
}
}
public bool DbBackup(string url)
{
bool bl = true;
SQLDMO.Backup oback = new SQLDMO.BackupClass();
SQLDMO.SQLServer oServer = new SQLDMO.SQLServerClass();
try
{
oServer.LoginSecure = false;
string pstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString().ToLower();
oServer.Connect(".", "sa", "123");
oback.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oback.Database = "IMS_rcjz"; //数据库名称
oback.Files = url;
oback.BackupSetName = "IMS_rcjzBK"; //备份数据后的名称
oback.BackupSetDescription = DateTime.Now.ToString("yyyyMMddHHmm") + "数据库备份";
oback.Initialize = true;
oback.SQLBackup(oServer);
}
catch (Exception)
{
bl = false;
throw;
}
finally {
oServer.DisConnect();
}
return bl;
}
追问
可以不用sqldmo语句吗 还有没有其它方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询