DateTime fromDate = dateTimePicker1.Value, toDate = dateTimePicker2.Value;
string query2 = "select * from Sales where DatePurchased >= '" + fromDate.ToShortDateString() + "' AND DatePurchased <='" + toDate.ToShortDateString() + "'";
using (OleDbConnection conn2 = new OleDbConnection(connStr))
{
using (OleDbCommand command = new OleDbCommand(query2, conn2))
{
command.Connection = conn2;
conn2.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
graphics.DrawString(string.Format("{0:000000}", reader.GetInt32(0)), font, new SolidBrush(Color.Black), startX, startY + offset);
graphics.DrawString(reader.GetString(1), font, new SolidBrush(Color.Black), startX+90, startY + offset);
graphics.DrawString(reader.GetString(2), font, new SolidBrush(Color.Black), startX + 250, startY + offset);
graphics.DrawString(Convert.ToString(reader.GetDouble(3)), font, new SolidBrush(Color.Black), startX + 500, startY + offset);
startY += 35;
}
}
}
我在这里发现错误:
OleDbDataReader reader = command.ExecuteReader();
错误是
“未处理OleDbException:条件表达式中的数据类型不匹配。”
我不知道该怎么做,我在数据库中的数据是在日期/时间。请帮忙
答案 0 :(得分:1)
您的ToShortDateString()不是与sql兼容的字符串:
fromDate.ToString("yyyyMMdd")
将为您提供与sql兼容的日期字符串。
string query2 = "select * from Sales where DatePurchased >= '" + fromDate.ToString("yyyyMMdd")+ "' AND DatePurchased <='" + toDate.ToString("yyyyMMdd")+ "'";
然而,使用参数化查询会更好。
答案 1 :(得分:0)
如果修复了您的错误,请尝试将查询更改为
string query2 = "select * from Sales where DatePurchased between " + fromDate.ToShortDateString() + " AND " + toDate.ToShortDateString() + " "
如果它不起作用,请检查这两个日期范围之间是否有数据。
答案 2 :(得分:0)
试试这个:
string query2 = "select * from Sales where DatePurchased >= '" + fromDate.ToString("yyyy-MM-dd") + "' AND DatePurchased <='" + toDate.ToString("yyyy-MM-dd") + "'";
之前它适用于我