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#里为什么错了?请高手指点下,我就这些分了,全给了 展开
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#里为什么错了?请高手指点下,我就这些分了,全给了 展开
2个回答
展开全部
string strCondition = " where datediff(Minute,InquiryDate," + querydate + ") < 5";
需要改成
string strCondition = " where datediff(Minute,InquiryDate,cast('" + querydate + "' as datetime) ) < 5";
需要改成
string strCondition = " where datediff(Minute,InquiryDate,cast('" + querydate + "' as datetime) ) < 5";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询