Capturing the ADO.net DB queries run-time

427 Views Asked by At

I am trying to capture the Db query for this below piece of C# .net Web application.

There are way to do if i use DbProviderfactory by making a proxy of it. But instead of using a DbProviderFactory if i use SqlConnection and SqlCommand directly like below , how can i trace my DB calls ?

            SqlConnection myConn = new SqlConnection(@"Server=192.168.23.99;Initial Catalog=cat1;User ID=user21;Password=userpwd");
            SqlCommand myCommand = new SqlCommand("select * from table1", myConn);
            SqlDataReader dataReader;

            System.Threading.Thread.Sleep(5000);
            try
            {
                myConn.Open();
                dataReader = myCommand.ExecuteReader();

                GridView1.DataSource = dataReader;
                GridView1.DataBind();
                dataReader.Close();
                myCommand.Dispose();
                myConn.Close();
            }
            catch (System.Exception ex)
            {
                Response.Write(ex.ToString());
            }

I am aware that there are profilers to trace this. But , I want to do this in my program of my own.

Basically i need to profile the Db Connection and Db commands in a web request, Which does not use DbProviderFactories in it. I am new in developing profilers for .Net , so i expect some guidelines to do this.

Thanks.

1

There are 1 best solutions below

0
On

Easy, just run SQL Profiler, and when you step over the myCommand.ExecuteReader() line, watch what SQL gets run in the profiler.

For more info, see: https://msdn.microsoft.com/en-us/library/ff650699.aspx