Link to home
Start Free TrialLog in
Avatar of santuon
santuon

asked on

How to get progress of sql query using ADO.Net

Hello,

Is it possible to use ADO.Net to get the progress (ie % complete) of a sql query?

The query I am running is to backup a database
eg "backup database test to disk = 'D:\mssql\backup\test.bck'

As some backup/restores take considerable time, I wish to display some progress, but don't know how to go about getting sql progress information from the sqlcommand object.

I know its possible to get the progress using other .net libraries such as SMO, but I wish to restrict the usage to only ADO.Net and anything under system.data.

Any help would be much appreciated.

Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

>>Is it possible to use ADO.Net to get the progress (ie % complete) of a sql query?<<
I very much doubt it, but see if using the STATS keyword helps.
If the sql is called from an app, create a separate thread for it and spawn it with sql. Then, have a timer that tciks away and based on that show the progress using a progress bar.

You cal also get more stats based on how many records its updated. See the count of tables and the record, it has to back up and then based on whats been backed up, you can display the percentage.

CT.
Avatar of iboutchkine
iboutchkine

SQL Server: Dataset Fill with ProgressBar

This is a kind of workaround for filling a dataset with a progressbar.

Used is the NorthWind SQL database as sample database, while the sample needs a datagrid and
a progressbar on a form.

Be aware that this sample uses a show in the load event. It is better not to do that. However in this case
to keep the sample simple, because otherwise a button was needed, which could confuse.

--------------------------------------------------------------------------------

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As New SqlClient.SqlConnection("Server=(local);" & _
                     "DataBase=Northwind; Integrated Security=SSPI")
        Dim cmd As New SqlClient.SqlCommand("Select Count(*) from Employees", conn)
        conn.Open()
        ProgressBar1.Maximum = DirectCast(cmd.ExecuteScalar, Integer)
        ProgressBar1.Step = 1
        ProgressBar1.Minimum = 0
        cmd.CommandText = "SELECT * FROM Employees"
        Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
        Dim ds As New DataSet
        Dim dt As DataTable
        Show()
        'Normally is this "not done" in the load event,
        'you will see a strange datagrid
        While rdr.Read
            If dt Is Nothing Then
                dt = New DataTable
                Dim dtschema As DataTable
                dtschema = rdr.GetSchemaTable
                For Each drschema As DataRow In dtschema.Rows
                    dt.Columns.Add(drschema("ColumnName").ToString, _
                    Type.GetType(drschema("DataType").ToString))
                Next
            End If
            ProgressBar1.PerformStep()
            Dim dr As DataRow = dt.NewRow
            Dim tempObject(dt.Columns.Count - 1) As Object
            rdr.GetValues(tempObject) 'did not go in one time
            dr.ItemArray = tempObject
            dt.Rows.Add(dr)
            Threading.Thread.Sleep(500) 'only for showing
        End While
        ds.Tables.Add(dt)
        DataGrid1.DataSource = ds.Tables(0)
        rdr.Close()
        conn.Dispose()
End Sub
Avatar of santuon

ASKER

thanks for the help.

Solutions so far are work arounds. What I am actually looking for is weither ADO.Net has features build in to easily query the percentage complete of an SQL backup/restore. Without the need to guess or estimate table, files size etc.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin