餐饮管理系统大作业SQL/C#

来源:互联网 发布:如何破解mac迅雷限速 编辑:程序博客网 时间:2024/04/30 18:48

using System.Data.SqlClient;

//数据绑定

private void BindData()

        {
            SqlConnection con=new SqlConnection(s);
            SqlDataAdapter sda = new SqlDataAdapter("select * from cpb",con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Columns[0].HeaderText = "菜名";
            dataGridView1.Columns[1].HeaderText = "菜品种类";
            dataGridView1.Columns[2].HeaderText = "菜品容量";
            dataGridView1.Columns[3].HeaderText = "菜品价格";

        }

//修改按钮

private void buttonxg_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)//有选中的行
            {
                string names = dataGridView1.SelectedCells[0].Value.ToString();
                SqlConnection con = new SqlConnection(s);
                con.Open();
                string s3 = "update cpb set cpname='"+textBoxcpm.Text+"',cptype='"+textBoxcpzl.Text+"',cpstore="+textBoxcpyl.Text+",cpprice="+textBoxcpjg.Text+"where cpname='" + names.Trim() + "'";
                SqlCommand cmd = new SqlCommand(s3, con);
                cmd.ExecuteNonQuery();
                con.Close();
                BindData();
            }
        }

//program.cs 登陆主界面切换

 static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);                 
            Formdl fl = new Formdl();
            fl.ShowDialog();            
            if (fl.DialogResult == DialogResult.OK)
            {
                MainForm mf = new MainForm();
                mf.name = fl.textBox1.Text;
                mf.password = fl.textBox2.Text;
                mf.time = DateTime.Now.ToShortDateString();
                Application.Run(mf);
            }
            else
            {
                return;
            }        
        }

 private void button1_Click(object sender, EventArgs e)
        {
            string s = "server=localhost;Database=cygl;uid="
            + textBox1.Text.ToString() + ";pwd=" + textBox2.Text.ToString();         
            con = new SqlConnection(s);
            try
            {
                con.Open();
                if (con.State == ConnectionState.Open)//判断数据库是否连接成功
                {
                    con.Close();
                    this.DialogResult = DialogResult.OK;
                    this.Close();                    
                }           
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

//主界面 listview控件 激活 记得设置LISTVIEW的视图和LARGEIMAGELIST

 private void MainForm_Activated(object sender, EventArgs e)
        {
            lv.Items.Clear();
            s = "server=localhost;Database=cygl;uid=" + name + ";pwd=" + password;
            SqlConnection con = new SqlConnection(s);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from ztb",con);
            sdr = cmd.ExecuteReader();
            while(sdr.Read())
            {
                string zt=sdr["state"].ToString().Trim();
                AddItems(zt);
            }
            con.Close();
        }

private void AddItems(string zt)
        {
            if(zt=="0")
            {
                lv.Items.Add("     "+sdr["tableid"].ToString(), 0);//imagelist中的图片索引
            }
            else
            {
                lv.Items.Add("     " + sdr["tableid"].ToString(), 1);
            }
        }

 private void lv_Click(object sender, EventArgs e)
        {
            string names = lv.SelectedItems[0].SubItems[0].Text;//点击选中的LV中的项目
            s = "server=localhost;Database=cygl;uid=" + name + ";pwd=" + password;
            SqlConnection con = new SqlConnection(s);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from ztb where tableid="+names, con);
            sdr = cmd.ExecuteReader();
            sdr.Read();
            string zt = sdr["state"].ToString().Trim();
            sdr.Close();
            if(zt=="0")
            {
                this.contextMenuStrip1.Items[0].Enabled = false;
                this.contextMenuStrip1.Items[1].Enabled = true;
                this.contextMenuStrip1.Items[3].Enabled = true;
                this.contextMenuStrip1.Items[5].Enabled = true;
              
            }
            if(zt=="1")
            {
                this.contextMenuStrip1.Items[0].Enabled = true;
                this.contextMenuStrip1.Items[1].Enabled = false;
                this.contextMenuStrip1.Items[3].Enabled = false;
                this.contextMenuStrip1.Items[5].Enabled = false;
           
            }
            con.Close();
        }

    private void menu1_Click(object sender, EventArgs e)
        {
            if (lv.SelectedItems.Count != 0)
            {
                SqlConnection con = new SqlConnection(s);
                con.Open();
                SqlCommand cmd = new SqlCommand("update ztb set state=0 where tableid=" + lv.SelectedItems[0].SubItems[0].Text, con);
                cmd.ExecuteNonQuery();
                con.Close();
                this.MainForm_Activated(sender, e);
            }
            else
            {
                MessageBox.Show("请选择桌台");
            }
        }


设置ANCHOR属性四个全选 让控件随着界面变化而变化

cmd3.ExecuteScalar() 返回查询的结果集的第一行第一列

设置界面的KEYPRIVIEW属性为TRUE 使界面接受到按键事件

 private void Formdl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) //如果按下回车键 
                button1_Click(sender, e);
            if (e.KeyChar == 27)         //按下ESC键
                button2_Click(sender, e);
        }

0 0
原创粉丝点击