餐饮管理之菜品管理
一、有关菜品的数据库的设计
菜品主要涉及菜系、菜名、价格、折扣这些属性。
在实际情况中很有可能新加入一种菜系,但是不能即时的增加相应的菜名,故而将菜系单独的作为一个基本表——菜系表。具体的菜系表如下(主键:菜系编号)
根据菜品的属性,设计的菜单信息表如下(主键:菜号)
二、菜品的增加
界面如下:
其中菜系使用的是下拉框combobox控件,里面的内容是在界面的初始化时访问菜系表加载进去的,代码如下:
private void Menu_Load(object sender, EventArgs e) { string strConn = @"Data Source=.;Initial Catalog=Restaurant;Integrated Security=SSPI; "; SqlConnection conn = new SqlConnection(strConn); conn.Open(); String sqlcommondcaixi = String.Format(@"select * from 菜系信息"); SqlCommand cmdcaixi = new SqlCommand(sqlcommondcaixi, conn); SqlDataReader drcaixi = cmdcaixi.ExecuteReader(); while (drcaixi.Read()) { cbxcaixi.Items.Add(drcaixi["菜系编号"].ToString()+drcaixi["菜系名"].ToString()); } drcaixi.Close(); conn.Close(); }
选好菜系、填写名称、价格后点击确认,首先将菜系号提取出来保存在变量caixi中,接着判断要加入的菜是否已经存在,如果存在会提示,如果不存在就将加入菜单信息表中。代码如下:
private void btn_menuok_Click(object sender, EventArgs e) { string caixi = cbxcaixi.Text.Trim(); if (caixi[1] >= '0' && caixi[1] <= '9') { caixi = caixi.Substring(0, 2); } else { caixi = caixi.Substring(0, 1); } string caiming = txbname.Text.Trim(); float price = float.Parse(txbprice.Text); string strConn = @"Data Source=.;Initial Catalog=Restaurant;Integrated Security=SSPI; "; SqlConnection conn = new SqlConnection(strConn); conn.Open(); string sqlcommond = string.Format(@"select * from 菜单信息 where (菜系编号='" + caixi.Substring(0,1) + "') and (菜名 = '" + caiming + "') "); SqlCommand cmd = new SqlCommand(sqlcommond, conn); SqlDataReader dr = cmd.ExecuteReader(); if (!dr.Read()) { dr.Close(); string sqlcommond1 = string.Format(@"insert into 菜单信息(菜系编号,菜名,价格,折扣)values('{0}','{1}','{2}',1)", caixi, caiming, price); SqlCommand cmd1 = new SqlCommand(sqlcommond1, conn); SqlDataReader dr1 = cmd1.ExecuteReader(); MessageBox.Show("成功加入新菜!!"); dr1.Close(); } else { MessageBox.Show("您加的菜已经存在!!"); } conn.Close(); }
三、菜单更新
界面如下:
在这部分中,用treeview控件来显示现有的菜(父结点:菜系;子节点:菜名)。treeview控件的内容是在界面初始化时访问数据库,使用两层循环,外层循环确定父结点,内层循环确定子节点。代码如下
private void ViewOrder_Load(object sender, EventArgs e) { treeView1.Nodes.Clear(); string strConn = @"Data Source=.;Initial Catalog=Restaurant;Integrated Security=SSPI; "; SqlConnection conn = new SqlConnection(strConn); conn.Open(); string sqlcommond = "select 菜系编号,菜系名 from 菜系信息 "; SqlCommand cmd = new SqlCommand(sqlcommond, conn); // SqlDataReader dr = cmd.ExecuteReader(); //对SQL或存储过程执行后返回的“结果”进行操作 SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); DataTable dt = new DataTable(); da.Fill(ds); dt = ds.Tables[0]; // MessageBox.Show(dt.Rows.Count.ToString()); for (int i = 0; i < dt.Rows.Count; i++) { TreeNode rootnode = new TreeNode(); string str = dt.Rows[i]["菜系编号"].ToString(); rootnode.Text = str + dt.Rows[i]["菜系名"].ToString();//父结点 treeView1.Nodes.Add(rootnode); string sqlcommondkid = "select 菜名 from 菜单信息 where 菜系编号 ='" + str + "' "; SqlCommand cmdkid = new SqlCommand(sqlcommondkid, conn); // SqlDataReader dr = cmd.ExecuteReader(); //对SQL或存储过程执行后返回的“结果”进行操作 SqlDataAdapter dakid = new SqlDataAdapter(); dakid.SelectCommand = cmdkid; DataSet dskid = new DataSet(); DataTable dtkid = new DataTable(); dakid.Fill(dskid); dtkid = dskid.Tables[0]; for (int j = 0; j < dtkid.Rows.Count; j++)//得到子结点 { string kid = dtkid.Rows[j]["菜名"].ToString(); rootnode.Nodes.Add(kid); } } conn.Close(); }
当选中某个子节点会触发treeview控件的AfterSelect事件,右侧的菜品名、原单价、原折扣对应的控件会将选中节点的对应信息显示出来。在AfterSelect事件中首先判断选中的是否是子节点,只有子节点才会继续进行。代码如下:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Parent != null) { //将选择的菜名显示在菜名框里 if (e.Node.Parent.Text[1] >= '0' && e.Node.Parent.Text[1] <= '9')//第二个字符是数字,菜系就是两位数 { strcaixi = e.Node.Parent.Text.Substring(0, 2); } else { strcaixi = e.Node.Parent.Text.Substring(0, 1); } txb_ordername.Text = e.Node.ToString().Substring(10);//去除文字中开始的“treenode”和“ ” //查询菜的具体信息:价格、折扣等 string strConn = @"Data Source=.;Initial Catalog=Restaurant;Integrated Security=SSPI; "; SqlConnection conn = new SqlConnection(strConn); conn.Open(); string sqlcommond = string.Format("select * from 菜单信息 where 菜名 = '" + txb_ordername.Text + "' "); SqlCommand cmd = new SqlCommand(sqlcommond, conn); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { txb_oldperprice.Text = dr["价格"].ToString(); txb_olddiscount.Text = dr["折扣"].ToString(); break; } conn.Close(); } }
当点击更改按钮时会将新的要填写的信息显示出来(一开始时它们的visible属性是false,),代码如下:
private void btn_change_Click(object sender, EventArgs e) { //将填写更新的价格折扣框显示出来 label2.Visible = true; label4.Visible = true; txb_newdiscount.Visible = true; txb_newperprice.Visible = true; btn_ok.Visible = true; }
之后点击确认按钮,将新的信息保存到数据库中,代码如下:
private void btn_ok_Click(object sender, EventArgs e) { string caiming = txb_ordername.Text.Trim(); string newprice = txb_newperprice.Text.Trim(); string newdiscount = txb_newdiscount.Text.Trim(); string strConn = @"Data Source=.;Initial Catalog=Restaurant;Integrated Security=SSPI; "; SqlConnection conn = new SqlConnection(strConn); conn.Open(); //建立SqlCommand对象,负责SQL语句的执行和存储过程的调用; string sqlcom = string.Format(@"update 菜单信息 set 价格 = '" + newprice + "',折扣 = '" + newdiscount + "' where 菜名 = '" + caiming + "'");//插入数据是数字就无 ''。。。 SqlCommand cmd = new SqlCommand(sqlcom, conn); SqlDataReader dr = cmd.ExecuteReader(); dr.Close(); conn.Close(); MessageBox.Show(caiming+" 菜品更新成功!!"); }
当点击删除时将菜品信息从菜单信息表中删除,,代码如下:
private void btn_delete_Click(object sender, EventArgs e) { string caiming = txb_ordername.Text; string strConn = @"Data Source=.;Initial Catalog=Restaurant;Integrated Security=SSPI; "; SqlConnection conn = new SqlConnection(strConn); conn.Open(); string sqlcommond = string.Format(@"delete from 菜单信息 where 菜名 = '" + caiming + "'"); //建立SqlCommand对象,负责SQL语句的执行和存储过程的调用; SqlCommand cmd = new SqlCommand(sqlcommond, conn); //对SQL或存储过程执行后返回的“结果”进行操作 SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; cmd.ExecuteReader(); MessageBox.Show(caiming+" 成功从菜单中删除菜品!!"); conn.Close(); }