June 23, 2012 at 7:03 pm
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
June 28, 2012 at 5:47 am
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
June 29, 2012 at 10:06 am
Try setting the NoRecovery (rstDb.NoRecovery) property to false before calling the SQLRestore method.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply