C#运行sql执行datediff作为条件时,提示SQL语句错误

stringquerydate=DateTime.Now.ToString().Trim();stringstrCondition="wheredatediff(Minu... string querydate = DateTime.Now.ToString().Trim();
string strCondition = " where datediff(Minute,InquiryDate," + querydate + ") < 5";
string strsql = "select sum(MinTruck_Number)+sum(MidTruck_Number)+sum(LargeTruck_Number)+sum(MinCar_Number)+sum(LargeCar_Number)+sum(Trailer_Number)+sum(OutsizeTruck_Number) as SumProduct from HourNumberTable " + strCondition + " group by HourSegment";

我在SQL测试的语句:
select sum(MinTruck_Number)+sum(MidTruck_Number)+sum(LargeTruck_Number)+sum(MinCar_Number)+sum(LargeCar_Number)+sum(Trailer_Number)+sum(OutsizeTruck_Number) as SumProduct from HourNumberTable where datediff(Minute,InquiryDate,getdate())<5 group by HourSegment order by HourSegment desc
正确的,返回结果了,但在C#里为什么错了?请高手指点下,我就这些分了,全给了
展开
 我来答
syht2000
高粉答主

2013-08-29 · 关注我不会让你失望
知道大有可为答主
回答量:3万
采纳率:79%
帮助的人:1.4亿
展开全部
string strCondition = " where datediff(Minute,InquiryDate," + querydate + ") < 5";

需要改成
string strCondition = " where datediff(Minute,InquiryDate,cast('" + querydate + "' as datetime) ) < 5";
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
射手幽灵伊
2013-08-29 · TA获得超过2715个赞
知道大有可为答主
回答量:4955
采纳率:81%
帮助的人:1918万
展开全部
string strCondition = " where datediff(Minute,InquiryDate,'" + querydate + "') < 5";
要加单引号。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式