ASP.NET中怎样实现数据库备份
展开全部
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;
}
}
/// <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;
}
}
壹寰(深圳)科技文化有限公司
2021-03-27 广告
2021-03-27 广告
不同公司提供的方案也不一样。以上回答如果还觉得不够详细,可以来咨询下壹寰(深圳)科技文化有限公司。91数据恢复是壹寰(深圳)科技文化有限公司旗下专业数据恢复品牌,91数据恢复专注于勒索病毒数据恢复、勒索病毒数据修复、数据库修复、数据库解密恢...
点击进入详情页
本回答由壹寰(深圳)科技文化有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询