首页 > 解决方案 > C# SQL Server 从数据库中读取数量并将其显示在消息框中

问题描述

所以我几乎完成了使用 C# 和 sql server management studio 完成我的 POS,但是我在显示特定产品的可用数量时遇到问题,如果你输入的 Quantity 大于 Stocks(每种产品的数量是基于我的股票)并点击添加按钮,将出现一个消息框,提示用户该产品的唯一可用数量就是这样。

这是为了确定数量是否超过:

 public void Q_tity()
    {


        try
        {
            SqlConnection cn = new SqlConnection();
            con.Open();
            string qwe = "Select MIN(Quantity/Quantifier) from Stocks where ItemID in (Select ItemID from Recipe where MenuID in (Select MenuID from Menu where ItemName ='" + txtProd.Text + "'))";


            SqlCommand cmd = new SqlCommand(qwe, con);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (txtProd.Text == "")
                {
                    MessageBox.Show("Please Input a correct format", "Format info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {


                    double val = double.Parse(dr[0].ToString());
                    Quantity = Convert.ToInt32(val);



                }
            }
        }
        catch
        {
            MessageBox.Show("The ingredient on recipe cannot identify. please fix it from the maintenance form", "Recipe Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        con.Close();
    }

这是为了识别数量是否超过。

这是应该提示用户的添加按钮:

private void btnAdd_Click(object sender, EventArgs e)
    {

        if (txtQuant.Text.Length == 0)
        {
            MessageBox.Show("Quantity Field is Required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            Q_tity();
            con.Open();

          //  con.Close();
            if (Quantity < Convert.ToDouble(txtQuant.Text))
            {
                string qwe = "Select MIN(Quantity/Quantifier) from Stocks where ItemID in (Select ItemID from Recipe where MenuID in (Select MenuID from Menu where ItemName ='" + txtProd.Text + "'))";


                SqlCommand cmd = new SqlCommand(qwe, con);
                SqlDataReader dr = cmd.ExecuteReader();


                MessageBox.Show(txtQuant.Text + " is unavailable! \n The Available Quantity of  " + txtProd.Text + " is " + dr + " only!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                con.Close();
            }

这是应该提示用户的添加按钮

可悲的是,这就是结果:

可悲的是,这就是结果。

标签: c#sql-server-2012

解决方案


在您的情况下,您必须使用 ExecuteScalar() 而不是 ExecuteReader

这是修复

    private void btnAdd_Click(object sender, EventArgs e)
    {

        if (txtQuant.Text.Length == 0)
        {
            MessageBox.Show("Quantity Field is Required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            Q_tity();
            con.Open();

            //  con.Close();
            if (Quantity < Convert.ToDouble(txtQuant.Text))
            {
                string qwe = "Select MIN(Quantity/Quantifier) from Stocks where ItemID in (Select ItemID from Recipe where MenuID in (Select MenuID from Menu where ItemName ='" + txtProd.Text + "'))";


                SqlCommand cmd = new SqlCommand(qwe, con);
                int qty = Convert.ToInt32(cmd.ExecuteScalar()); //<--  use executescalar


                MessageBox.Show(txtQuant.Text + " is unavailable! \n The Available Quantity of  " + txtProd.Text + " is " + qty + " only!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                con.Close();
            }
        }

推荐阅读