CSDN博客

img goody9807

ASP.NET下对远程SQL SERVER数据库的备份和恢复的存储过程

发表于2005/1/3 9:54:00  1529人阅读

Sql server的帮助文档地址,在IE里面直接输入就行
mk:@MSITStore:C:/Program%20Files/Microsoft%20SQL%20Server/80/Tools/Books/tsqlref.chm::/ts_ba-bz_35ww.htm

backup database to disk path备份
restore database from disk path恢复


using System;
using System.Collections;
using System.Security.Cryptography;

using System.Data;
using System.Data.SqlClient;
using System.Web;

using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Diagnostics ;
using System.Text ;
using System.ComponentModel;
using System.Configuration;
using System.Data.OleDb;

namespace DbService
{
 /// <summary>
 /// DbOper类,主要实现对Microsoft SQL Server数据库的备份和恢复
 /// </summary>
 public sealed class DbOper
 {
  /// <summary>
  /// DbOper类的构造函数
  /// </summary>
  //private DbOper()
  //{
  //}

  /// <summary>
  /// 数据库备份
  /// </summary>
  ///
  public static string BackFileName=System.Web.HttpContext.Current.Request.PhysicalApplicationPath+"BackUp//MyDbBack.bak";
  public static void DbBackup(string DbNanme,string userid,string pwd)
  {
   try
   {
    SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
    SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
    oSQLServer.LoginSecure = false;
    oSQLServer.Connect("localhost",userid,pwd);
    oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
    oBackup.Database = DbNanme;
    oBackup.Files =BackFileName;
    oBackup.BackupSetName =DbNanme;
    oBackup.BackupSetDescription = "数据库备份";
    oBackup.Initialize = true;
    oBackup.SQLBackup(oSQLServer);
   }
   catch
   {
    throw;
   }
  }

  

  /// <summary>
  /// 还原数据库函数
  /// </summary>
  /// <param name="strDbName">数据库名</param>
  /// <param name="strFileName">数据库备份文件的完整路径名</param>
  /// <returns></returns>
  public bool RestoreDB(string strDbName,string strFileName,string userid,string pwd)
  {
   //PBar = pgbMain ;
   SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
   try
   {
    //服务器名,数据库用户名,数据库用户名密码
    svr.Connect("localhost",userid,pwd) ;
    
    SQLDMO.QueryResults qr = svr.EnumProcesses(-1) ;
    int iColPIDNum = -1 ;
    int iColDbName = -1 ;
    for(int i=1;i<=qr.Columns;i++)
    {
     string strName = qr.get_ColumnName(i) ;
     if (strName.ToUpper().Trim() == "SPID")
     {
      iColPIDNum = i ;
     }
     else if (strName.ToUpper().Trim() == "DBNAME")
     {
      iColDbName = i ;
     }
     if (iColPIDNum != -1 && iColDbName != -1)
      break ;
    }
    //杀死使用strDbName数据库的进程
    for(int i=1;i<=qr.Rows;i++)
    {
     int lPID = qr.GetColumnLong(i,iColPIDNum) ;
     string strDBName = qr.GetColumnString(i,iColDbName) ;
     if (strDBName.ToUpper() == strDbName.ToUpper())
     {
      svr.KillProcess(lPID) ;
     }
    }
               
    SQLDMO.Restore res = new SQLDMO.RestoreClass() ;
    res.Action = 0 ;
    res.Files = strFileName ;

    res.Database = strDbName ;
    res.ReplaceDatabase = true ;
    res.SQLRestore(svr) ;
    return true ;
   }
   catch
   {
    return false;
   }
   finally
   {
    svr.DisConnect() ;
   }
  }

 }
}

0 0

相关博文

我的热门文章

img
取 消
img