2015-01-21 21 views
3

對於那些在C#和分析器中成爲極客的人來說,這個問題很愚蠢。使用MiniProfiler進行直接的ADO.net調用

我是新來的c#(基本上是一個c + +開發人員)。如果它使用dbproviderfactory,我可以對數據庫查詢進行概要分析,但我無法在直接使用ado.net調用時對其進行概要分析(原始的SqlConnection & SqlCommand)。我遇到了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(..)時調用這些類(希望類名是0​​)?

需要對此做出更好的說明。

回答

2

按照documentation,您需要提供ProfiledDbConnection即能跟蹤你的查詢來包裝你SqlConnection

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); 
+0

感謝haim770!所以這不能在運行時完成..儀式? – 2015-01-21 09:47:05

+1

我對MiniProfiler不太瞭解,但我相信。 – haim770 2015-01-21 09:50:05