예제 #1
0
        public void SaveBook(Book book)
        {
            DatabasePackage dbPackage = new DatabasePackage(_ConnectionString);

            try
            {
                string sqlQuery = string.Empty;

                if (book.ID == 0) // Add a book
                {
                    sqlQuery = "Insert Into Book (Name) Values ('" + book.Name + "')";
                }
                else // Modify a book
                {
                    sqlQuery = "Update Book Set Name = '" + book.Name + "' Where ID = " + book.ID;
                }

                dbPackage.CommandText = sqlQuery;
                dbPackage.OpenConnection();
                dbPackage.BeginTransaction();

                if (dbPackage.ExecuteNonQuery() == 0)
                {
                    throw new DataAccessException("Unable to save object.", new Exception());
                }

                dbPackage.CommitTransaction();
            }
            catch (DataDuplicityException ex)
            {
                dbPackage.RollbackTransaction();

                if (ex.ErrorMessage.IndexOf("Book_Name_Unique") > 0)
                {
                    throw new DataDuplicityException("Book already exist.", ex);
                }
            }
            catch (DataAccessException ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            catch (Exception ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            finally
            {
                dbPackage.CloseConnection();
            }
        }
예제 #2
0
        public void SaveUser(User user)
        {
            DatabasePackage dbPackage = new DatabasePackage(_ConnectionString);

            try
            {
                string sqlQuery = string.Empty;

                if (user.ID == 0) // Add a user
                {
                    sqlQuery = "Insert Into User (Name, Password) Values ('" + user.Name + "', '" + user.Password + ")";
                }
                else // Modify a user
                {
                    sqlQuery = "Update User Set Name = '" + user.Name + "', Password = '******' Where ID = " + user.ID;
                }

                dbPackage.CommandText = sqlQuery;
                dbPackage.OpenConnection();
                dbPackage.BeginTransaction();

                if (dbPackage.ExecuteNonQuery() == 0)
                {
                    throw new DataAccessException("Unable to save object.", new Exception());
                }

                dbPackage.CommitTransaction();
            }
            catch (DataDuplicityException ex)
            {
                dbPackage.RollbackTransaction();

                if (ex.ErrorMessage.IndexOf("User_Name_Unique") > 0)
                {
                    throw new DataDuplicityException("User already exist.", ex);
                }
            }
            catch (DataAccessException ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            catch (Exception ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            finally
            {
                dbPackage.CloseConnection();
            }
        }
예제 #3
0
        public void DeleteUser(int id)
        {
            DatabasePackage dbPackage = new DatabasePackage(_ConnectionString);

            try
            {
                dbPackage.CommandText = "Delete From User Where ID = " + id;
                dbPackage.OpenConnection();
                dbPackage.BeginTransaction();

                if (dbPackage.ExecuteNonQuery() == 0)
                {
                    throw new DataAccessException("Unable to save object.", new Exception());
                }

                dbPackage.CommitTransaction();
            }
            catch (DataDependencyException ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataDependencyException(ex.ErrorMessage, ex);
            }
            catch (DataAccessException ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            catch (Exception ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            finally
            {
                dbPackage.CloseConnection();
            }
        }
예제 #4
0
        public void ReturnBook(int bookID)
        {
            DatabasePackage dbPackage = new DatabasePackage(_ConnectionString);

            try
            {
                dbPackage.CommandText = "Update Book Set IssueDate = null, MemberID = null Where ID = " + bookID;
                dbPackage.OpenConnection();
                dbPackage.BeginTransaction();

                if (dbPackage.ExecuteNonQuery() == 0)
                {
                    throw new DataAccessException("Unable to save object.", new Exception());
                }

                dbPackage.CommitTransaction();
            }
            catch (DataDuplicityException ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataDuplicityException(ex.ErrorMessage, ex);
            }
            catch (DataAccessException ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            catch (Exception ex)
            {
                dbPackage.RollbackTransaction();
                throw new DataLayerException("Unable to save object.", ex);
            }
            finally
            {
                dbPackage.CloseConnection();
            }
        }