private void Button1_Click(object sender, System.EventArgs e)
{ if (Directory.Exists("c:\\mydbstock\\") == false)
Directory.CreateDirectory("c:\\mydbstock\\");
systemsetting aaa= new systemsetting();
string strdate = "linpcontract"+DateTime.Now.Year+DateTime.Now.Month+DateTime.Now.Day+".bak";
if (aaa.BackupDatabase(strdate) == true)
{
systemsetting sss = new systemsetting();
sss.InsertSysLog(Session["LoginName"].ToString(),"备份数据库");
ShowErrMsg("备份数据库成功!");
}
else if (aaa.BackupDatabase(strdate) == false)
{
ShowErrMsg("备份数据库失败!");  
}
}
public bool BackupDatabase(string dbname) 
{
SqlConnection myCn00 = new SqlConnection(strConn);
try
{
string sBackup = "BACKUP DATABASE linpcontract TO DISK = 'c:\\mydbstock\\"+dbname+"' WITH NOINIT"; 
SqlCommand cmd = new SqlCommand(sBackup, myCn00);
myCn00.Open();
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
myCn00.Close();
}
}
  

解决方案 »

  1.   

    使用SQL-DMO:using System;
    using System.Runtime.InteropServices;
    using SQLDMO;SQLServer2Class sqlserver = new SQLServer2Class();
    sqlserver.LoginSecure = true;
    sqlserver.Connect("(local)\\dev", null, null);Backup2Class backup = new Backup2Class();
    backup.Database = "pubs";
    backup.Files = "C:\\TEMP\\pubs.bak";
    backup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
    backup.SQLBackup(sqlserver);
    backup = null;
      

  2.   

    string StrSql = "backup database daj to disk='"+ Server.MapPath( "..\\data\\" + DbName + ".bak " ) +"' with init";
    myCommand = new SqlCommand( StrSql, myConnection );
    myCommand.ExecuteNonQuery();