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

无人久伴 提交于 2019-12-02 02:31:59
mayank

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!