예제 #1
0
        /// <summary>
        /// Loads a data object using a primary key
        /// </summary>
        /// <param name="ID">Primary key value to load.</param>
        public void Load(int ID)
        {
            // Load a dataobject by it's Primary[0] ID

            if (PrimaryKeys.Count == 0)
            {
                DataObjectException ValidationError = new DataObjectException("Only one key was given for a multiple key object.");
                throw ValidationError;
            }

            if (TableName.Trim().Length == 0)
            {
                DataObjectException ValidationError = new DataObjectException("The object's table has not been set.");
                throw ValidationError;
            }

            try
            {
                DataCommand.Connection = ConnHandler.RequestConnection(Alias, this);

                DataCommand.CommandText = "SELECT * FROM " +
                                          TableName + " WHERE " + PrimaryKeys[0].Name +
                                          " = " + ID.ToString();

                DataAdapter.SelectCommand = _dbcmdData;
                int x = (int)DataAdapter.Fill(_dataTable);

                if (x == 0)
                {
                    // Record doesn't exist
                    _dataRow = null;
                    DataObjectException.InvalidRecordException PrimaryKeyError =
                        new DataObjectException.InvalidRecordException();
                    throw PrimaryKeyError;
                }
                else
                {
                    _dataRow = _dataTable.Rows[_dataTable.Rows.Count - 1];
                    if (RecordChanged != null)
                    {
                        RecordChangedEventArgs args = new RecordChangedEventArgs(RowAction.Loaded);
                        RecordChanged(this, args);
                    }
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                ConnHandler.ReleaseConnection(this, DataCommand.Connection);
            }
        }
예제 #2
0
        /// <summary>
        /// Delete the object with this ID from the database.
        /// </summary>
        public void Delete(int ID)
        {
            if (PrimaryKeys.Count > 1)
            {
                DataObjectException ValidationError = new DataObjectException("Only one key was given for a multiple key object.");
                throw ValidationError;
            }

            if (_dataRow != null)
            {
                if (_dataRow[PrimaryKeys[0].Name] is int)
                {
                    if ((int)_dataRow[PrimaryKeys[0].Name] == ID)
                    {
                        Delete();
                        return;
                    }
                }
            }

            try
            {
                string Sql;
                Sql = "DELETE FROM " + TableName + " WHERE " + PrimaryKeys[0].Name + " = " + ID.ToString();

                DataCommand.Connection = ConnHandler.RequestConnection(Alias, this);

                _dbcmdData.CommandText = Sql;
                int x = (int)_dbcmdData.ExecuteNonQuery();
                if (x == 0)
                {
                    // Record doesn't exist
                    _dataRow = null;
                    DataObjectException.InvalidRecordException PrimaryKeyError =
                        new DataObjectException.InvalidRecordException();
                    throw PrimaryKeyError;
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                ConnHandler.ReleaseConnection(this, DataCommand.Connection);
            }
        }
예제 #3
0
        /// <summary>
        /// Loads a data object using multiple primary keys
        /// </summary>
        /// <param name="IDs">Primary key array to load.</param>
        public void Load(int[] IDs)
        {
            // Load a dataobject by all of it's IDs

            if (PrimaryKeys.Count != IDs.Length)
            {
                DataObjectException ValidationError = new DataObjectException("The object's primary keys do match the load values given.");
                throw ValidationError;
            }

            if (TableName.Trim().Length == 0)
            {
                DataObjectException ValidationError = new DataObjectException("The object's table has not been set.");
                throw ValidationError;
            }

            try
            {
                DataCommand.Connection = ConnHandler.RequestConnection(Alias, this);

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

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

                    DataCommand.CommandText += " " + PrimaryKeys[i].Name + " = " + IDs[i].ToString();
                }

                DataAdapter.SelectCommand = _dbcmdData;
                int x = (int)DataAdapter.Fill(_dataTable);

                if (x == 0)
                {
                    // Record doesn't exist
                    _dataRow = null;
                    DataObjectException.InvalidRecordException PrimaryKeyError =
                        new DataObjectException.InvalidRecordException();
                    throw PrimaryKeyError;
                }
                else
                {
                    _dataRow = _dataTable.Rows[_dataTable.Rows.Count - 1];
                    if (RecordChanged != null)
                    {
                        RecordChangedEventArgs args = new RecordChangedEventArgs(RowAction.Loaded);
                        RecordChanged(this, args);
                    }
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                ConnHandler.ReleaseConnection(this, DataCommand.Connection);
            }
        }
예제 #4
0
        /// <summary>
        /// Delete the object with these IDs from the database.
        /// </summary>
        public void Delete(int[] IDs)
        {
            if (PrimaryKeys.Count != IDs.Length)
            {
                DataObjectException ValidationError = new DataObjectException("The object's primary keys do match the ID values given.");
                throw ValidationError;
            }

            if (_dataRow != null)
            {
                for (int i = 0; i < PrimaryKeys.Count; i++)
                {
                    if (_dataRow[PrimaryKeys[i].Name] is int)
                    {
                        if ((int)_dataRow[PrimaryKeys[i].Name] != IDs[i])
                        {
                            break;
                        }
                        else if (i == PrimaryKeys.Count - 1)
                        {
                            // The object trying to be deleted is this object
                            Delete(); // Delete self
                            return;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            try
            {
                string Sql;
                Sql = "DELETE FROM " + TableName + " WHERE";

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

                    Sql += " " + PrimaryKeys[i].Name + " = " + IDs[i].ToString();
                }

                DataCommand.Connection = ConnHandler.RequestConnection(Alias, this);
                _dbcmdData.CommandText = Sql;
                int x = (int)_dbcmdData.ExecuteNonQuery();
                if (x == 0)
                {
                    // Record doesn't exist
                    _dataRow = null;
                    DataObjectException.InvalidRecordException PrimaryKeyError =
                        new DataObjectException.InvalidRecordException();
                    throw PrimaryKeyError;
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                ConnHandler.ReleaseConnection(this, DataCommand.Connection);
            }
        }