首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用MvcMiniProfiler分析亚音速SQL

用MvcMiniProfiler分析亚音速SQL
EN

Stack Overflow用户
提问于 2012-03-01 21:33:06
回答 1查看 160关注 0票数 3

有人成功地用MvcMiniProfiler分析了他们的亚音速sql查询吗?我似乎无法在Subsonic中找到与SqlConnection创建过程挂钩的确切位置。

  • http://code.google.com/p/mvc-mini-profiler/#Database_profiling
  • http://subsonicproject.com/
EN

回答 1

Stack Overflow用户

发布于 2012-04-10 14:48:44

我从来没有找到一个很好的方法来做这件事,但我确实找到了一些有用的东西。我无法子类SubSonic.DataProviders.DbDataProvider类,因为构造函数是“内部的”(不是很棒)。因此,我只是将源代码复制到我的项目中,并做了一些更改。

必须更改的主要代码行是在"CreateConnection“方法中,它需要返回一个ProfiledDbConnection。

代码语言:javascript
复制
public DbConnection CreateConnection(string connectionString)
        {
            DbConnection conn = Factory.CreateConnection();
            conn.ConnectionString = connectionString;
            if(conn.State == ConnectionState.Closed) conn.Open();
            return conn;
        }

由于连接不再是SqlConnection,所以这在一些现有代码中造成了转换错误。为了解决这些问题,我更改了"ExecuteDataSet“方法,以使用来自作用域的连接,而不是工厂。

代码语言:javascript
复制
public DataSet ExecuteDataSet(QueryCommand qry)
{
            if (Log != null)
                Log.WriteLine(qry.CommandSql);
#if DEBUG
            //Console.Error.WriteLine("ExecuteDataSet(QueryCommand): {0}.", qry.CommandSql);
#endif
            using (AutomaticConnectionScope scope = new AutomaticConnectionScope(this))
            {
                DbCommand cmd = scope.Connection.CreateCommand();
                cmd.CommandText = qry.CommandSql;
                cmd.CommandType = qry.CommandType;
                DataSet ds = new DataSet();
                cmd.Connection = scope.Connection;
                AddParams(cmd, qry);
                DbDataAdapter da = Factory.CreateDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);

                return ds;
            }
        }

我还更改了"ExecuteScalar“方法以使用范围连接。

代码语言:javascript
复制
public object ExecuteScalar(QueryCommand qry)
        {
            if (Log != null)
                Log.WriteLine(qry.CommandSql);

#if DEBUG
            //Console.Error.WriteLine("ExecuteScalar(QueryCommand): {0}.", qry.CommandSql);
            //foreach (var param in qry.Parameters) {
            //    if(param.ParameterValue==null)
            //        Console.Error.WriteLine(param.ParameterName + " = NULL");
            //    else
            //        Console.Error.WriteLine(param.ParameterName + " = " + param.ParameterValue.ToString());
            //}
#endif

            object result;
            using (AutomaticConnectionScope automaticConnectionScope = new AutomaticConnectionScope(this))
            {
                DbCommand cmd = automaticConnectionScope.Connection.CreateCommand();
                cmd.Connection = automaticConnectionScope.Connection;
                cmd.CommandType = qry.CommandType;
                cmd.CommandText = qry.CommandSql;
                AddParams(cmd, qry);
                result = cmd.ExecuteScalar();
            }

            return result;
        }

现在一切似乎都正常了。我目前正在使用IOC来确定数据库类本身是使用这个ProfilingDbDataProvider还是使用现有的DbDataProvider。我在上下文类的代码生成中将此更改为从IOC中提取,而不是使用ProviderFactory。

希望这能帮到别人。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9524453

复制
相关文章

相似问题

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