Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restore A SQL 2000 Database In ASP.Net Using SQL-DMO

Status
Not open for further replies.

JabbaTheNut

Programmer
Jul 29, 2002
176
US
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: .

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.

Game Over, Man!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top