public bool Load(string conjuction) { bool loaded = false; DataTable dt = null; try { if ((_aggregateParameters == null || _aggregateParameters.Count <= 0) && _resultColumns.Length <= 0 && _countAll == false) { this._entity._canSave = true; } this._entity.OnQueryLoad(conjuction); IDbCommand cmd = _Load(conjuction); _lastQuery = cmd.CommandText; IDbDataAdapter da = this._entity.CreateIDbDataAdapter(); da.SelectCommand = cmd; TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); dt = new DataTable(_entity.MappingName); txMgr.Enlist(cmd, _entity); DbDataAdapter dbDataAdapter = this._entity.ConvertIDbDataAdapter(da); dbDataAdapter.Fill(dt); txMgr.DeEnlist(cmd, _entity); } catch (Exception ex) { throw ex; } finally { this._entity.DataTable = dt; loaded = (dt.Rows.Count > 0); } return(loaded); }
protected void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs e) { try { if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert) { TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY"); txMgr.Enlist(cmd, this); object o = cmd.ExecuteScalar(); txMgr.DeEnlist(cmd, this); if (o != null) { e.Row[this.GetAutoKeyColumn()] = o; e.Row.AcceptChanges(); } } } catch {} }
protected bool LoadFromRawSql(string rawSql, params object[] parameters) { bool loaded = false; DataTable dt = null; try { IDbCommand cmd = _LoadFromRawSql(rawSql, parameters); IDbDataAdapter da = this.CreateIDbDataAdapter(); da.SelectCommand = cmd; TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); dt = new DataTable(this.MappingName); txMgr.Enlist(cmd, this); DbDataAdapter dbDataAdapter = this.ConvertIDbDataAdapter(da); dbDataAdapter.Fill(dt); txMgr.DeEnlist(cmd, this); } catch (Exception ex) { throw ex; } finally { this.DataTable = dt; loaded = (dt.Rows.Count > 0); } return(loaded); }
protected void OnRowUpdated(object sender, MySqlRowUpdatedEventArgs e) { try { if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert) { TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); string identityCol = this.GetAutoKeyColumns(); MySqlCommand cmd = new MySqlCommand(); cmd.CommandText = "SELECT LAST_INSERT_ID();"; txMgr.Enlist(cmd, this); object o = cmd.ExecuteScalar(); txMgr.DeEnlist(cmd, this); if (o != null) { e.Row[identityCol] = o; } e.Row.AcceptChanges(); } } catch {} }
protected object LoadFromSqlScalar(string sp, ListDictionary Parameters, CommandType commandType, int commandTimeout) { object retValue = 0; try { IDbCommand cmd = this.CreateIDbCommand(); cmd.CommandText = sp; cmd.CommandType = commandType; if (commandTimeout > 0) { cmd.CommandTimeout = commandTimeout; } IDataParameter p; if (Parameters != null) { foreach (DictionaryEntry param in Parameters) { p = param.Key as IDataParameter; if (null == p) { p = this.CreateIDataParameter((string)param.Key, param.Value); } else { p.Value = param.Value; } cmd.Parameters.Add(p); } } TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); txMgr.Enlist(cmd, this); retValue = cmd.ExecuteScalar(); txMgr.DeEnlist(cmd, this); } catch (Exception ex) { throw ex; } return(retValue); }
protected bool LoadFromSql(string sp, ListDictionary Parameters, CommandType commandType) { DataTable dataTable = null; bool loaded = false; try { dataTable = new DataTable(this.MappingName); IDbCommand cmd = this.CreateIDbCommand(); cmd.CommandText = sp; cmd.CommandType = commandType; IDataParameter p; if (Parameters != null) { foreach (DictionaryEntry param in Parameters) { p = param.Key as IDataParameter; if (null == p) { p = this.CreateIDataParameter((string)param.Key, param.Value); } else { p.Value = param.Value; } cmd.Parameters.Add(p); } } IDbDataAdapter da = this.CreateIDbDataAdapter(); da.SelectCommand = cmd; TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); txMgr.Enlist(cmd, this); DbDataAdapter dba = ConvertIDbDataAdapter(da); dba.Fill(dataTable); txMgr.DeEnlist(cmd, this); } catch (Exception ex) { throw ex; } finally { this.DataTable = dataTable; loaded = (this.RowCount > 0); } return(loaded); }
virtual public void Save() { if (_dataTable == null) { return; } if (!_canSave) { throw new Exception("Cannot call Save() after Query.AddResultColumn()"); } TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr(); try { bool needToInsert = false; bool needToUpdate = false; bool needToDelete = false; DataRow row; DataRowCollection rows = _dataTable.Rows; for (int i = 0; i < rows.Count; i++) { row = rows[i]; switch (row.RowState) { case DataRowState.Added: needToInsert = true; break; case DataRowState.Modified: needToUpdate = true; break; case DataRowState.Deleted: needToDelete = true; break; } } if (needToInsert || needToUpdate || needToDelete) { IDbDataAdapter da = this.CreateIDbDataAdapter(); if (needToInsert) { da.InsertCommand = GetInsertCommand(); } if (needToUpdate) { da.UpdateCommand = GetUpdateCommand(); } if (needToDelete) { da.DeleteCommand = GetDeleteCommand(); } txMgr.BeginTransaction(); int txCount = txMgr.NestingCount; if (txCount > 1) { txMgr.AddBusinessEntity(this); } if (needToInsert) { txMgr.Enlist(da.InsertCommand, this); } if (needToUpdate) { txMgr.Enlist(da.UpdateCommand, this); } if (needToDelete) { txMgr.Enlist(da.DeleteCommand, this); } DbDataAdapter dbDataAdapter = ConvertIDbDataAdapter(da); this.HookupRowUpdateEvents(dbDataAdapter); dbDataAdapter.Update(_dataTable); txMgr.CommitTransaction(); if (txCount == 1) { this.AcceptChanges(); } if (needToInsert) { txMgr.DeEnlist(da.InsertCommand, this); } if (needToUpdate) { txMgr.DeEnlist(da.UpdateCommand, this); } if (needToDelete) { txMgr.DeEnlist(da.DeleteCommand, this); } } } catch (Exception ex) { if (txMgr != null) { txMgr.RollbackTransaction(); } throw ex; } }