【问题标题】:How do you compute each row of foreach double value to compute into a SINGLE value?你如何计算每一行 foreach 双精度值以计算成一个单值?
【发布时间】: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


【解决方案1】:

您需要避免过多地往返于数据库。循环中的一对 select-insert 语句不被认为是最佳实践。

我会尝试使用INSERT INTO SELECT 声明。比如:

INSERT INTO PAYMENT (amount, appointmentID) 
SELECT price * quantity, appointmentID FROM MEDICATION
INNER JOIN APPOINTMENT
ON "whatever_matches_medications_and_appointments"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多