0

Error : *"Restore failed for Server '\.\pipe\81DAC374-2583-49\tsql\query'."*

i get the error here resDB.SqlRestore(sv) My backup works fine but restore fails.i am trying to get it working from the past two days and no luck yet.

   Public Shared Sub RestoreBackup()
        Dim sConnect As String = My.Settings.LICConnectionString
        Dim dbName As String

        Using cnn As New SqlConnection(sConnect)
            cnn.Open()
            dbName = cnn.Database.ToString()
            cnn.ChangeDatabase("master")

            Dim sc As New ServerConnection(cnn)
            Dim sv As New Server(sc)

            ' Check that I'm connected to the user instance
            MsgBox(sv.InstanceName.ToString())

            ' Create backup device item for the backup
            Dim bdi As New BackupDeviceItem("C:\Backup\LIC.bak", DeviceType.File)

            ' Create the restore object
            Dim resDB As New Restore()
            resDB.Devices.Add(bdi)
            resDB.NoRecovery = False
            resDB.ReplaceDatabase = True
            resDB.Database = dbName

            ' Restore the database
            resDB.SqlRestore(sv)
            MsgBox("Your database has been restored.")
        End Using
    End Sub

Why does it fail??Any Help Appreciated.

Sunny Bhattacharjee
  • 107
  • 1
  • 3
  • 10

1 Answers1

1

It looks like you're trying to restore over the master database. You will need to change cnn.ChangeDatabase("master") to cnn.ChangeDatabase("LIC").

EDIT:

Alternatively, remove the line cnn.ChangeDatabase("master") altogether and add the following line after instantiating the Restore class:

resDB.Action = RestoreActionType.Database

Also, if the backup is from another server you may want to look at using RelocateFile - How to restore a database from C#

Community
  • 1
  • 1
Kev Ritchie
  • 1,599
  • 10
  • 16
  • [http://blogs.msdn.com/b/sqlexpress/archive/2007/03/20/backup-and-restore-with-user-instances.aspx](http://blogs.msdn.com/b/sqlexpress/archive/2007/03/20/backup-and-restore-with-user-instances.aspx) – Sunny Bhattacharjee Jan 15 '12 at 08:28
  • @SunnyBhattacharjee I have updated the answer re. the link you have posted. – Kev Ritchie Jan 15 '12 at 11:31