コード例 #1
0
        public void AddBook()
        {
            var addbookwindow = new AddBookWindow();

            addbookwindow.DataContext = this;
            var result = addbookwindow.ShowDialog();

            if (result == true)
            {
                if (title == null | category == null | bookid.Count == 0 | bookauthor.Count == 0)
                {
                    MessageBox.Show("incomplete of filling up the information");
                }
                else
                {
                    for (int i = 0; i < bookid.Count; i++)
                    {
                        var newbook = new Book();
                        newbook.BookTitle        = title;
                        newbook.BookCategory     = category;
                        newbook.BookAvailability = Availability.AVAILABLE.ToString();
                        for (int a = 0; a < bookauthor.Count; a++)
                        {
                            newbook.BOOKAUTHOR.Add(bookauthor[a]);
                        }
                        newbook.BookIDNumber = bookid[i];
                        BOOKSLIST.Add(newbook);
                    }
                    MessageBox.Show("successfully added the book");
                }
                title    = null;
                category = null;
                bookid.Clear();
                bookauthor.Clear();
            }
            else
            {
                title    = null;
                category = null;
                bookid.Clear();
                bookauthor.Clear();
            }
        }
コード例 #2
0
        //public Author selectedauthor { get; set; }
        //public ObservableCollection<Author> bookauthor { get; set; } = new ObservableCollection<Author>();

        public void AddBook()
        {
            var addbookwindow = new AddBookWindow();

            addbookwindow.DataContext = this;
            var result = addbookwindow.ShowDialog();

            if (result == true)
            {
                if (title == null | category == null | bookid.Count == 0 | authorfirstname == null | authorlastname == null)
                {
                    MessageBox.Show("incomplete of filling up the information");
                }
                else
                {
                    //for (int i = 0; i < bookid.Count; i++)
                    //{
                    //    var newbook = new BOOK();
                    //    newbook.BookTitle = title;
                    //    newbook.BookCategory = category;
                    //    newbook.BookAvailability = Availability.AVAILABLE.ToString();
                    //    for (int a = 0; a < bookauthor.Count; a++)
                    //    {
                    //        newbook.BOOKAUTHOR.Add(bookauthor[a]);
                    //    }
                    //    newbook.BookIDNumber = bookid[i];
                    //    BOOKSLIST.Add(newbook);
                    //}
                    SqlCommand command_addauthor = new SqlCommand("INSERT INTO [AUTHOR](LastName,FirstName) VALUES(@LastName,@FirstName)", con);
                    //SqlCommand command_author = new SqlCommand("INSERT INTO [AUTHOR](AuthorID,LastName,FirstName) VALUES(@AuhtorID,@LastName,@FirstName)", con);
                    //command_author.Parameters.AddWithValue("@AuhtorID",dt_author.Rows.Count+1);
                    command_addauthor.Parameters.AddWithValue("@LastName", authorlastname);
                    command_addauthor.Parameters.AddWithValue("@FirstName", authorfirstname);
                    con.Open();
                    command_addauthor.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show(dt_author.Rows.Count.ToString());
                    for (int i = 0; i < bookid.Count; i++)
                    {
                        //SqlCommand command_book = new SqlCommand("INSERT INTO [BOOK](BookID,Title,Category,Availability,AuthorID) VALUES(@BookID,@Title,@Category,@Availability,@AuthorID)", con);
                        SqlCommand command_addbook = new SqlCommand("INSERT INTO [BOOK](Title,Category,Availability,AuthorID) VALUES(@Title,@Category,@Availability,@AuthorID)", con);
                        //command_book.Parameters.AddWithValue("@BookID", bookid[i]);
                        command_addbook.Parameters.AddWithValue("@Title", title);
                        command_addbook.Parameters.AddWithValue("@Category", category);
                        command_addbook.Parameters.AddWithValue("@Availability", Availability.AVAILABLE.ToString());
                        command_addbook.Parameters.AddWithValue("@AuthorID", int.Parse(dt_author.Rows[dt_author.Rows.Count - 1]["AuthorID"].ToString()));
                        BOOKSLIST.Add(new BOOK(title, category, int.Parse(dt_author.Rows[dt_author.Rows.Count - 1]["AuthorID"].ToString()), bookid[i], Availability.AVAILABLE.ToString()));
                        con.Open();
                        command_addbook.ExecuteNonQuery();
                        con.Close();
                    }
                    //for(int i = 0;i<bookauthor.Count;i++)
                    //{
                    //    SqlCommand command_author = new SqlCommand()
                    //}
                }
                //title = null;
                //category = null;
                //authorfirstname = null;
                //authorlastname = null;
                //bookid.Clear();
                //bookauthor.Clear();
            }
            //else
            //{
            //    title = null;
            //    category = null;
            //    bookid.Clear();
            //    bookauthor.Clear();
            //}
            title           = null;
            category        = null;
            authorfirstname = null;
            authorlastname  = null;
            bookid.Clear();
        }