Restoring from a device works almost exactly the same as restoring from a
file. The biggest difference is that if you're using a device, you just provide
the device name, not the complete path to it. Assuming I've restored database
Pubs to a device called Pubs, this is how you would use this code:
Call RestoreDBFromDevice("ANDY","Pubs","Pubs")
Sub RestoreDBFromDevice(ServerName As String, DBName As String,
DeviceNameToRestore As String)
Dim oServer As SQLDMO.SQLServer
Dim oRestore As SQLDMO.Restore
On Error GoTo Handler
'simple err checking
If ServerName = "" Or DBName = "" Or
DeviceNameToRestore = "" Then
MsgBox "You MUST provide server name, database name,
and the name of the device you want to restore",
vbInformation + vbOKOnly, "Error"
Exit Sub
End If
'open connection to server
Set oServer = New SQLDMO.SQLServer
With oServer
.LoginSecure = True
.Connect ServerName
End With
'also need a restore object
Set oRestore = New SQLDMO.Restore
'use the 'with' construct to minimize property lookups
With oRestore
'this is where your backup will be restored to
.Database = DBName
'same as EM or TSQL, you can restore database, file, or log, here we're going
to
'use database
.Action = SQLDMORestore_Database
'this is the "force restore over existing database" option
.ReplaceDatabase = True
'this time we're using a device, so it's only a little different - the device
name
'is all you need, not the path to where the device is stored
.Devices = DeviceNameToRestore
'do it
.SQLRestore oServer
End With
'standard clean up
Set oRestore = Nothing
oServer.DisConnect
Set oServer = Nothing
Exit Sub
Handler:
If MsgBox(Err.Description & ". Would you like to continue?",
vbInformation + vbYesNo) = vbYes Then
Resume Next
End If
End Sub
|