Search code examples
sql-serverado.netmvc-mini-profiler

Using mvc-mini-profiler with ADO.NET SqlConnection


I'm attempting to utilize the (awesome) mvc-mini-profiler with some pre-existing SqlConnection stored procedure code (we don't use EF or L2S, just ADO.NET to SQL Server 2008). I'm looking for some guidance on how to integrate the inherited ProfiledDb types into this kind of code.

var con = new SqlConnection("connectionstring");  
var cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.CommandText = "SP_STORED_PROCEDURE_NAME";
cmd.Paramters.Add("recordsetid",SqlDbType.UniqueIdentifier).Value = recordsetid;
var dSet = new DataSet();
var da = new SqlDataAdapter(cmd);
da.fill(dSet);
<parse DataSet>

Any help here for us legacy ADO.NET users would be great because on the surface it appears that the SQL profiler should be applicable to this situation


Solution

  • You will need to do is wrap up your connection and use the DbConnection CreateCommand factory.

    Similarly to pass params you will need to use the base interface methods, and avoid stuff like SqlParameter cause it is not wrapped.

    So:

    var cnn = MvcMiniProfiler.Data.ProfiledDbConnection.Get(new SqlConnection(str));
    var cmd = cnn.CreateCommand();
    var param = cmd.CreateParameter(); 
    ...
    

    I have not tested DataSets and DataAdapters, honestly I use Dapper for this kind of stuff these days as it is much less verbose. If it plays up, be sure to report on Google code.