说我有以下代码:
private AdomdDataReader ExecuteReader(string query)
{
var conn = new AdomdConnection(ConnectionString);
conn.Open();
var cmd = new AdomdCommand(query, conn);
cmd.Parameters.Add("Foo", "Bar");
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
然后在使用中,您将读者包裹在using
声明中:
using (var reader = ExecuteReader("..."))
...
这与每次手动打开连接和执行阅读器形成对比:
using(var conn = new AdomdConnection(ConnectionString))
{
conn.Open();
using(var cmd = new AdomdCommand(query, conn))
{
cmd.Parameters.Add("Foo", "Bar");
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
...
}
}
所以我基本上只是想减少从数据库中读取许多类似查询的样板。我这样做会让自己暴露在资源泄漏中吗?