MiniProfiler - ProfiledDbDataAdapter

Попытка заставить 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)

Ваш ответ на вопрос