使用MiniProfiler直接调用ADO.net

问题描述 投票:3回答:1

对于那些在C#和剖析中极客的人来说,这个问题将是愚蠢的。

我是c#的新手(基本上是一个c ++开发人员)。如果它使用dbproviderfactory,我可以分析数据库查询,但是当它直接使用时我无法分析ado.net调用(原始SqlConnectionSqlCommand)。我遇到了Miniprofiler代码,他们也在其中描述了直接的ADO.net调用。我只是不知道如何使用它(将其集成到我的探查器中)。

我的代码是

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

当执行上面的代码时,将如何调用MiniProfiler中的类?当我的Web应用程序调用SqlCommand(..)时,如何调用这些类(希望类名为SimpleProfiledDbCommand)。

需要更好地澄清这一点。

c# ado.net mvc-mini-profiler
1个回答
2
投票

根据documentation,您需要使用提供的能够跟踪您的查询的SqlConnection包裹您的ProfiledDbConnection

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);
© www.soinside.com 2019 - 2024. All rights reserved.