C# SQLSERVER数据库备份程序

软件功能:
       SQLSERVER数据库的备份和还原,有日志记录。傻瓜操作,避开了直接对SQLSERVER的操作。

 废话一堆:
     在公司做程序的期间我常常遇到客户不懂怎样备份数据库的问题,恰恰公司的开发语言有PB,DELPHI,C#等,所以公司没有花精力让我们做系统自动备份程序。要维护客户的程序,我们有时就需要得到用户数据库上的数据,但客户基本上都不会做备份操作,这样给我们的维护工作造成许多障碍。(废话真多)。于是,我写了下面这个程序。(功能不强,自己做的玩的哈)
     另外,软件上虽然有公司标志,请不要介意,我并没有将程序提交到公司。


     程序界面:

主要代码段:
 

//数据库的备份和实时进度显示代码:
  public bool BackUPDB(string ServerName,string UserName,string Password,string strDbName,string strFileName, ProgressBar pgbMain)
  {
           PBar = pgbMain ;
           SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
           try
           { 
                     svr.Connect(ServerName,UserName,Password) ;
                     SQLDMO.Backup bak = new SQLDMO.BackupClass();
                     bak.Action = 0 ;
                     bak.Initialize = true ;
                      SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
                     bak.PercentComplete += pceh;

                     bak.Files = strFileName;
                     bak.Database = strDbName;
                     bak.SQLBackup(svr);
    
                     return true ;
           }
           catch(Exception err)
           {
                    throw(new Exception("备份数据库失败"+err.Message)) ;
                    //return false ;
                   //MessageBox.Show("备份数据库失败"+err.Message);
            }
            finally
           {
                    svr.DisConnect() ;
            }
     }


//数据库的恢复的代码:
  public bool RestoreDB(string ServerName,string UserName,string Password,string strDbName,string strFileName, ProgressBar pgbMain)
  {
           PBar = pgbMain ;
           SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; 
           try
           {
                   svr.Connect(ServerName,UserName,Password) ; 
                   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 ; 
                     }

                    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 ; 
                   SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
                   res.PercentComplete += pceh;
                   res.Files = strFileName ;

                    res.Database = strDbName ;
                    res.ReplaceDatabase = true ;
                    res.SQLRestore(svr) ;
                    return true ;
            }
             catch(Exception err)
             {
                    throw(new Exception("恢复数据库失败,请关闭所有和该数据库连接的程序!"+err.Message)) ;
                   //return false ;
                  //MessageBox.Show("恢复数据库失败,请关闭所有和该数据库连接的程序!"+err.Message);
              }
               finally 
              {
                    svr.DisConnect() ;
               }
      }

帮助界面:



          就是一个简单的程序,界面用了制作不规则窗体的一点东西。主要是调用了SQL自己的东西做的。
          软件下载:/Files/allancandy/Debug.rar    (我懒,没有做安装包:))
          运行环境:自己只在win2000上用过,必要条件是先装.net framework

            希望大家多提意见和建议哈。

posted on 2005-08-23 11:20  李白  阅读(3681)  评论(3编辑  收藏  举报