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

     }
    }


  • 相关阅读:
    优化慢执行或慢查询的方法
    Top K问题的两种解决思路
    优先队列实现 大小根堆 解决top k 问题
    进程间的八种通信方式----共享内存是最快的 IPC 方式
    二叉树基础之按层打印
    按层打印二叉树--每行打印一层
    给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边
    缓存与数据库一致性保证
    一致性哈希算法原理
    Linux复制指定目录下的文件夹结构
  • 原文地址:https://www.cnblogs.com/goody9807/p/231057.html
Copyright © 2011-2022 走看看