开发者

How could I instantiate a Profiled DataAdapter to use with MVC MINI PROFILER?

开发者 https://www.devze.com 2023-03-26 01:14 出处:网络
None of the Command objects have Fill methods, but in the former way I was doing I could instantiate a new OracleDataAdapter. How could I instantiate a Profiled DataAdapter to profile my database acti

None of the Command objects have Fill methods, but in the former way I was doing I could instantiate a new OracleDataAdapter. How could I instantiate a Profiled DataAdapter to profile my database activity with MVC MINI PROFILER?

All I need is to use the Fill command with the profiled connection of MVC mini Profiler.

[UPDATE]

I think many must have done that before, unless they are using Entity Framework, which works nice and easy. In my case the query is loaded dynamically into a Datatable, and the entity cannot be mapped, since it is unknown by the application.

开发者_如何学编程

The biggest problem after creating a command by the profiled connection is to set it to a DataAdapter which cannot be instantiated.

[UPDATE] Further References:

  • How to hook up SqlDataAdapter to profile db operations with mvc mini profiler
  • MiniProfiler - ProfiledDbDataAdapter


According to Rory

"There's a class ProfiledDbDataAdapter provided for this that you can use wrapped around your existing SqlDataAdapter."

By this hint, you can write some code like this

public DbConnection _dbConnection;
private DbCommand _dbCommand;
private DbDataAdapter _dbDataAdapter;

public DataSet GetResultByProcWithSingleParam(string procName, SqlParameter sqlParams)
        {
            try
            {
                _dbCommand = _dbConnection.CreateCommand();
                _dbCommand.CommandType = CommandType.StoredProcedure;
                _dbCommand.Parameters.Add(sqlParams);
                _dbCommand.CommandText = procName;
                _dbConnection.Open();
                _dbCommand.ExecuteNonQuery();
                _dbDataAdapter = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateDataAdapter();
                _dbDataAdapter = new ProfiledDbDataAdapter(_dbDataAdapter);
                _dbDataAdapter.SelectCommand = _dbCommand;
                _ds = new DataSet();
                _dbDataAdapter.Fill(_ds);
                _dbConnection.Close();
                return _ds;
            }
            catch (Exception ex)
            {

                throw;
            }

        } 

And namespaces for this code are:

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using StackExchange.Profiling;
using StackExchange.Profiling.Data;

I hope it will work. In my case, it is working successfully.

0

精彩评论

暂无评论...
验证码 换一张
取 消