不必借助监控程序(例如WIndows的性能计数器或SQL Server Profiler),SqlConnection也能获取T SQL的执行状态,结果可用于性能分析。
程序片断如下:
......
sConn.StatisticsEnabled = true;
if (!string.IsNullOrEmpty(iSQL))
{
using (SqlCommand iCmd=new SqlCommand (iSQL,sConn ))
{
iCmd .ExecuteNonQuery ();
}
}
IDictionaryEnumerator iEnum =sConn.RetrieveStatistics().GetEnumerator ();
......
默认情况下,SQlConnection不提供统计信息,要使用此功能需要将StatisticsEnabled 设为true. SqlConnection的 RetrieveStatistics 方法返回在调用该方法时统计数据的键值对集合,可以如代码所示获取其信息察看。
![](https://img2024.cnblogs.com/blog/35695/202407/35695-20240713070336838-1837943664.jpg)