BackTerminal.frmBookNew.buttonOK_Click C# (CSharp) Method

buttonOK_Click() private method

private buttonOK_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!TreeViewSelected()) return;
            string categoryTitle = tvCategory.SelectedNode.Text;

            // 获取各控件的值
            string title = textBoxTitle.Text;
            string author = textBoxAuthor.Text;
            string publisher = textBoxPublisher.Text;
            string ISBN = textBoxISBN.Text;
            int quantity = (int)numericUpDown.Value;
            string libraryName = (string)comboBoxLibrary.SelectedItem;
            string description = textBoxDescription.Text;

            // 检查各控件的值
            if (!(CheckString(title, 20) && CheckString(author, 50) && CheckString(publisher, 50)
                && CheckString(ISBN, 20) && (CheckString(description, 100) || description.Length == 0)))
            {
                ErrorMessage("您输入的字符串过长或为空");
                return;
            }

            if (categoryTitle == root)
            {
                MessageBox.Show("不能选择“全部分类”。请重新选择一个分类!",
                    "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            // 检查 dbo.book 中是否存在这个 ISBN
            SqlConnection connection = Library.Connection.Instance();
            string queryString = "SELECT COUNT(*) FROM dbo.book where isbn=@ISBN;";
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@ISBN", ISBN);
            SqlDataReader reader = command.ExecuteReader();
            int count = 0;
            while (reader.Read())
            {
                count = int.Parse(reader[0].ToString());
            }
            reader.Close();
            if (count > 0)
            {
                ErrorMessage("ISBN已存在");
                return;
            }

            // 获取 category id
            queryString = "SELECT id FROM dbo.category where title=@title";
            command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@title", categoryTitle);
            reader = command.ExecuteReader();
            int categoryId = 1;
            while (reader.Read())
            {
                categoryId = (int)reader[0];
            }
            reader.Close();

            // 获取 library id
            queryString = "SELECT id FROM dbo.library where name=@libraryName";
            command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@libraryName", libraryName);
            reader = command.ExecuteReader();
            int libraryId = 1;
            while (reader.Read())
            {
                libraryId = (int)reader[0];
                Console.Out.WriteLine(libraryId);
            }
            reader.Close();

            // INSERT INTO dbo.book
            string sql = "INSERT INTO dbo.book (isbn, title, author, publisher, description, category_id)" +
                "VALUES(@ISBN, @title, @author, @publisher, @description, @category_id);";
            command = new SqlCommand(sql, connection);
            command.Parameters.AddWithValue("@ISBN", ISBN);
            command.Parameters.AddWithValue("@title", title);
            command.Parameters.AddWithValue("@author", author);
            command.Parameters.AddWithValue("@publisher", publisher);
            command.Parameters.AddWithValue("@description", description);
            command.Parameters.AddWithValue("@category_id", categoryId);
            command.ExecuteNonQuery();

            // INSERT INTO dbo.particular_book
            for (int i = 0; i < quantity; i++)
            {
                sql = "INSERT INTO dbo.particular_book (book_isbn, library_id)" +
                    "VALUES(@book_isbn, @library_id);";
                command = new SqlCommand(sql, connection);
                command.Parameters.AddWithValue("@book_isbn", ISBN);
                command.Parameters.AddWithValue("@library_id", libraryId);
                command.ExecuteNonQuery();
            }
            this.Dispose();
        }