用A.NET恢复备份Sql server(3)_.NET概论及软件使用_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

用A.NET恢复备份Sql server(3)

作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0

本篇关键词:备份恢复'string
黑客防线网安网讯: else  {  dbName = txtDbName.Text.Trim();  }   string SqlStr1 = "Data Source=.\\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True";  stri...

 else

  {

  dbName = txtDbName.Text.Trim();

  }
   string SqlStr1 = "Data Source=.\\sqlexpress;Initial Catalog='" + dbName + "';Integrated Security=True";
  string SqlStr2 = @"use master restore database " + dbName + " from disk='" + path + "'";

  SqlConnection con = new SqlConnection(SqlStr1);

  con.Open();

  try

  {

  SqlCommand com = new SqlCommand(SqlStr2, con);

  com.ExecuteNonQuery();

  Response.Write("<script language=javascript>alert('还原数据成功!');'</script>");

  }

  catch (Exception error)

  {

  Response.Write(error.Message);

  Response.Write("<script language=javascript>alert('还原数据失败!')</script>");

  txtDbName.Text = SqlStr2;

  }

  finally

  {

  con.Close();

  }

  }

  /// <summary>

  /// 恢复数据库可选择是否可以强制还原(即在其他人在用的时候依然可以还原)

  /// </summary>

  /// <param name="databasename">待还原的数据库名称</param>

  /// <param name="databasefile">带还原的备份文件的完全路径</param>

  /// <param name="errormessage">恢复数据库失败的信息</param>

  /// <param name="forceRestore">是否强制还原(恢复),如果为TRUE,则exec killspid '数据库名' 结束此数据库的进程,这样才能还原数据库</param>

  /// <returns></returns>

  public bool RestoreDataBase(string databasename, string databasefile, ref string returnMessage, bool forceRestore, SqlConnection conn)

  {

  bool success = true;

  string path = databasefile;

  string dbname = databasename;

  string restoreSql = "use master;";

  if (forceRestore)//如果强制回复

  restoreSql += string.Format("use master exec killspid '{0}';", databasename);

  restoreSql += "restore database @dbname from disk = @path;";

  SqlCommand myCommand = new SqlCommand(restoreSql, conn);

  myCommand.Parameters.Add("@dbname", SqlDbType.Char);

  myCommand.Parameters["@dbname"].Value = dbname;

  myCommand.Parameters.Add("@path", SqlDbType.Char);

  myCommand.Parameters["@path"].Value = path;

  Response.Write(restoreSql);

  try

  {

  myCommand.Connection.Open();

  myCommand.ExecuteNonQuery();

  returnMessage = "还原成功";

  }

  catch (Exception ex)

  {

  returnMessage = ex.Message;

  success = false;

  }

  finally

  {

  myCommand.Connection.Close();

  }

  return success;

  }

  protected void Button3_Click(object sender, EventArgs e)

  {

  string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称

  string dbName = string.Empty;

  if (DropDownList1.Items.Count != 0)

  {

  dbName = DropDownList1.SelectedValue.Trim();

  }

  else

  {

  dbName = txtDbName.Text.Trim();

  }

  string returnMessage = string.Empty;

  string SqlStr1 = "Data Source=.\\sqlexpress;Initial Catalog='

 

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14193-1.html
网站维护教程更新时间:2012-03-30 05:30:06  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer