Exforsys
+ Reply to Thread
Results 1 to 2 of 2

How to restore SQL Database

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 database ...

  1. #1
    fikmesf is offline Junior Member Array
    Join Date
    Jul 2006
    Answers
    1

    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


  2. #2
    techguru is offline Senior Member Array
    Join Date
    Mar 2005
    Answers
    112
    You might has users connected to the database.. first you need clear those sessions before the restore.

    are you trying to restore master db..?

    try with some otehr database .. it should work...


Latest Article

Network Security Risk Assessment and Measurement

Read More...