【发布时间】:2014-02-08 14:22:59
【问题描述】:
现在我的代码背后有这个逻辑:
INSERT INTO PAYMENT (amount, appointmentID) VALUES
(@amount1, @app1),
(@amount2, @app2),
(@amount3, @app3),
...
假设@amount1 是10,@amount2 是15,@amount3 是20
我如何计算amount1、amount2、amount 3等的TOTAL值,并以这种方式将其作为总值45提交到数据库中:(@amount1,@app1)?我该如何处理这种逻辑?
这就是我的按钮点击代码:
string cMedication = string.Empty;
string cQuantity = string.Empty;
string cAppointment = string.Empty;
foreach (DataGridViewRow row in this.dataPrescription.Rows)
{
cMedication = row.Cells[0].Value.ToString();
cQuantity = row.Cells[1].Value.ToString();
cAppointment = txtAppointmentID.Text;
if (cAppointment == "NO APPOINTMENT HAS BEEN MADE")
{
MessageBox.Show("Please make an appointment first at the Nurse counter", "WARNING");
}
else
{
this.calculateTotal(cMedication, cQuantity, cAppointment);
}
}
这是我的 calculateTotal 函数:
private void calculateTotal(string cMedication, string cQuantity, string cAppointment)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["HConnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnectionString);
string insertPayment = "INSERT INTO PAYMENT (amount, appointmentID) " +
"VALUES (@insertAmount, @insertAppointment)";
using (SqlConnection connection = new SqlConnection(strConnectionString))
{
using (SqlCommand cmdPayment = new SqlCommand(insertPayment, connection))
{
string strPrice = "SELECT medicationPrice FROM MEDICATION WHERE medicationName= @getName";
SqlCommand cmdPrice = new SqlCommand(strPrice, con);
cmdPrice.Parameters.AddWithValue("@getName", cMedication);
con.Open();
SqlDataReader readPrice = cmdPrice.ExecuteReader();
if (readPrice.Read())
{
string getPrice = readPrice["medicationPrice"].ToString();
double doublePrice = Convert.ToDouble(getPrice);
double doubleQuantity = Convert.ToDouble(cQuantity);
double result = doublePrice * doubleQuantity;
string answer = result.ToString();
cmdPayment.Parameters.AddWithValue("@insertAmount", answer);
}
readPrice.Close();
con.Close();
cmdPayment.Parameters.AddWithValue("@insertAppointment", txtAppointmentID.Text);
connection.Open();
cmdPayment.ExecuteNonQuery();
connection.Close();
}
}
}
【问题讨论】:
-
您想在哪里总结所有内容?我没有看到任何代码将 amount1 amount2 amount3 参数相加。您希望它发生在数据库中还是在您的代码中?即您是否需要在 UI 中将其呈现给某人,或者将其保存在数据库中就足够了?
-
那是因为我不知道如何使用该方法,因此提出了这个问题。这只是对数据库的简单插入
标签: c# winforms loops foreach logic