0
現在我背後有我的代碼這樣的邏輯:如何計算每行的foreach double值以計算爲單值?
INSERT INTO PAYMENT (amount, appointmentID) VALUES
(@amount1, @app1),
(@amount2, @app2),
(@amount3, @app3),
...
假設@ amount1是10,@ AMOUNT2是15,@ AMOUNT3是20
我怎麼計算amount1的總價值,AMOUNT2 ,金額3等,並以這種方式將其總數值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();
}
}
}
你想把所有的東西都歸在哪裏?我沒有看到任何代碼將總金額3的金額累加起來。 你想讓它發生在數據庫或你的代碼? I.E您是否需要將其呈現給用戶界面中的某個用戶,或者您是否足夠擁有數據庫? –
那是因爲我不知道如何處理這個方法,所以這個問題。這只是一個簡單的插入數據庫 – user3195396