zoukankan      html  css  js  c++  java
  • [转贴]ASP.NET下对远程SQL SERVER数据库的备份和恢复的存储过程

    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() ;
       }
      }

     }
    }


  • 相关阅读:
    小程序中阿里图标引入方式
    微信小程序自定义下导航页面切换效果的合理写法
    小程序自定义下导航效果简单实现
    自适应布局all样式
    兼容html5新标签及媒体查询引入插件
    all.css
    一个简单遮罩弹窗效果
    前端代码标准最佳实践:CSS
    复选框常用选中状态判断
    使输入框(input  & textarea)变为只可读状态readonly="readonly",禁用输入框disabled="disabled"
  • 原文地址:https://www.cnblogs.com/goody9807/p/231057.html
Copyright © 2011-2022 走看看