public void ExecuteDataReader(string sql, object parameters, Action <DbDataReader> action, int commandTimeout) { using (var connection = CreateConnection()) { using (var cmd = connection.CreateCommand()) { var db = new CommandDatabase(cmd); db.ExecuteDataReader(sql, parameters, action, commandTimeout); } } }
public IEnumerable <T> ExecuteDataReader <T>(string sql, object parameters, Func <DbDataReader, T> action, int commandTimeout) { using (var connection = CreateConnection()) { using (var cmd = connection.CreateCommand()) { var db = new CommandDatabase(cmd); // 这里一定要用yield,这样可以延迟执行,直接用return db.ExecuteDataReader(sql, parameters, action)在执行dr.Read()的时候,cmd对象早就释放掉了 foreach (var r in db.ExecuteDataReader(sql, parameters, action, commandTimeout)) { yield return(r); } } } }