Using MiniProfiler for direct ADO.net calls

谁都会走 提交于 2019-12-23 19:05:09

问题


This question will be silly for those who are geeks in C# and profilers.

I am new to c# (basically a c++ developer) . I can profile the database queries if it use dbproviderfactory , but i cannot profile the ado.net calls when it is used directly(raw SqlConnection & SqlCommand). I came across the Miniprofiler code where they also profile direct ADO.net calls. I just dont know how to use it ( integrate it in my profiler ).

My code is

        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());
        }

When the above code gets executed , how will the classes in MiniProfiler will be called? How to make those classes(hope the class name is SimpleProfiledDbCommand) called when my web application calls SqlCommand(..).?

Need a better clarification on this.


回答1:


According to the documentation, you need to wrap your SqlConnection with the provided ProfiledDbConnection that is able to track your queries:

SqlConnection underlyingConn = new SqlConnection(@"Server=192.168.23.99;Initial Catalog=cat1;User ID=user21;Password=userpwd");

SqlConnection myConn = new StackExchange.Profiling.Data.ProfiledDbConnection(underlyingConn, MiniProfiler.Current);


来源:https://stackoverflow.com/questions/28063601/using-miniprofiler-for-direct-ado-net-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!