This is a discussion on How to restore SQL Database within the Microsoft .NET forums, part of the Programming Talk category; I have a problem of restoring my SQL database using VB.NET. I got an error which says that the ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
How to restore SQL Database
I have a problem of restoring my SQL database using VB.NET. I got an error which says that the database is in use.
I used the following code please help me to resolve the problem by providing me a code that disconnect and connect the database Thank you Mesfin Sub RestoreDBFromFile(ByVal ServerName As String, ByVal DBName As String, ByVal BackupToRestore As String, byval usedatabase as String) Dim oServer As SQLDMO.SQLServer Dim oRestore As SQLDMO.Restore On Error GoTo Handler 'simple err checking If ServerName = "" Or DBName = "" Or BackupToRestore = "" Then MsgBox("You MUST provide server name, database name, and the name of the bak file you want to restore", vbInformation + vbOKOnly, "Error") Exit Sub End If 'open connection to server oServer = New SQLDMO.SQLServer With oServer .LoginSecure = True .Connect(ServerName) '.AttachDB("Master", "master.mdf") End With 'also need a restore object 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 = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database 'this is the "force restore over existing database" option .ReplaceDatabase = True 'this does a restore from a file instead of a device - note that we're still 'restoring a database, NOT a file group .Files = BackupToRestore 'do it .SQLRestore(oServer) End With 'standard clean up oRestore = Nothing oServer.DisConnect() oServer = Nothing Exit Sub Handler: If MsgBox(Err.Description & ". Would you like to continue?", vbInformation + vbYesNo) = vbYes Then Resume Next End If End Sub |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Database Tuning Advisor in SQL Server 2005 | Happy Harry | SQL Server | 2 | 04-18-2006 09:37 AM |
| SQL database back-up !!!! | Kartik | SQL Server 2005 Tutorials | 6 | 05-23-2005 02:48 PM |
| hierarchail & network | kisshug | Oracle Tutorials | 2 | 05-01-2005 05:17 PM |
| SQL SERVER CE | NareshShroff | Microsoft .NET Tutorials and Articles | 0 | 08-18-2004 05:32 AM |
| Migrating Oracle Databases to SQL Server 2000 | sanereddy | SQL Server 2005 Tutorials | 0 | 07-05-2004 08:37 AM |