【发布时间】:2012-03-01 21:33:06
【问题描述】:
有人在使用 MvcMiniProfiler 分析他们的 Subsonic sql 查询方面取得了成功吗?我似乎无法在 Subsonic 中准确找到挂接到 SqlConnection 创建过程的位置。
【问题讨论】:
标签: subsonic3 mvc-mini-profiler
有人在使用 MvcMiniProfiler 分析他们的 Subsonic sql 查询方面取得了成功吗?我似乎无法在 Subsonic 中准确找到挂接到 SqlConnection 创建过程的位置。
【问题讨论】:
标签: subsonic3 mvc-mini-profiler
我从来没有找到一个非常好的方法来做到这一点,但我确实找到了一些行之有效的方法。我不能继承 SubSonic.DataProviders.DbDataProvider 类,因为构造函数是“内部的”(不是很棒)。所以我只是将源代码复制到我的项目中并进行了一些更改。
需要修改的主要代码行在“CreateConnection”方法中,它需要返回一个ProfiledDbConnection。
public DbConnection CreateConnection(string connectionString)
{
DbConnection conn = Factory.CreateConnection();
conn.ConnectionString = connectionString;
if(conn.State == ConnectionState.Closed) conn.Open();
return conn;
}
由于 Connection 不再是 SqlConnection,所以这在一些现有代码中导致了转换错误。为了解决这些问题,我将“ExecuteDataSet”方法更改为使用范围中的连接而不是工厂。
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”方法以使用范围连接。
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。我在 Context 类的代码生成中将其更改为从 IOC 中提取,而不是使用 ProviderFactory。
希望对其他人有所帮助。
【讨论】: