Esempio n. 1
0
        /// <summary>
        /// adds the book object to database
        /// </summary>
        public void Add()
        {
            if (BookAlreadyExistsDisabled())
            {
                this.Activate();
                this.Update();
                return;
            }
            CustomSqlConnection con = new CustomSqlConnection();

            if (con.ConnectError())
            {
                return;
            }
            string command = "INSERT INTO [dbo].[t_s_buecher] (buch_isbn, buch_titel, buch_genre_id, " +
                             "buch_verlag_id, buch_erscheinungsdatum, buch_sprache_id, buch_auflage, buch_neupreis, " +
                             "buch_image, buch_activated, buch_anzahl) VALUES " +
                             "(@bookIsbn, @bookTitle, @bookGenreId, @bookPublisherId, @bookDatePublication, @bookLanguageId, @bookEdition, " +
                             "@bookPrice, @bookImage, 1, 0)";
            SqlCommand cmd = new SqlCommand(command, con.Con);

            cmd.Parameters.AddWithValue("@bookIsbn", BookIsbn);
            cmd.Parameters.AddWithValue("@bookTitle", BookTitle);
            cmd.Parameters.AddWithValue("@bookGenreId", BookGenre.GenreId);
            cmd.Parameters.AddWithValue("@bookPublisherId", BookPublisher.PublisherId);
            cmd.Parameters.AddWithValue("@bookLanguageId", BookLanguage.LanguageId);
            cmd.Parameters.AddWithValue("@bookDatePublication", BookDatePublication);
            if (BookEdition == null)
            {
                cmd.Parameters.AddWithValue("@bookEdition", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@bookEdition", BookEdition);
            }
            cmd.Parameters.AddWithValue("@bookPrice", BookPrice);
            cmd.Parameters.Add("@bookImage", SqlDbType.VarBinary, -1);
            if (BookImage == null)
            {
                cmd.Parameters["@bookImage"].Value = DBNull.Value;
            }
            else
            {
                using (var ms = new MemoryStream())
                {
                    BookImage.Save(ms, ImageFormat.Png);
                    cmd.Parameters["@bookImage"].Value = ms.ToArray();
                }
            }
            cmd.ExecuteNonQuery();
            AddBookAuthors();
            con.Close();
        }
Esempio n. 2
0
        /// <summary>
        /// updates the book
        /// </summary>
        public void Update()
        {
            CustomSqlConnection con = new CustomSqlConnection();

            if (con.ConnectError())
            {
                return;
            }
            DeleteBookAuthors();
            string command = "UPDATE [dbo].[t_s_buecher] set buch_titel = @bookTitle , buch_genre_id = @bookGenreId, " +
                             "buch_sprache_id = @bookLanguageId, buch_verlag_id = @bookPublisherId, buch_auflage = @bookEdition, " +
                             "buch_erscheinungsdatum = @bookDatePublication, buch_neupreis = @bookPrice, " +
                             "buch_image = @bookImage WHERE buch_isbn = @bookIsbn";
            SqlCommand cmd = new SqlCommand(command, con.Con);

            cmd.Parameters.AddWithValue("@bookTitle", BookTitle);
            cmd.Parameters.AddWithValue("@bookGenreId", BookGenre.GenreId);
            cmd.Parameters.AddWithValue("@bookLanguageId", BookLanguage.LanguageId);
            cmd.Parameters.AddWithValue("@bookPublisherId", BookPublisher.PublisherId);
            if (BookEdition == null || BookEdition.Equals(""))
            {
                cmd.Parameters.AddWithValue("@bookEdition", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@bookEdition", BookEdition);
            }
            cmd.Parameters.AddWithValue("@bookDatePublication", BookDatePublication);

            cmd.Parameters.AddWithValue("@bookPrice", BookPrice);
            cmd.Parameters.Add("@bookImage", SqlDbType.VarBinary, -1);
            if (BookImage == null)
            {
                cmd.Parameters["@bookImage"].Value = DBNull.Value;
            }
            else
            {
                using (var ms = new MemoryStream())
                {
                    BookImage.Save(ms, ImageFormat.Png);
                    cmd.Parameters["@bookImage"].Value = ms.ToArray();
                }
            }
            cmd.Parameters.AddWithValue("@bookIsbn", BookIsbn);
            cmd.ExecuteNonQuery();
            AddBookAuthors();
            con.Close();
        }