Public M_Style As Long
Private WithEvents objBackup As SQLDMO.Backup
Private WithEvents objRestore As SQLDMO.Restore
'
Private Sub DbBackup()
Dim objSQLServer As New SQLDMO.SQLServer
Dim strServer As String
Dim strUserID As String
Dim strPassword As String
Dim strDatabase As String
If Len(Dir$(TxtNNFileName)) > 0 Then
If MsgBox("文件" & TxtNNFileName & "已存在,是否覆盖?", vbQuestion + vbYesNo) = vbYes Then
Kill TxtNNFileName
Else
Exit Sub
End If
End If
Set objBackup = New SQLDMO.Backup
With objBackup
.PercentCompleteNotification = 1
.Database = strDatabase
.Files = TxtNNFileName
.SQLBackup objSQLServer
End With
Set objBackup = Nothing
objSQLServer.Close
Set objSQLServer = Nothing
Screen.MousePointer = 0
Exit Sub
ErrorHandler:
Screen.MousePointer = 0
Label2.Caption = ""
MsgBox Err.Description, vbCritical
End Sub
'
Private Sub DbRestore()
Dim objSQLServer As New SQLDMO.SQLServer
Dim Cnn As New ADODB.Connection
Dim IRs As New ADODB.Recordset
Dim strServer As String
Dim strUserID As String
Dim strPassword As String
Dim strDatabase As String
Dim StrSql As String
Dim ConnStr As String
'关闭所有用户进程
ConnStr = "Driver={SQL Server};"
ConnStr = ConnStr & "Server=" & strServer & ";"
ConnStr = ConnStr & "uid=" & strUserID & ";pwd=" & strPassword & ";"
Cnn.Open ConnStr
StrSql = "select spid from master..sysprocesses where dbid=db_id('" & strDatabase & "')"
IRs.Open StrSql, Cnn, adOpenKeyset, adLockReadOnly
While IRs.EOF = False
StrSql = "kill " & IRs(0)
Cnn.Execute StrSql
IRs.MoveNext
Wend
If IRs.State = 1 Then IRs.Close
If Cnn.State = 1 Then Cnn.Close
Set IRs = Nothing
Set Cnn = Nothing
'-------------------------------------------
objSQLServer.Connect strServer, strUserID, strPassword
Set objRestore = New SQLDMO.Restore
With objRestore
.PercentCompleteNotification = 1
.Database = strDatabase
.ReplaceDatabase = True
.Files = TxtNNFileName
.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 CmdNNCan_Click()
On Error Resume Next
P_Cnn.Open
Unload Me
End Sub
Private Sub CmdNNFileName_Click()
On Error Resume Next
DigNNFileName.filter = "数据库备份文件(*.DBK)|*.DBK"
If M_Style = 1 Then '//备份
DigNNFileName.ShowSave
TxtNNFileName = DigNNFileName.FileName
Else '//恢复
DigNNFileName.ShowOpen
TxtNNFileName = DigNNFileName.FileName
End If
End Sub
Private Sub CmdNNOK_Click()
If Len("strDatabase") = 0 Then
MsgBox "数据库名不能为空!", vbOKOnly, Me.Caption
Exit Sub
End If
ProgressBar1.Max = 100
ProgressBar1.Min = 0
ProgressBar1.Value = 0
If M_Style = 1 Then '//备份
Call DbBackup
Else '//恢复
Call DbRestore
End If
End Sub
Private Sub Form_Load()
TxtDBName = SetStarReco.DbName
If P_Cnn.State = 1 Then P_Cnn.Close
SetComColor.SetComFore Me, RGB(0, 0, 0)
Call P_WinCtrl.NoClose(Me.hwnd)
End Sub
Private Sub objBackup_Complete(ByVal Message As String)
Label2.Caption = "备份成功!"
Set objBackup = Nothing
End Sub
Private Sub objBackup_PercentComplete(ByVal Message As String, ByVal Percent As Long)
lblProgress.Caption = Percent & "%"
ProgressBar1.Value = Percent
DoEvents
End Sub
Private Sub objRestore_Complete(ByVal Message As String)
Label2.Caption = "恢复成功!"
Set objRestore = Nothing
End Sub
Private Sub objRestore_PercentComplete(ByVal Message As String, ByVal Percent As Long)
lblProgress.Caption = Percent & "%" '//这里显示备份的百分比.
ProgressBar1.Value = Percent '//备份的进度.
DoEvents
End Sub
Private Sub TxtNNFileName_Change()
CmdNNOK.Enabled = CBool(Len(TxtNNFileName))
End Sub