【问题标题】:MiniProfiler - ProfiledDbDataAdapterMiniProfiler - ProfiledDbDataAdapter
【发布时间】:2014-03-04 12:25:33
【问题描述】:

试图让 MiniProfiler 通过存储过程分析加载数据表

// 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)
        {
        }
    }
}

编辑1: 这有效:

// 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);

【问题讨论】:

  • 只有在使用存储过程通过SqlDataAdapterDataTable 时才会发生这种情况吗?还是当您尝试通过SqlDataAdapter 运行普通sql 时也会发生这种情况?
  • 使用纯 SQL 无法正常工作——同样的错误。我发现它在使用 DataSet 而不是 DataTable 时确实有效。 stackoverflow.com/questions/10824852/…
  • 在主要问题中添加代码以更新结果
  • 好的,根据您的报告,一切都指向 ProfiledDbConnection 处理 DataAdapter > DataTable 操作的方式中的错误。我会尽可能地研究它,但这需要建立一个解决方案来复制它(这需要更多时间)。

标签: mvc-mini-profiler


【解决方案1】:

事实证明,虽然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

【讨论】:

  • 非常感谢 Yaakov - 非常快的错误修复,现在按预期工作!
  • db参数呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-29
  • 2013-02-11
相关资源
最近更新 更多