In my application I want the show the SQL Query progress. For example, assume a user clicked on a backup button, then I want him to be able to view the progress of the query he made in case the database is too big to restore or backup. At first I tried to do this by assuming how much disk space the database might take before even start backing up and then using that with the copying rate to estimate a time it might take to backup. But unfortunately I failed. Then by luck I found one lovely T-SQL command that show the stats when used in SSMS and it is just using an extra line 'With Stats = [Some integer] e.g. 1, 10, 20, etc' but here is the problem. I cannot find any work through for this to work with Vb.Net aka my application.
I found this awesome query from this link (www.mssqltips.com). If you open the website you can see few examples showing this.
The code to get stats:
Use dbName Backup dbName To Disk='BackupLocation' With STATS = 10
Edit #1:
Adding these lined of code now does showing the InfoMessage I was searching for but still not useful as this shows up only when the full operation is completed. But what I want is to show them dynamically.
Code that worked:
AddHandler con.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)
Private Sub OnInfoMessage(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs)
strInfo.AppendLine(e.Message)
txtMsg.Text = strInfo.ToString
End Sub
My complete code:
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Text
Public Class BackupForm
Dim ConnectionString As String = My.Settings.LADBConnectionString
Dim con As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand()
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Using OFD As New SaveFileDialog
With OFD
.FileName = "Backup " + Now.ToString("dd-MM-yyyy")
.Filter = "Log Application Backup |*.bak"
.CheckFileExists = False
.OverwritePrompt = False
Select Case .ShowDialog
Case DialogResult.OK
If .FileName <> "" Then
txtBackup.Text = .FileName
End If
End Select
End With
End Using
End Sub
Private Sub btnBackup_Click(sender As Object, e As EventArgs) Handles btnBackup.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Dim strInfo As New StringBuilder
Private Sub OnInfoMessage(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs)
strInfo.AppendLine(e.Message)
txtMsg.Text = strInfo.ToString
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim backupQuery As String = $"use LDDB Backup database LDDB to Disk='{txtBackup.Text}' With STATS = 1"
Try
con.Open()
cmd.CommandType = CommandType.Text
cmd.CommandText = backupQuery
cmd.Connection = con
AddHandler con.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
con.Close()
con.Dispose()
End Try
End Sub
Private Sub BackupForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
End Class