MiniProfiler-ProfiledDbDataAdapter [英] MiniProfiler - ProfiledDbDataAdapter

查看:124
本文介绍了MiniProfiler-ProfiledDbDataAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让MiniProfiler通过存储的Proc配置文件加载数据表

Trying to get MiniProfiler to profile loading a DataTable via a Stored Proc

// 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上的空引用异常

Problem: Null reference exception on SelectCommand

请忽略使用/处置不足

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

I can successfully profile using ProfiledDbCommand to a 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)
        {
        }
    }
}

这有效:

// 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一样

This doesn't DataTable.. but does with 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);

推荐答案

事实证明,尽管ProfiledDbDataAdapter是从DbDataAdapter继承的,但它没有覆盖DbDataAdapter.Fill(DataTable)的默认功能,从而导致您看到错误

It turns out that though ProfiledDbDataAdapter inherited from DbDataAdapter, it did not override the default functionality of DbDataAdapter.Fill(DataTable), leading to the errors that you saw.

已在MiniProfiler代码中修复了此问题.修复程序可以在nuget,版本3.0.10-beta7 及更高版本中获得.

I fixed this in the MiniProfiler code. Fix is available in nuget, version 3.0.10-beta7 and higher.

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

I have tested this with your code from above and it works for me:

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

这篇关于MiniProfiler-ProfiledDbDataAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆