我正在研究用于药店(购买,销售药品)的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();
}
或者你可以帮我找一个对我有用的链接吗?
答案 0 :(得分:0)
首先 - 请注意有关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);
其次,要回答您的问题,没有适用于所有情况的具体模式。但是我建议使用一个简单的数据访问类来表示您的库存项目(药品),这个类应该有一个通用的addInventory(int adder)方法。销售将导致将负数传递给添加库存方法,然后减少库存。因此,销售将按以下方式执行:
var myMedicine = new Medicine();
//sell 10 units
myMedicine.addInventory(-10);
或者,如果除了减少库存之外还需要在销售期间完成其他工作,那么addInventory方法可能应该是私有的,并且可以从包含额外工作的公共.Sold(int unitsSold)方法内部调用。< / p>