我正在使用 Essential.Logging 的 SQL 记录器。我在这里查看了库的来源并看到了(在最后)
每次要登录到 SQL 时:
using (var connection = DbProviderFactoryExtensions.CreateConnection(dbFactory, connectionSettings.ConnectionString))
{
using (var command = DbProviderFactoryExtensions.CreateCommand(dbFactory, CommandText, connection))
{
command.Parameters.Add(DbProviderFactoryExtensions.CreateParameter(dbFactory, "@ApplicationName", ApplicationName != null ? (object)ApplicationName : DBNull.Value));
// ..snip..
command.Parameters.Add(DbProviderFactoryExtensions.CreateParameter(dbFactory, "@Data", dataString != null ? (object)dataString : DBNull.Value));
connection.Open(); // <====
command.ExecuteNonQuery();
}
}
using 语句顶部的辅助函数是
public static DbConnection CreateConnection(DbProviderFactory dbFactory, string connectionString)
{
if (dbFactory == null) throw new ArgumentNullException("dbFactory");
var connection = dbFactory.CreateConnection();
connection.ConnectionString = connectionString;
return connection;
}
问题:
- 是否每次都打开(和关闭)与 SQL 服务器的连接?我认为汇集到 SQL 服务器的连接会更有效,对吧?
- 如果我将其替换为 Entity Framework 5.0(带有 a
using (var db = new myDbContext()) { }
)会导致连接池吗?