我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > net专区 > 用ASP.NET还原与恢复SQLServer数据库
热门文章排行
热门文章排行 检查email地址格式的代码(01-11)
PHP操作文件问答(01-11)
PHP安装攻略:安装并配置PHP(10-23)
PHP的十个高级技巧 4(10-23)
关于APE的介绍、播放及制作(03-22)
精采文章排行
精采文章排行 ASP.NET与MySQL数据库简明图示入门教(11-16)
ASP.NET与MySQL数据库简明图示入门教(11-16)
ASP.NET 链接数据库基础(11-16)
webconfig的设置节点说明(11-16)
部署ASP.NET的三大技术(上)(11-16)
技术专题推荐
网管论坛交流
 

用ASP.NET还原与恢复SQLServer数据库 

作者:佚名   来源:Linux 宝库   点击:   日期:2006-12-22


  需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:
  
  create proc killspid (@dbname varchar(20))
  as
  begin
  declare @sql nvarchar(500)
  declare @spid int
  set @sql='declare getspid cursor for
  select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
  exec (@sql)
  open getspid
  fetch next from getspid into @spid
  while @@fetch_status<>-1
  begin
  exec('kill '+@spid)
  fetch next from getspid into @spid
  end
  close getspid
  deallocate getspid
  end
  GO
  
  在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)
  
  using System;
  
  using System.Configuration;
  
  using System.Data.SqlClient;
  
  using System.Data;
  
  namespace web.base_class
  
  {
  
  /// <summary>
  
  /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复
  
  /// </summary>
  
  public class DbOper
  
  {
  
  private string server;
  
  private string uid;
  
  private string pwd;
  
  private string database;
  
  private string conn;
  
  /// <summary>
  
  /// DbOper类的构造函数
  
  /// </summary>
  
  public DbOper()
  
  {
  
  conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
  
  server=cut(conn,"server=",";");
  
  uid=cut(conn,"uid=",";");
  
  pwd=cut(conn,"pwd=",";");
  
  database=cut(conn,"database=",";");
  
  }
  
  public string cut(string str,string bg,string ed)
  
  {
  
  string sub;
  
  sub=str.Substring(str.IndexOf(bg)+bg.Length);
  
  sub=sub.Substring(0,sub.IndexOf(";"));
  
  return sub;
  
  }
  
  
  
  /// <summary>
  
  /// 数据库备份
  
  /// </summary>
  
  public bool DbBackup(string url)
  
  {
  
  SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
  
  SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
  
  try
  
  {
  
  oSQLServer.LoginSecure = false;
  
  oSQLServer.Connect(server,uid, pwd);
  
  oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
  
  oBackup.Database = database;
  
  oBackup.Files = url;//"d:\Northwind.bak";
  
  oBackup.BackupSetName = database;
  
  oBackup.BackupSetDescription = "数据库备份";
  
  oBackup.Initialize = true;
  
  oBackup.SQLBackup(oSQLServer);
  
  return true;
  
  }
  
  catch
  
  {
  
  return false;
  
  throw;
  
  }
  
  finally
  
  {
  
  oSQLServer.DisConnect();
  
  }
  
  }
  
  /// <summary>
  
  /// 数据库恢复
  
  /// </summary>
  
  public string DbRestore(string url)
  
  {
  
  if(exepro()!=true)//执行存储过程
  
  {
  
  return "操作失败";
  
  }
  
  else
  
  {
  
  SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
  
  SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
  
  try
  
  {
  
  oSQLServer.LoginSecure = false;
  
  oSQLServer.Connect(server, uid, pwd);
  
  oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
  
  oRestore.Database = database;
  
  oRestore.Files = url;//@"d:\Northwind.bak";
  
  oRestore.FileNumber = 1;
  
  oRestore.ReplaceDatabase = true;
  
  oRestore.SQLRestore(oSQLServer);
  
  return "ok";
  
  }
  
  catch(Exception e)
  
  {
  
  return "恢复数据库失败";
  
  throw;
  
  }
  
  finally
  
  {
  
  oSQLServer.DisConnect();
  
  }
  
  }
  
  }
  
  private bool exepro()
  
  {
  
  SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");
  
  SqlCommand cmd = new SqlCommand("killspid",conn1);
  
  cmd.CommandType = CommandType.StoredProcedure;
  
  cmd.Parameters.Add("@dbname","port");
  
  try
  
  {
  
  conn1.Open();
  
  cmd.ExecuteNonQuery();
  
  return true;
  
  }
  
  catch(Exception ex)
  
  {
  
  return false;
  
  }
  
  finally
  
  {
  
  conn1.Close();
  
  }
  
  }
  
  }
  
  }





文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·给你的FileSystemObject对象加把锁 ·在 Web 页上使用条件数值格式
·连接数据库查询手册(不仅仅适用于asp) ·警惕"给你的FileSystemObject对象加把锁"
·用ASP做全文检索 ·如何把ASP编写成DLL

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。