public static IEnumerable <object[]> ExecEnumerable(this ISqlProc proc, IDataMapHelper <object[]> mapper, Action <double> progress = null, Action <SqlField[]> onReadFields = null) { Ai.Guard.Check(proc.Connection != null, "proc.Connection null error in ExecFill"); using (SqlConnection connection = new SqlConnection(proc.ConnectionString())) { if (connection.State != ConnectionState.Open) { connection.Open(); if (connection.State != ConnectionState.Open) { yield break; } } if (connection.Database != proc.DbName) { connection.ChangeDatabase(proc.DbName); } using (var command = proc.CreateCommand()) { command.Connection = connection; if (progress != null) { progress(0.0); } using (SqlDataReader dataReader = command.ExecuteReader()) { if (!dataReader.Read()) { if (progress != null) { progress(1.0); } yield break; } // Task<T> GetFieldValueAsync<T>(int i, CancellationToken cancellationToken); var helper = mapper; // new DbDataMapHelper<T>(); helper.GetProperties(dataReader); if (onReadFields != null) { onReadFields(helper.GetFields(dataReader)); } do { object[] objVal = helper.DbRecordArray(); dataReader.GetValues(objVal); object[] val = helper.SetValues(objVal); yield return(val); }while (dataReader.Read()); if (progress != null) { progress(1.0); } } } } }
// Unsafe public static SqlDataReader ExecMultiReader(this ISqlProc proc, Action <SqlCommand> setup = null, Action <Exception> onError = null, Action <double> progress = null) { var command = proc.CreateCommand(); //if (command.Connection == null || command.Connection.State != ConnectionState.Open) var connStr = proc.ConnectionString(); Guard.Check(!string.IsNullOrWhiteSpace(connStr), "Exec MultiReader connection error"); SqlConnection connection = new SqlConnection(connStr); if (connection.State != ConnectionState.Open) { connection.Open(); if (connection.State != ConnectionState.Open) { return(null); } } if (connection.Database != proc.DbName) { connection.ChangeDatabase(proc.DbName); } command.Connection = connection; if (setup != null) { setup(command); } if (progress != null) { progress(0.0); } SqlDataReader dataReader = null; if (onError != null) { try { dataReader = command.ExecuteReader(behavior: CommandBehavior.CloseConnection); } catch (Exception ex) { onError(ex); } } else { dataReader = command.ExecuteReader(behavior: CommandBehavior.CloseConnection); } if (dataReader != null && dataReader.IsClosed) // !dataReader.Read()) { if (progress != null) { progress(1.0); } return(null); } return(dataReader); }