Solved

Help restoring database

Posted on 2006-11-08
4
184 Views
Last Modified: 2010-04-30
Hi folks, I'm using the below function which I knocked together from a couple of examples to try and restore a database back into MS SQL.  The function starts running, but the VB app just hangs and doesn't resume processing with any reasonable time (at the moment just doing 50MB restore which takes couple of seconds usually).

Any help would be greatly appreciated... Basically the DB I'm trying to restore is one that can be connected to multiple times by the same user.

Private Function RestoreBackup(sFile As String) As Boolean
    On Error GoTo errH
   
    Dim dbHost As String
    Dim dbName As String
   
    dbHost = cFMSettings("database|host")
    dbName = cFMSettings("database|name")
   
    Dim oConn As New ADODB.Connection
    Dim rs As Recordset
   
    Dim sConn As String
   
    sConn = "Driver={SQL Server};" & _
           "Server=" & dbHost & ";" & _
           "Database=" & dbName & ";" & _
           "Uid=user;" & _
           "Pwd=pass"
           
    oConn.Open sConn
   
    Dim sSQL As String
   
    sSQL = "USE master;" & vbCrLf & _
           "ALTER DATABASE " & dbName & " SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;" & vbCrLf & _
           "RESTORE DATABASE " & dbName & " FROM DISK = '" & sFile & "';" & vbCrLf '& _
           "ALTER DATABASE " & dbName & " SET MULTI_USER WITH ROLLBACK IMMEDIATE;"
   
    Set rs = oConn.Execute(sSQL)
   
    'If RestoreDatabaseOK(oConn, dbName, sFile) = True Then
    '    RestoreBackup = True
    'Else
    '    RestoreBackup = False
    'End If
   
    RestoreBackup = True
    Exit Function
   
errH:
    If Err.Number = -2147217900 Then
        Resume
    Else
        MsgBox Err.Number & " " & Err.Description
    End If
    RestoreBackup = False
    Exit Function
End Function




The if err.number = -2147217900 part is supposedly there to enable to code to continue running when it receives the "Changed context to.. " message which apparently is just an information message rather than an error.

Thanks in advance
0
Comment
Question by:ShelfieldCollege
  • 3
4 Comments
 
LVL 4

Author Comment

by:ShelfieldCollege
ID: 17898616
oh and the commented out 'If RestoreDatabaseOK(oConn, dbName, sFile) = True Then parts are just from me attempting to use another function found on here for restoring databases, however that function took the DB offline, failed restoring then didn't put it back online... I got a feeling it's not the fault of the original author of the RestoreDatabaseOK funciton, its something else I'm doing wrong.

Anyways, thats besides the point, I'd like to do it using the SQL already there or something similar if that's possble

Cheers
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 17898655
>           "Database=" & dbName & ";" & _

DON'T connect to the database you want to restore.
using the USE MASTER in the sql will not help.
0
 
LVL 4

Author Comment

by:ShelfieldCollege
ID: 17899071
ahh I thought just using USE MASTER would free up the database, I'll give it a try, thanks.
0
 
LVL 4

Author Comment

by:ShelfieldCollege
ID: 17905305
Hi ya, thanks for your comment.  After a little more work (turns out the user im connecting as didn't have permission to restore the database which messed it up again, so now my application prompts the user for their SA password to do the restore.

And all works well, cheers :)

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question