Today, when I did a program to restore the database, it suddenly occurred to me that SQL Server had a powerful component SQLDMO, and it was helpful to check it out to meet my needs:
Here is the code for my program, where you share the group.
The following code is compiled in the VB6 Chinese version!
To start a data service:
Public Sub Start_server ()
Dim SVR as New sqldmo.sqlserver
On Error GoTo Errhwnd
Svr. Start False, MyServer
Svr. Close
Exit Sub
Errhwnd:
If Err.Number <> -2147023840 Then
MsgBox Err.Number & vbCrLf & Err.Description, vbexclamation, "error"
End If
End Sub
Join and verify Password:
Public Function connserver (ByVal SQL Server as String, ByVal uid As String, ByVal pwd as String) as Boolean
Dim St as Sqldmo_svcstatus_type
Dim SVR as New sqldmo.sqlserver
On Error GoTo Errhwnd
Svr. LoginTimeout = 10
Svr. Connect SQL Server, UID, pwd
Connserver = True
Exit Function
Errhwnd:
If Err.Number = -2147203048 Then
MsgBox "Login failed password is incorrect, please re-enter!" ", vbinformation," hint "
Else
Resume Next
End If
End Function
If you want to redeploy the database when restoring the database the location of the special files should be noted
To recover a database:
Public WithEvents ores as SQLDMO. Restore ' Declare event note this row is placed before the procedure call
Private Sub Restoredata (ByVal datafile as String)
On Error GoTo ERRHWD
Dim OSQL as Sqldmo.sqlserver
Set ores = New SQLDMO. Restore
Set OSQL = New sqldmo.sqlserver
Osql.loginsecure = False
Osql.connect ServerName, SqlUser, sqlpwd ' Join Data Services
Ores. Action = Sqldmorestore_database ' recovery type database
Ores. db = "Rcrs" database name
Ores. ReplaceDatabase = True ' replaces an existing database and creates it if it is not saved
Ores. Files = datafile ' backup filename, if required on multiple devices. Devices Property
Ores. DatabaseFiles = datafile ' database file This parameter must be set otherwise cannot be used RelocateFiles
Ores. filenumber = 1 ' file ID number on device if you only have one file, it's 1.
' RelocateFiles database logical name ' [Logical name],[physical file name address] '
Ores. RelocateFiles = "[Tyrs2_data]" & "," & "[" & App.Path & "\rcrs_data.mdf]," & _
[Tyrs2_log] "&", "&" ["& App.Path &" \rcrs_log.ldf] "
DoEvents
Ores. SQLRestore OSql ' Invoke recovery method
Set ores = Nothing
Set oSql = Nothing
Exit Sub
ERRHWD:
Select Case Err.Number
Case-2147221499
' This error does not yet know the cause, but does not affect the result of the operation
Resume Next
Case-2147218403
MsgBox database is in use, close all programs that are using data! ", vbexclamation," wrong "
Case Else
MsgBox Err.Number & vbCrLf & Err.Description, vbexclamation, "Tips"
End Select
End Sub
' Recovery events
Private Sub ores_percentcomplete (ByVal message as String, ByVal Percent as Long)
Probar.value = Probar.max * (percent/100) ' Set progress bar '
End Sub