首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MiniProfiler - ProfiledDbDataAdapter

MiniProfiler - ProfiledDbDataAdapter
EN

Stack Overflow用户
提问于 2014-03-04 12:25:33
回答 1查看 854关注 0票数 1

试图让MiniProfiler配置文件,通过存储的Proc加载DataTable

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
// 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:这起作用是:

代码语言:javascript
复制
// 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

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-06 08:50:51

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

I 修正了这个在MiniProfiler代码中。Fix可在nuget,版本3.0.10-beta7 7和更高版本中使用。

我从上面用您的代码测试了这一点,它对我有用:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22171922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档