SMO Restore works only if I change the system date.

  • Hi,

    I am using the (SQL Express ) bellow given code to restore data from *.bak file (A backup file created earlier by SQL backup). Some times this code do not restore the data and gives error "Restore Failed".

    But when I change the system date to next day date then it restore data successfully.

    What is the reason of this problem & How to solve this issue. VB code used is as follow:

    Try

    Dim strFileName As String

    Dim rstDb As New Restore

    Dim srv As Server

    srv = New Server("(local)\SQLEXPRESS")

    Dim db As Database

    db = srv.Databases("mydatabase")

    strFileName = txtrestore.Text

    rstDb.Database = db.Name

    rstDb.Action = RestoreActionType.Database

    rstDb.Devices.Add(New Microsoft.SqlServer.Management.Smo.BackupDeviceItem(strFileName, DeviceType.File))

    rstDb.UnloadTapeAfter = True

    rstDb.ReplaceDatabase = True

    rstDb.SqlRestore(srv)

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    Thanks

  • Finally I have got the solution,

    I have executed the queries in the below given order :

    1. User Master

    2. ALTER DATABASE mydatabasename SET OFFLINE WITH Rollback Immediate

    3. Restore command as in question post.

    4. ALTER DATABASE mydatabasename SET ONLINE

    Now it is working well.

    Thanks

  • Try setting the NoRecovery (rstDb.NoRecovery) property to false before calling the SQLRestore method.


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply