July 21, 2003 at 5:09 pm
The following code will enable you to restore a SQL Server 2000 database from a backup device using VB.Net in the code-behind page of an ASP.Net web form. I found this code in an article written by Andy Warren in the Database Journal dated February 26, 2001. The article was located at the following URL: http://www.databasejournal.com/features/mssql/article.php/1479521 .
To use this code, you must add a reference in your VS.Net project to SQLDMO (it is located in the tab entitled "COM"). Additionally, you must import the SQLDMO namespace (i.e. "Imports SQLDMO").
********************
Imports SQLDMO
Private Sub RestoreDB()
' Dim the server and restore objects used for this procedure.
Dim objServer As SQLDMO.SQLServer
Dim objRestore As SQLDMO.Restore
' Create an instance of the server object and connect
' to the server.
objServer = New SQLDMO.SQLServer()
objServer.Connect("SQLServerName", "LoginName", "Password")
' Create an instance of the restore object, set variables and
' execute the restore procedure.
objRestore = New SQLDMO.Restore()
With objRestore
' This is the database to which your backup will be restored.
.Database = "MyDatabaseName"
' Set the restore action to restore the entire database.
.Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
' This will force the restore to overwrite the existing database.
.ReplaceDatabase = True
' This is the name of the backup device that contains the backup
' you wish to restore.
.Devices = "MyBackupDeviceName"
' Execute the restore procedure
.SQLRestore(objServer)
End With
' Clean up objects and disconnect from the server.
objRestore = Nothing
objServer.DisConnect()
objServer = Nothing
End Sub
********************
Please note: If you want to restore your database from a file instead of a backup device, you can replace the ".Devices = "MyBackupDeviceName" code above with the following:
.Files = "PathToYourBackupFile"
I hope the above code will prove useful for those of you, like me, that could not find adequate information on the topic.
July 22, 2003 at 9:49 am
Glad you found the article helpful. We have a good bit of other DMO information here.
Andy
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply