Example #1
0
        /// <summary>
        /// Gets the author with the specified Aid. If there is no author
        /// with the specified Aid then author is null.
        /// </summary>
        /// <param name="author"></param>
        /// <param name="Aid"></param>
        /// <returns></returns>
        static public bool GetAuthor(out Author author, int Aid)
        {
            author = null;
            SqlCommand command = new SqlCommand("SELECT * from AUTHOR WHERE Aid = @Aid");
            command.Parameters.AddWithValue("@Aid", Aid);

            List<Author> authorList;
            bool result = GetAuthors(out authorList, command);

            if (result && authorList.Count > 0)
            {
                author = authorList[0];
            }

            return result;
        }
Example #2
0
        /// <summary>
        /// Inserts a new author into the database. Returns false if it fails.
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        static public bool Insert(Author author)
        {
            try
            {
                using (SqlConnection connection = HelperFunctions.GetConnection())
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand("INSERT INTO AUTHOR (FirstName, LastName, BirthYear) VALUES (@FirstName, @LastName, @BirthYear)"))
                    {
                        command.Connection = connection;
                        command.Parameters.AddWithValue("@FirstName", HelperFunctions.ValueOrDBNull(author.FirstName));
                        command.Parameters.AddWithValue("@LastName", HelperFunctions.ValueOrDBNull(author.LastName));
                        command.Parameters.AddWithValue("@BirthYear", HelperFunctions.ValueOrDBNull(author.BirthYear));

                        if (command.ExecuteNonQuery() != 1)
                        {
                            return false;
                        }
                    }
                }
            }
            catch(Exception)
            {
                return false;
            }

            return true;
        }
Example #3
0
        /// <summary>
        /// Updates the author in the database. Returns false if it fails.
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        static public bool Update(Author author)
        {
            try
            {
                using (SqlConnection connection = HelperFunctions.GetConnection())
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand("UPDATE AUTHOR SET FirstName=@FirstName, LastName=@LastName, BirthYear=@BirthYear WHERE Aid=@Aid"))
                    {
                        command.Connection = connection;
                        command.Parameters.AddWithValue("@Aid", author.Aid);
                        command.Parameters.AddWithValue("@FirstName", HelperFunctions.ValueOrDBNull(author.FirstName));
                        command.Parameters.AddWithValue("@LastName", HelperFunctions.ValueOrDBNull(author.LastName));
                        command.Parameters.AddWithValue("@BirthYear", HelperFunctions.ValueOrDBNull(author.BirthYear));

                        if (command.ExecuteNonQuery() != 1)
                        {
                            return false;
                        }
                    }
                }
            }
            catch(Exception)
            {
                return false;
            }

            return true;
        }