像这样编写这个辅助方法是否安全?它会一直关闭连接吗?我知道如果一切顺利,它会的,但是 ExecuteReader 会关闭连接,即使它抛出?
public static IEnumerable<DbDataRecord> ExecuteSelect(string commandText, DbConnection conn)
{
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = commandText;
conn.Open();
using (DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
foreach (DbDataRecord record in reader) { yield return record; }
}
}
}