习题,模拟超市结算系统

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace jiesuanxitong
{
    public partial class Form1 : Form
    {
        public SqlConnection conn;
        public Form1()
        {
            conn = new SqlConnection("Data Source=.;Initial Catalog=depot;User ID=sa;Password=1234");
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string my_t = textBox1.Text.ToString().Trim();
            if (my_t != "")
            {
                string a = "";
                string b = "";
                int c = 0;
                string sql = "select * from depot where dep_tiao='" + my_t + "'";
                SqlCommand myCommand = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    a = myReader.GetString(1);
                    b = myReader.GetString(2);
                    c = Convert.ToInt32(myReader.GetInt32(3));
                }
                conn.Close();
                if (this.HasCode(a))
                {
                    UpdateOrder(a);
                }
                else
                {
                    conn.Close();
                    Insert(a, b, c);
                }
            }
            else
            {
                MessageBox.Show("请输入条形码!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void Insert(string sno, string sname, int ssex)
        {
            string a = sno;
            string b = sname;
            int c = ssex;

            if (c == 0)
            {
                MessageBox.Show("对不起!本店没有此商品》", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                SqlCommand myCommand = new SqlCommand("insert into depotl (dep_tiao,dep_name,dep_value,dep_sum) values ('" + a + "','" + b + "'," + c + "," + 1 + ")", conn);
                conn.Open();
                myCommand.ExecuteNonQuery();
                conn.Close();
                shuaxin();
            }

        }
        private void shuaxin()
        {
            SqlCommand myCommand = new SqlCommand("select dep_tiao,dep_name,dep_value,dep_sum from depotl", conn);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = myCommand;
            conn.Close();
            DataSet ds = new DataSet();
            sda.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                string sql = "select sum(dep_value*dep_sum) from depotl";
                SqlCommand myCommand = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader myReader = myCommand.ExecuteReader();

                while (myReader.Read())
                {
                    label3.Text = myReader.GetInt32(0).ToString().Trim();

                } conn.Close();
            }
            else
            {
                MessageBox.Show("还未购买任何物品!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlCommand myCommand = new SqlCommand("delete from depotl ", conn);
            conn.Open();
            myCommand.ExecuteNonQuery();
            conn.Close();
            shuaxin();
            label3.Text = "0";
            textBox1.Text = "";
        }
        /// <summary>
        /// 判断数据库里面有没有 编号为:code的数据
        /// </summary>
        /// <param name="code">条形码</param>
        /// <returns></returns>
        public bool HasCode(string code)
        {
            string sql = "select * from depotl where dep_tiao='" + code + "'";
            SqlCommand myCommand = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader myReader = myCommand.ExecuteReader();
            bool tag = myReader.Read();
            conn.Close();
            if (tag)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 如果有,则将数据更新
        /// </summary>
        /// <param name="code">条形码</param>
        public void UpdateOrder(String code)
        {
            SqlCommand myCommand = new SqlCommand("update depotl set dep_sum=dep_sum+1 where dep_tiao='" + code + "'", conn);
            conn.Open();
            myCommand.ExecuteNonQuery();
            conn.Close();
            shuaxin();
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值