private int ExecuteNonQuery(IDbCommand cmd) { int affectedRecords = 0; SetupCmd(cmd); using (cmd) { DataHelper.EnsureConnectionOpen(Connection, () => { affectedRecords = cmd.ExecuteNonQuery(); }); } return(affectedRecords); }
internal T ExecuteCommand <T>(IDbCommand cmd, CommandBehavior cmdBehaviour, Func <IDataReader, T> getResult) { T res = default(T); DataHelper.EnsureConnectionOpen(cmd.Connection, () => { try { using (var rdr = cmd.ExecuteReader(cmdBehaviour)) { res = getResult(rdr); } } catch (Exception ex) { throw new ExecuteDbCommandException(cmd, ex); } }); return(res); }
private int ExecuteNonQuery(IDbCommand cmd) { int affectedRecords = 0; SetupCmd(cmd); using (cmd) { DataHelper.EnsureConnectionOpen(Connection, () => { try { affectedRecords = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw new ExecuteDbCommandException(cmd, ex); } }); } return(affectedRecords); }
/// <summary> /// Executes INSERT statement generated by specified table name and annotated POCO model. /// </summary> /// <param name="tableName">table name</param> /// <param name="pocoModel">POCO model with public properties that match table columns.</param> /// <returns>Number of inserted data records.</returns> public int Insert(string tableName, object pocoModel) { if (pocoModel == null) { throw new ArgumentNullException($"{nameof(pocoModel)}"); } int affected = 0; DataHelper.EnsureConnectionOpen(Connection, () => { affected = InsertInternal(tableName, DataHelper.GetChangeset(pocoModel, null)); var autoIncrementCol = FindAutoIncrementColumn(pocoModel); if (autoIncrementCol != null) { var insertedId = CommandBuilder.DbFactory.GetInsertId(Connection); if (insertedId != null) { autoIncrementCol.SetValue(pocoModel, insertedId); } } }); return(affected); }
internal int Update() { int affected = 0; DataHelper.EnsureConnectionOpen(DbAdapter.Connection, () => { foreach (var row in RS) { if ((row.State & RecordSet.RowState.Added) == RecordSet.RowState.Added) { affected += ExecuteInsertCmd(row); } else if ((row.State & RecordSet.RowState.Deleted) == RecordSet.RowState.Deleted) { affected += ExecuteDeleteCmd(row); } else if ((row.State & RecordSet.RowState.Modified) == RecordSet.RowState.Modified) { affected += ExecuteUpdateCmd(row); } row.AcceptChanges(); } }); return(affected); }