static string pathBak;
/// <summary>
/// 备份SQL数据库
/// </summary>
/// <param name="databasename">要备份的数据源名称</param>
/// <param name="backuptodatabase">备份到的数据库文件名称及路径.bak</param>
/// <returns>返回生成的备份文件全路径名</returns>
public string BakSql(string databaseName, string bakPath)
{
try
{
//声明临时数据库名称
string bakName = string.Empty;
if (bakPath.IndexOf(".bak") < 0)
{
bakName = databaseName;
bakName += DateTime.Now.Year.ToString();
bakName += DateTime.Now.Month.ToString();
bakName += DateTime.Now.Day.ToString();
bakName += DateTime.Now.Hour.ToString();
bakName += DateTime.Now.Minute.ToString();
bakName += DateTime.Now.Second.ToString();
bakName += ".bak";
if (bakPath.LastIndexOf('\\') != bakPath.Length)
bakPath += '\\';
}
FileInfo fi = new FileInfo(bakPath);
if (!Directory.Exists(fi.DirectoryName))
Directory.CreateDirectory(fi.DirectoryName);
bakPath = bakPath + bakName;
pathBak = bakPath;
bakPath = bakPath.Replace("\\", "/");
string sqlbak = string.Format("BACKUP DATABASE " + databaseName + " TO DISK = '{0}' WITH INIT", bakPath);
if (SqlHelper.ExecuteNonQuery(Common.objCom.GetConnectionString("DBMaster"), CommandType.Text, sqlbak) != 0)
return pathBak;
else
return string.Empty;
}
catch (Exception ex)
{
lbTip.Text = ex.Message;
lbTip.ForeColor = Color.FromName("#FF8080");
return string.Empty;
}
}