"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();
}