Home  >  Article  >  Database  >  调用sql语句实现SqlServer的备份和还原,包括完整备份和差异备份

调用sql语句实现SqlServer的备份和还原,包括完整备份和差异备份

WBOY
WBOYOriginal
2016-06-07 15:51:421740browse

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 调用sql语句实现SqlServer的备份还原,包括完整备份和差异备份,因为执行备份还原需要一定的时间,因此需要设定 CommandTimeout参数。 /// summary /// 备份数据库 调用SQL语句 /// /summary /// pa

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入

  调用sql语句实现SqlServer的备份还原,包括完整备份和差异备份,因为执行备份还原需要一定的时间,因此需要设定 CommandTimeout参数。

  ///

  /// 备份数据库 调用SQL语句

  ///

  /// 备份文件名

  /// 0表示完整备份,为1表示差异备份

  ///

  public bool BackUPDB(string strFileName, int BackUpType)

  {

  //如果是差异备份,就是看一下文件是否存在,如果不存在,就不执行

  if (BackUpType == 1 && File.Exists(strFileName) == false)

  {

  return false;

  }

  bool result = false;

  try

  {

  string[] strConnSqlArr = strConnSql.Split(';');

  string DBName = strConnSqlArr[4].ToString()。Split('=')[1].ToString();//数据库名称

  string backUp_full = string.Format("backup database {0} to disk = '{1}' ;", DBName, strFileName);

  string backUp_Diff = string.Format("backup database {0} to disk='{1}' WITH DIFFERENTIAL ;", DBName, strFileName);

  WKK.DBUtility.DbHelperSQL.ExecuteSql(BackUpType == 0 ? backUp_full : backUp_Diff, 600);

  result = true;

  }

  catch (Exception ex)

  {

  Common.Log.WriteLog(string.Format("备份{0}数据库失败", BackUpType == 0 ? "完整" : "差异"), ex);

  //  System.Diagnostics.Debug.WriteLine(string.Format("备份{0}数据库失败", BackUpType == 0 ? "完整" : "差异"));

  result = false;

  }

  finally

  {

  if (result == true)

  {

  string str_InfoContent = string.Format("备份{0}数据库成功", BackUpType == 0 ? "完整" : "差异");

  // System.Diagnostics.Debug.WriteLine(str_InfoContent);

  }

  }

  return result;

  }

  ///

  /// 还原数据库 使用Sql语句

  ///

  /// 数据库名

  /// 备份文件名

  public bool RestoreDB(string strDbName, string strFileName)

  {

  bool result = false;

  try

  {

  string strConnSql = ConfigurationSettings.AppSettings["ConnectionString"].ToString();

  string[] strConnSqlArr = strConnSql.Split(';');

  string DBName = strConnSqlArr[4].ToString()。Split('=')[1].ToString();//数据库名称

  #region 关闭所有访问数据库的进程,否则会导致数据库还原失败 闫二永  17:39 2014/3/19

  string cmdText = String.Format("EXEC sp_KillThread @dbname='{0}'", DBName);

  WKK.DBUtility.DbHelperSQL.connectionString = strConnSql.Replace(DBName, "master");

  WKK.DBUtility.DbHelperSQL.ExecuteSql(cmdText);

  #endregion

  string Restore = string.Format("RESTORE DATABASE {0} FROM DISK='{1}'WITH replace", DBName, strFileName);

  WKK.DBUtility.DbHelperSQL.ExecuteSql(Restore, 600);

  result = true;

  }

  catch (Exception ex)

  {

  MessageBox.Show("还原数据库失败\r\n" + ex.Message, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);

  Common.Log.WriteLog(string.Format("还原数据库失败--{0}", DateTime.Now.ToString()), ex);

  result = false;

  }

  finally

  {

[1] [2] 

调用sql语句实现SqlServer的备份和还原,包括完整备份和差异备份

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn