MiniProfiler - ProfiledDbDataAdapter

时间:2014-03-04 12:25:34

标签: mvc-mini-profiler

尝试让MiniProfiler通过存储过程来分析加载DataTable

// Use a DbDataAdapter to return data from a SP using a DataTable
var sqlConnection = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;");
DbConnection connection = new StackExchange.Profiling.Data.ProfiledDbConnection(sqlConnection, MiniProfiler.Current);

var table = new DataTable();
DbDataAdapter dataAdapter = new SqlDataAdapter("GetCountries", sqlConnection);
ProfiledDbDataAdapter prdataAdapter = new ProfiledDbDataAdapter(dataAdapter, null);

// null reference exception here - SelectCommand is null
prdataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

prdataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@countryID", 2));
prdataAdapter.Fill(table);

ViewBag.table = table;

问题:SelectCommand上的空引用异常

请忽略缺少使用/处置

我可以使用ProfiledDbCommand成功分析到SP:

// call a SP from DbCommand
var sqlConnection2 = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;");
DbConnection connection2 = new StackExchange.Profiling.Data.ProfiledDbConnection(sqlConnection2, MiniProfiler.Current);

if (connection2 != null)
{
    using (connection2)
    {
        try
        {
            // Create the command.
            DbCommand command = new SqlCommand();
            ProfiledDbCommand prcommand = new ProfiledDbCommand(command, connection2, null);
            prcommand.CommandType = CommandType.StoredProcedure;
            prcommand.CommandText = "dbo.GetCountries";
            prcommand.Parameters.Add(new SqlParameter("@countryID", 3));
            prcommand.Connection = connection2;

            //command.CommandText = "SELECT Name FROM Countries";
            //command.CommandType = CommandType.Text;

            // Open the connection.
            connection2.Open();

            // Retrieve the data.
            DbDataReader reader = prcommand.ExecuteReader();
            while (reader.Read())
            {
                var text = reader["Name"];
                result += text + ", ";
            }
        }

        catch (Exception ex)
        {
        }
    }
}

EDIT1: 这有效:

// 2) SqlConnection, SqlDataAdapter.. dataAdapter command - works
var sqlConnection = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;");
var table = new DataTable();
//var dataAdapter = new SqlDataAdapter("GetCountries", sqlConnection);
var dataAdapter = new SqlDataAdapter("select * from countries", sqlConnection);
//dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
//dataAdapter.SelectCommand.Parameters.AddWithValue("@countryID", 2);
dataAdapter.Fill(table);

这不是DataTable ..但是使用DataSet

var sqlConnection = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;");
DbConnection connection = new ProfiledDbConnection(sqlConnection, MiniProfiler.Current);

DbDataAdapter dataAdapter = new SqlDataAdapter("select * from countries", sqlConnection);
ProfiledDbDataAdapter prdataAdapter = new ProfiledDbDataAdapter(dataAdapter, null);

var table = new DataTable();
var dataset = new DataSet();

// Doesn't work
//prdataAdapter.Fill(table);
// Does work
prdataAdapter.Fill(dataset);

1 个答案:

答案 0 :(得分:3)

事实证明虽然ProfiledDbDataAdapter继承自DbDataAdapter,但它没有覆盖DbDataAdapter.Fill(DataTable)的默认功能,导致您看到的错误。

MiniProfiler代码中的fixed this。修复版可在nuget,version 3.0.10-beta7及更高版本中使用。

我已经使用上面的代码对此进行了测试,它对我有用:

DbConnection connection = 
             new ProfiledDbConnection(sqlConnection, MiniProfiler.Current);    
var sql = "select * from countries";
DbDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlConnection);
ProfiledDbDataAdapter prdataAdapter = new ProfiledDbDataAdapter(dataAdapter);    
var table = new DataTable();
dataAdapter.Fill(table);            // this now works