public bool ReadRows(string szQ, Action <MySqlCommand> initCommand, Action <MySqlDataReader> readRow, ReadRowMode rowMode) { DBHelperCommandArgs args = new DBHelperCommandArgs() { QueryString = szQ }; return(ReadRows(args, initCommand, readRow, rowMode)); }
/// <summary> /// Executes a query /// </summary> /// <param name="args">Query string plus any pre-initialized parameters</param> /// <param name="initCommand">Lambda to add any necessary parameters or otherwise pre-process the command</param> /// <param name="readRow">Lambda to read a row. All rows will be read unless true is returned</param> /// <param name="rowMode">Row mode - read all available rows or just a single row</param> /// <returns>True for success. Sets LastError</returns> public bool ReadRows(DBHelperCommandArgs args, Action <MySqlCommand> initCommand, Action <MySqlDataReader> readRow, ReadRowMode rowMode) { if (args == null) { throw new ArgumentNullException(nameof(args)); } if (readRow == null) { throw new ArgumentNullException(nameof(readRow)); } bool fResult = true; using (MySqlCommand comm = new MySqlCommand()) { InitCommandObject(comm, args); using (comm.Connection = new MySqlConnection(ConnectionString)) { LastError = string.Empty; initCommand?.Invoke(comm); try { comm.Connection.Open(); using (MySqlDataReader dr = comm.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { readRow(dr); if (rowMode == ReadRowMode.SingleRow) { break; } } } } } catch (MySqlException ex) { throw new MyFlightbookException("MySQL error thrown in ReadRows; Query = " + CommandText ?? string.Empty, ex, string.Empty); } catch (MyFlightbookException ex) { LastError = ex.Message; fResult = false; } catch (Exception ex) { MyFlightbookException exNew = new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Uncaught exception in ReadRows:\r\n:{0}", comm.CommandText), ex); MyFlightbookException.NotifyAdminException(exNew); throw exNew; } } } return(fResult); }