Ahe*_*eho 7 sql-server asp.net diagnostics glimpse
我已经下载了Glimpse和Glimpse.ADO扩展,并将其安装在我的测试实例上.
我以为我会捕获已执行的任何sql,但似乎它不会以我们的代码编写方式捕获命令.
using (var conn = new SqlConnection(cString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;
txtResult2.Text = cmd.ExecuteScalar().ToString();
conn.Close();
}
Run Code Online (Sandbox Code Playgroud)
我可以通过如下所示的sql代码从测试页面提供信息:
var factory =DbProviderFactories.GetFactory(cString.ProviderName);
using (var connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString.ConnectionString;
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT COUNT(*) FROM table";
command.CommandType = CommandType.Text;
txtResult1.Text = command.ExecuteScalar().ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我只能使用此dbProviderFactories方法捕获数据,那么我的代码中有太多地方需要更改.
有没有办法让Glimpse.ADO使用System.Data.SqlClient.SqlConnection类?是否有另一个Glimpse扩展适用于此命名空间?
有没有其他方法可以解决这个问题?
我同意@StriplingWarrior,利用提供商工厂将使您的代码更加干燥并遵循最佳实践.DbProviderFactories真的是最好的方法,你的代码不会明确地依赖于Glimpse.
但是,如果您真的想继续使用现有的应用代码,Glimpse将通过以下更改为您提供支持:
using (var conn = new GlimpseDbConnection(new SqlConnection(cString))
{
conn.Open();
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;
txtResult2.Text = cmd.ExecuteScalar().ToString();
conn.Close();
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,使用该CreateCommand()方法创建命令,从而无需关联命令和连接.
或者,您仍然可以显式创建命令,如下所示:
conn.Open();
DbCommand cmd = new GlimpseDbCommand(new SqlCommand());
cmd.Connection = conn;
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;
Run Code Online (Sandbox Code Playgroud)
最后,?当您选择了选项卡时,单击Glimpse UI中的图标,或者访问getGlimpse.com上的SQL文档,可以获得有关SQL选项卡的更多文档.(我将把这些信息添加到该页面以供将来参考.)