/// <summary> /// This sub queries the data using the given dbkey. /// </summary> /// <param name="DBKeyValue"><see cref=" string"></see> representation of primary key values.</param> /// <exception cref="RowNotFoundException">Thrown when the row could not be found.</exception> public void SelectByDBKey(string DBKeyValue) { string[] strtemp; string[] oldtemp; long i; if (DBKeyValue.Contains(Table.DAS.KeyDelimiter.ToString())) { strtemp = DBKeyValue.Split(Table.DAS.KeyDelimiter); oldtemp = new string[strtemp.Length]; for (i = 0; i < strtemp.Length; i++) { var ColumnValue = new DASColumnValue(this, (DASDataColumn)Table.PrimaryKey[i]); oldtemp[i] = ColumnValue.GetValueAsString(); ColumnValue.SetValueAsString(strtemp[i]); } } else { oldtemp = new string[1]; var ColumnValue = new DASColumnValue(this, (DASDataColumn)Table.PrimaryKey[0]); oldtemp[0] = ColumnValue.GetValueAsString(); ColumnValue.SetValueAsString(DBKeyValue); } if (ExistsInDB()) { if (RowState != DataRowState.Detached && RowState != DataRowState.Added) { AcceptChanges(); } SelectFromDB(); } else { if (DBKeyValue.Contains("|")) { for (i = 0; i < oldtemp.Length; i++) { var ColumnValue = new DASColumnValue(this, (DASDataColumn)Table.PrimaryKey[i]); ColumnValue.SetValueAsString(oldtemp[i]); } } else { var ColumnValue = new DASColumnValue(this, (DASDataColumn)Table.PrimaryKey[0]); ColumnValue.SetValueAsString(oldtemp[0]); } throw new RowNotFoundException(DBKeyValue); } }
/// <summary> /// This function deletes the row in the database. /// </summary> /// <returns>Number of affected rows.</returns> /// <exception cref="NotEditableException">Thrown when the table is not editable.</exception> /// <exception cref="InvalidRowStateException">Thrown when <see cref="DataRow.RowState"></see> is not equal <see cref=" DataRowState.Deleted"></see>.</exception> public virtual int DeleteFromDB() { int ReturnValue; if (!Table.IsEditable) { throw new NotEditableException(); } if (RowState == DataRowState.Deleted) { var tableDAS = Table.DAS; var stringBuilder = new StringBuilder(); long i = 1; foreach (DASDataColumn column in Table.PrimaryKey) { var ColumnValue = new DASColumnValue(this, column); switch (column.DASDataType) { case DASDataColumn.DASDataTypes.DASDATE: tableDAS.AddParameter(string.Format("@PK{0}", i), ColumnValue.DBValue, DAS.ParameterModes.PARM_IN, DAS.ServerTypes.DATE); break; case DASDataColumn.DASDataTypes.DASDOUBLE: case DASDataColumn.DASDataTypes.DASLONG: tableDAS.AddParameter(string.Format("@PK{0}", i), ColumnValue.DBValue, DAS.ParameterModes.PARM_IN, DAS.ServerTypes.NUMBER); break; case DASDataColumn.DASDataTypes.DASSTRING: tableDAS.AddParameter(string.Format("@PK{0}", i), ColumnValue.DBValue, DAS.ParameterModes.PARM_IN, DAS.ServerTypes.STRING); break; default: throw new UnsupportedDataTypeException(column.DASDataType.ToString()); } i++; } try { stringBuilder.AppendFormat("DELETE FROM {0}", Table.DBTableName); i = 1; foreach (DASDataColumn column in Table.PrimaryKey) { if (i == 1) { stringBuilder.Append(" WHERE "); } else { stringBuilder.Append(" AND "); } stringBuilder.AppendFormat("{0} = @PK{1}", column.ColumnName, i++); } ReturnValue = tableDAS.ExecuteSQL(stringBuilder.ToString()); } catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); } finally { for (var k = 1; k <= Table.PrimaryKey.Length; k++) { tableDAS.RemoveParameter(string.Format("@PK{0}", k)); } } } else { throw new InvalidRowStateException(RowState); } if (ReturnValue > 0) { AcceptChanges(); } return(ReturnValue); }
/// <summary> /// This function inserts the row into the database. /// </summary> /// <returns>Number of rows affected.</returns> /// <remarks>Columns with property <see cref="DASDataColumn.IsAutoValue"></see> = <c>True</c> /// must be set to their DASDataColumn.DefaultValue to get the auto value /// by <see cref="InsertIntoDB"></see>.</remarks> /// <exception cref="NotEditableException">Thrown when the table is not editable.</exception> /// <exception cref="UnsupportedDataTypeException">Thrown when an unsupported data type occurs.</exception> /// <exception cref="InvalidRowStateException">Thrown when <see cref="DataRow.RowState"></see> is not equal <see cref=" DataRowState.Added"></see>.</exception> public virtual int InsertIntoDB() { int ReturnValue; if (!(Table.DBTableName.Length > 0)) { throw new NotInsertableException(); } if (RowState != DataRowState.Added) { throw new InvalidRowStateException(RowState); } var stringBuilder = new StringBuilder(); var First = true; var TableDAS = Table.DAS; //check for autovalue columns foreach (DASDataColumn column in Table.Columns) { if (!column.IsAutoValue) { continue; } var curReadOnly = column.ReadOnly; column.ReadOnly = false; var ColumnValue = new DASColumnValue(this, column); if (ColumnValue.Value == column.DefaultValue) { ColumnValue.Value = TableDAS.GetAutoValue(column.AutoValueCreator); } column.ReadOnly = curReadOnly; } //Build SQL statement stringBuilder.AppendFormat("INSERT INTO {0} (", Table.DBTableName); foreach (DASDataColumn column in Table.Columns) { if (First) { First = false; } else { stringBuilder.Append(", "); } stringBuilder.Append(column.DBColumnName); } stringBuilder.Append(") VALUES ("); for (long k = 1; k <= Table.Columns.Count(); k++) { if (k > 1) { stringBuilder.Append(", "); } stringBuilder.AppendFormat("@P{0}", k); } stringBuilder.Append(")"); //Create Parameters long i = 1; foreach (DASDataColumn column in Table.Columns) { var ColumnValue = new DASColumnValue(this, column); switch (column.DASDataType) { case DASDataColumn.DASDataTypes.DASDATE: TableDAS.AddParameter(string.Format("@P{0}", i), ColumnValue.Value, DAS.ParameterModes.PARM_IN, DAS.ServerTypes.DATE); break; case DASDataColumn.DASDataTypes.DASDOUBLE: case DASDataColumn.DASDataTypes.DASLONG: TableDAS.AddParameter(string.Format("@P{0}", i), ColumnValue.Value, DAS.ParameterModes.PARM_IN, DAS.ServerTypes.NUMBER); break; case DASDataColumn.DASDataTypes.DASSTRING: TableDAS.AddParameter(string.Format("@P{0}", i), ColumnValue.Value, DAS.ParameterModes.PARM_IN, DAS.ServerTypes.STRING); break; default: throw new UnsupportedDataTypeException(column.DASDataType.ToString()); } i++; } try { ReturnValue = Table.DAS.ExecuteSQL(stringBuilder.ToString()); } catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); } finally { //Remove Parameters for (long k = 1; k <= Table.Columns.Count(); k++) { TableDAS.RemoveParameter(string.Format("@P{0}", k)); } } if (ReturnValue > 0) { AcceptChanges(); SelectFromDB(); } return(ReturnValue); }