可以用SQL DEMO组件来操作。 我写的一个备份恢复工具:
源代码如下: Option ExplicitPrivate Sub Command1_Click() On Error GoTo ErrHandler Dim Srv As SQLDMO.SQLServer Dim Rest As SQLDMO.Restore Dim Bak As SQLDMO.Backup
If Check = False Then Exit Sub Me.MousePointer = vbHourglass If Option2.Value = True Then Set Srv = New SQLDMO.SQLServer Srv.LoginTimeout = 15 Srv.Connect "(local)", txtUserID.Text, txtPassword.Text Set Rest = New SQLDMO.Restore Rest.Action = 0 ' full db restore Rest.Database = txtDBName.Text Rest.Devices = "" Rest.Files = txtBackupFile.Text Rest.ReplaceDatabase = True 'Force restore over existing database Rest.SQLRestore Srv Set Rest = Nothing Set Srv = Nothing MsgBox "Database restore succeed.", vbInformation, "OK" Else Set Srv = New SQLDMO.SQLServer Srv.LoginTimeout = 15 Srv.Connect "(local)", txtUserID.Text, txtPassword.Text Set Bak = New SQLDMO.Backup Bak.Database = txtDBName.Text Bak.Devices = "" Bak.Files = txtBackupFile.Text Bak.SQLBackup Srv Set Bak = Nothing Set Srv = Nothing MsgBox "Database backup succeed.", vbInformation, "OK" End If Me.MousePointer = vbDefault Exit Sub ErrHandler: Me.MousePointer = vbDefault MsgBox "Error:" & Err.Description, vbCritical, "Error" End Sub
Private Function Check() As Boolean Check = False If Trim(txtUserID.Text) = "" Then MsgBox "Please input the DBA user ID!", vbCritical, "Error" txtUserID.SetFocus Exit Function End If If Trim(txtPassword.Text) = "" Then MsgBox "Please input the DBA password!", vbCritical, "Error" txtPassword.SetFocus Exit Function End If If Trim(txtDBName.Text) = "" Then MsgBox "Please input the database name!", vbCritical, "Error" txtDBName.SetFocus Exit Function End If
If Trim(txtBackupFile.Text) = "" Then MsgBox "Please input the backup file path!", vbCritical, "Error" txtBackupFile.SetFocus Exit Function End If Check = True End Function
Private Function GetFileName(FileName As String) As String GetFileName = Mid(FileName, InStrRev(FileName, "\") + 1) End Function
Private Sub Command2_Click() Unload Me End Sub
Private Sub Form_Load() Dim sysPath As String sysPath = IIf(Len(App.Path) = 3, App.Path, App.Path & "\") txtBackupFile.Text = sysPath & "PrintMate.bak" End Sub