开发者

Restoring database with SMO - Reporting progress problems

开发者 https://www.devze.com 2023-01-02 05:45 出处:网络
I\'m using the below to restore a database in VB.NET. This works but causes the interface to lockup if the user clicks anything.

I'm using the below to restore a database in VB.NET. This works but causes the interface to lockup if the user clicks anything. Also, I cannot get the progress label to update incrementally, it's blank until the backup is complete then开发者_StackOverflow中文版 displays 100%

Sub DoRestore()
    Dim svr As Server = New Server("Server\SQL2008")
    Dim res As Restore = New Restore()
    res.Devices.AddDevice("C:\MyDB.bak", DeviceType.File)
    res.Database = "MyDB"
    res.RelocateFiles.Add(New RelocateFile("MyDB_Data", "C:\MyDB.mdf"))
    res.RelocateFiles.Add(New RelocateFile("MyDB_Log", "C:\MyDB.ldf"))
    res.PercentCompleteNotification = 1
    AddHandler res.PercentComplete, AddressOf ProgressEventHandler
    res.SqlRestore(svr)
End Sub

Is this change correct?:

Private Sub ProgressEventHandler(ByVal sender As Object, ByVal e As PercentCompleteEventArgs)
    UpdateProgressBar(e.Percent)
End Sub

Private Sub UpdateProgressBar(ByVal e As String)
    ProgressBar.Value = e
    Status.Text = e.ToString
End Sub


You need to use SqlRestoreAsync not SqlRestore to prevent it tying up your main thread.

Then to avoid the Cross Thread Operation error when updating the UI you can use the approach here. Where you create a method on the form that will update the UI elements and call that from your asynch handler using Invoke.

0

精彩评论

暂无评论...
验证码 换一张
取 消