控制产品数量的技术

Technique to control the quantity of products

我正在开发用于药店(购买、销售药品)的 c# 应用程序。 我想问一下,有没有正规的方法来控制数量?!我的意思是 .. 当我按下卖出按钮时,应该发生的数量操作是什么?

这就是我所做的:

MySqlCommand cmd = new MySqlCommand("insert into bills (bill_Number,bill_Date,bill_From,bill_Note,bill_TaxRate,bill_DisRate,bill_EntryDate,cus_Sup,byUser) values(" + txbBillNumber.Text + ",'" + DateTime.Parse(txbBillDate.Text).Year + "-" + DateTime.Parse(txbBillDate.Text).Month + "-" + DateTime.Parse(txbBillDate.Text).Day + "'," + sup_Id + ",'" + txb_Note.Text + "'," + taxRate + "," + disRate + ",'" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "'," + cus_Sup + ",'" + users + " | " + Environment.MachineName.ToString() + " | " + DateTime.Now.ToShortDateString() + " | " + DateTime.Now.ToShortTimeString() + "')", objConn);
cmd.ExecuteNonQuery();

//get the ID
cmd = new MySqlCommand("select max(bill_Id) from bills", objConn);
MySqlDataReader dataReader = cmd.ExecuteReader();
dataReader.Read();
billID = int.Parse(dataReader.GetValue(0).ToString());
dataReader.Close();


//insert into Bill_Details
for (int i = 0; i < listView1.Items.Count; i++)
{

    cmd = new MySqlCommand("insert into bill_Details (bill_Id,prod_Id,de_Cost,de_Quantity,de_Bonus,de_Price,de_ExpireDate,de_DisRate) values(" + billID + "," + int.Parse(listView1.Items[i].SubItems[1].Text) + "," + decimal.Parse(listView1.Items[i].SubItems[5].Text) + "," + decimal.Parse(listView1.Items[i].SubItems[3].Text) + "," + decimal.Parse(listView1.Items[i].SubItems[4].Text) + "," + decimal.Parse(listView1.Items[i].SubItems[6].Text) + ",'" + DateTime.Parse(listView1.Items[i].SubItems[7].Text).Year + "-" + DateTime.Parse(listView1.Items[i].SubItems[7].Text).Month + "-" + DateTime.Parse(listView1.Items[i].SubItems[7].Text).Day + "'," + decimal.Parse(listView1.Items[i].SubItems[10].Text) +")", objConn);
    cmd.ExecuteNonQuery();
}

//Edit information in products table IN ADDING
for (int i = 0; i < listView1.Items.Count; i++)
{
        cmd = new MySqlCommand("update products set prod_Quantity = (prod_Quantity-"+ ( decimal.Parse(listView1.Items[i].SubItems[3].Text)+decimal.Parse(listView1.Items[i].SubItems[4].Text) ) +") where prod_Id=" + listView1.Items[i].SubItems[1].Text, objConn);
        cmd.ExecuteNonQuery();
}

或者你能帮我做一个对我有用的 link 吗?

首先 - 请注意有关 SQL 注入的评论。如果您关心的是设计模式,那么您需要关注的第一个模式是 ADO.net

中正确参数化的 sql 命令

看这个例子:

            //create the command
            var cmd = new MySqlCommand(
                    "SELECT value1, value2 FROM myTable WHERE id_Field = @_id", 
                    connection); 

            //add the parameter to the command, with the desired value            
            cmd.Parameters.AddWithValue("@_id", myTextBox.Text);

其次,要回答您的问题,没有适用于所有情况的特定模式。但是我会推荐一个简单的数据访问 class 来表示您的库存项目(药品),并且这个 class 应该有一个通用的 addInventory(int adder) 方法。销售将导致将负数传递给添加库存方法,然后该方法将减少库存。因此,销售将执行为:

     var myMedicine = new Medicine();
     //sell 10 units
     myMedicine.addInventory(-10);

或者,如果除了减少库存之外还需要在销售期间完成其他工作,addInventory 方法可能应该是私有的,并从 public.Sold(int unitsSold) 方法内部调用,其中包含还有额外的工作。