예제 #1
0
        private void ValidateSave()
        {
            try
            {
                if (_dataTable == null)
                {
                    DataObjectException ValidationError = new DataObjectException("The object's data table has not been set.");
                    throw ValidationError;
                }

                for (int i = 0; i < PrimaryKeys.Count; i++)
                {
                    if (_dataTable.Columns[PrimaryKeys[i].Name] == null)
                    {
                        DataObjectException ValidationError = new DataObjectException("The primary key field does not exist in the datatable.");
                        throw ValidationError;
                    }
                }

                if (_dataRow == null)
                {
                    DataObjectException.NoCurrentRecordException ValidationError = new DataObjectException.NoCurrentRecordException();
                    throw ValidationError;
                }
            }
            catch (DataObjectException err)
            {
                throw err;
            }
            catch (Exception err)
            {
                throw err;
            }
        }
예제 #2
0
        /// <summary>
        /// Reloads the data for this object from the database.
        /// </summary>
        public virtual void Refresh()
        {
            try
            {
                DataCommand.Connection = this.ConnHandler.RequestConnection(Alias, this);

                int GetID = -1;

                if (_dataRow == null)
                {
                    // No current record
                    DataObjectException.NoCurrentRecordException NoRecordError =
                        new DataObjectException.NoCurrentRecordException();
                    throw NoRecordError;
                }

                DataCommand.CommandText = "SELECT * FROM " +
                                          TableName + " WHERE";

                for (int i = 0; i < PrimaryKeys.Count; i++)
                {
                    if (i > 0)
                    {
                        DataCommand.CommandText += " AND";
                    }

                    if (_dataTable.Columns.Count > 0)
                    {
                        if (_dataRow[PrimaryKeys[i].Name] is int)
                        {
                            GetID = (int)_dataRow[PrimaryKeys[i].Name];
                        }
                        else
                        {
                            GetID = -1;
                        }
                        DataCommand.CommandText += " " + PrimaryKeys[i].Name + " = " + GetID.ToString();
                    }
                    else
                    {
                        // This command will only return the table's structure, no actual records
                        DataCommand.CommandText += " " + PrimaryKeys[i].Name + " = -1";
                    }
                }


                if ((int)DataAdapter.Fill(_dataTable) > 0)
                {
                    // Existing record
                    _dataRow = _dataTable.Rows[_dataTable.Rows.Count - 1];
                }
                else
                {
                    // New record
                    _dataRow = _dataTable.NewRow();
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                ConnHandler.ReleaseConnection(this, DataCommand.Connection);
            }
        }
예제 #3
0
        /// <summary>
        /// Deletes this record from the database.
        /// </summary>
        public virtual void Delete()
        {
            try
            {
                // Ensure the object is ready to be deleted
                ValidateSave();

                string Sql;

                if (_dataRow == null)
                {
                    // No current row
                    DataObjectException.NoCurrentRecordException NoRecordError =
                        new DataObjectException.NoCurrentRecordException();
                    throw NoRecordError;
                }

                if (_dataRow.RowState == DataRowState.Detached)
                {
                    _dataRow = null;
                    return;
                }
                else
                {
                    Sql = "DELETE FROM " + TableName + " WHERE";

                    for (int i = 0; i < PrimaryKeys.Count; i++)
                    {
                        if (i > 0)
                        {
                            Sql += " AND";
                        }

                        if (_dataRow[PrimaryKeys[i].Name].ToString() != String.Empty)
                        {
                            Sql += " " + PrimaryKeys[i].Name + " = " + _dataRow[PrimaryKeys[i].Name].ToString();
                        }
                        else
                        {
                            DataObjectException PrimaryKeyError = new DataObjectException("One or more required fields do not have valid values.");
                            throw PrimaryKeyError;
                        }
                    }

                    DataCommand.Connection = ConnHandler.RequestConnection(Alias, this);
                    _dbcmdData.CommandText = Sql;
                    _dbcmdData.ExecuteNonQuery();
                    _dataRow = null;
                }
                if (RecordChanged != null)
                {
                    RecordChangedEventArgs args = new RecordChangedEventArgs(RowAction.Deleted);
                    RecordChanged(this, args);
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                ConnHandler.ReleaseConnection(this, DataCommand.Connection);
            }
        }