How to restore sql database RRS feed

  • Question

  • Hi Everyone

    I made a program for restore database from vb.net. but my code not correctly.the error show

    RESTORE cannot process database 'trydb' because it is in use by this session. It is recommended that the master database be used when performing this operation.

    RESTORE DATABASE is terminating abnormally.

    This is my code:

    Dim openfiledialog As New OpenFileDialog

            Dim restore As String

            restore = Path.GetDirectoryName(OpenFileDialog1.FileName) + "\" + Path.GetFileName(OpenFileDialog1.FileName)

            If OpenFileDialog1.ShowDialog = DialogResult.OK Then

                cmd = New SqlCommand("restore database [DRRM_Student_Database] from DISK ='" & restore & "' ", conn)




                MsgBox("restore success")


            End If

    Sunday, October 9, 2011 4:02 AM


  • Witwew,

    When creating your conn object, in the connection string, specify the master database as the initial database.  Don't have a session open to the trydb while you are backing it up.

    So you'll want a connection string similiar to this:

    Data Source=SQLServer1;Initial Catalog=master;User ID=sa;Password=***********

    Tom Overton
    • Edited by Tom_Overton Sunday, October 9, 2011 4:10 AM
    • Proposed as answer by Reza RaadMVP Sunday, October 9, 2011 4:58 AM
    • Marked as answer by Eileen Zhao Thursday, October 13, 2011 7:57 AM
    Sunday, October 9, 2011 4:05 AM