"SELECT SUM(Cost) FROM Repair WHERE RepairDate BETWEEN '" + startdate + "' AND '" + enddate + "'";
こんにちは、パラメータ化されたクエリに変更する以外に、この SQL に何か問題がありますか? ExecuteScalar を実行しようとすると、条件式エラーでデータ型が一致しません:
public int TotalRepairCost(DateTime startdate, DateTime enddate)
{
int total;
OleDbConnection oleConn = new OleDbConnection(connString);
oleConn.Open();
string sql = "SELECT SUM(Cost) FROM Repair WHERE RepairDate BETWEEN '" + startdate + "' AND '" + enddate + "'";
OleDbCommand cmd = new OleDbCommand(sql, oleConn);
total = (int)cmd.ExecuteScalar();
oleConn.Close();
return total;
}
私のWindowsフォームボタンクリックで
private void btnTotal_Click(object sender, EventArgs e)
{
DateTime startdate = Convert.ToDateTime(txtStartDate.Text);
DateTime enddate = Convert.ToDateTime(txtEndDate.Text);
lblTotal.Text = client.TotalRepairCost(startdate, enddate).ToString();
}