我试图运行以下简单的SQL查询来选择两个日期之间的数据。来自以下DateTimePickers日期:DTP_From
,DTP_To
从字符串中转换日期和/或时间时DateTime转换失败
DateTime startDate = DTP_From.Value.Date;
DateTime endDate = DTP_To.Value.Date;
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "SELECT * From Report_Sales where Date >= '" + startDate + "' AND Date <= '" + endDate + "'";
当执行查询我得到以下错误:
datetime conversion failed when converting date and/or time from character string
如何运行上面的查询无错?
我想喜欢使用“介于”关键字来代替> =和<=运营商 – Shell
将日期转换为'yyyy-MM-dd HH:mm'格式。并确保endDate的开始日期必须是00:00和23:29,否则某些记录将不会显示。 – Shell