注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

零售创新,创新那些事儿,SPSS,VBA

零售创新

 
 
 

日志

 
 
关于我

新浪微博,零售创新 研究经理,数据分析师 希望和市场研究和零售业的同事共同进步! 本博客发表的都是免费或试用的资料,如果有版权问题请发邮件wangli12a@163.com联系删除。 spss excel vba blog

网易考拉推荐

SQL数据备份和恢复模块  

2007-07-05 11:12:07|  分类: VB和VBA知识 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
SQL数据备份和恢复模块


这是我的数据备份和恢复模块
Option Explicit
  Public conn As New ADODB.Connection '数据库连接变量
  Private WithEvents objBackup       As SQLDMO.Backup
  Private WithEvents objRestore       As SQLDMO.Restore
  Public isql As String
   
  Private Sub cmdBackup_Click() '备份按钮
          Dim objSQLServer     As New SQLDMO.SQLServer
          Dim strServer     As String
          Dim strUserID     As String
          Dim strPassword     As String
          Dim strDatabase     As String
          Dim strFile     As String
          
          strServer = "127.0.0.1"
          strUserID = "sa"
          strPassword = ""
          strDatabase = "lifei"
          strFile = App.Path & "\数据备份\bak"
          
          If Dir(strFile) <> "" Then
                  If MsgBox("文件" & strFile & "已存在,是否删除?", vbQuestion + vbYesNo) = vbYes Then
                          Kill strFile
                  Else
                          Exit Sub
                  End If
          End If
          
          lblProgress.Caption = "备份进度:   0%"
          Screen.MousePointer = 11
          
          On Error GoTo ErrorHandler
          
          objSQLServer.Connect strServer, strUserID, strPassword
          
          Set objBackup = New SQLDMO.Backup
          With objBackup
                  .PercentCompleteNotification = 1
                  .Database = strDatabase
                  .Files = strFile
                  .SQLBackup objSQLServer
          End With
          Set objBackup = Nothing
          
          objSQLServer.Close
          Set objSQLServer = Nothing
          
          Screen.MousePointer = 0
          Exit Sub
          
ErrorHandler:
          Screen.MousePointer = 0
          MsgBox Err.Description, vbCritical
  End Sub
  
  Private Sub cmdRestore_Click()
 
          Dim objSQLServer     As New SQLDMO.SQLServer
          Dim strServer     As String
          Dim strUserID     As String
          Dim strPassword     As String
          Dim strDatabase     As String
          Dim strFile     As String
          
          strServer = "127.0.0.1"
          strUserID = "sa"
          strPassword = ""
          strDatabase = "lifei"
          strFile = App.Path & "\数据备份\bak"
          
          If Dir(strFile) = "" Then
                  MsgBox "文件" & strFile & "不存在!", vbExclamation
                  Exit Sub
          End If
          
          lblProgress.Caption = "恢复进度:   0%"
          Screen.MousePointer = 11
          
          On Error GoTo ErrorHandler
          
          objSQLServer.Connect strServer, strUserID, strPassword
          
          Set objRestore = New SQLDMO.Restore
          With objRestore
                  .PercentCompleteNotification = 1
                  .Database = strDatabase
                  .ReplaceDatabase = True
                  .Files = strFile
                  .SQLRestore objSQLServer
          End With
          Set objRestore = Nothing
          
          objSQLServer.Close
          Set objSQLServer = Nothing
          
          Screen.MousePointer = 0
          Exit Sub
          
ErrorHandler:
          Screen.MousePointer = 0
          MsgBox Err.Description, vbCritical
  End Sub
  
Private Sub Command1_Click()
Unload Me
End Sub


Private Sub Form_Unload(Cancel As Integer)
If conn.State <> 0 Then
        conn.Close
        Set conn = Nothing
    End If
End Sub

  Private Sub objBackup_Complete(ByVal Message As String)
          lblProgress.Caption = "备份成功!"
          Set objBackup = Nothing
  End Sub
  
  Private Sub objBackup_PercentComplete(ByVal Message As String, ByVal Percent As Long)
          lblProgress.Caption = "备份进度:   " & Percent & "%"
          DoEvents
  End Sub
  
  Private Sub objRestore_Complete(ByVal Message As String)
          lblProgress.Caption = "恢复成功!"
          Set objRestore = Nothing
  End Sub
  
  Private Sub objRestore_PercentComplete(ByVal Message As String, ByVal Percent As Long)
          lblProgress.Caption = "恢复进度:   " & Percent & "%"
          DoEvents
  End Sub

 
  评论这张
 
阅读(540)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017