BackTerminal.frmCatMan.btnRemoveCategory_Click C# (CSharp) Method

btnRemoveCategory_Click() private method

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

            // 不能删除“全部分类”
            if (title == root)
            {
                MessageBox.Show("不能删除“全部分类”。请重新选择一个分类!",
                    "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            string message = "你确定要删除“" + title + "”吗?";
            const string caption = "删除分类";
            var result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question);
            if (result == DialogResult.No) return;

            int id = GetIdByTitle(title);
            string str;
            str = "DELETE FROM dbo.category WHERE title='" + title + "';";

            SqlConnection connection = Library.Connection.Instance();
            SqlCommand command = new SqlCommand(str, connection);

            command.ExecuteNonQuery();

            command.Dispose();

            tvCategory.Nodes.Clear();
            InitializeTreeViewCategory();
        }