如何在这些sql中传递datetime的参数 [英] how to pass parameter of datetime in these sqls

查看:101
本文介绍了如何在这些sql中传递datetime的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string sql = "select * from   tblVehicle where CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME) = '?'";


在问号中,我需要传递DateTime的值,但是我的DateTime是字符串和短日期时间格式.
在这种情况下,如何传递DateTime类型的参数?

我保存日期的字符串的格式为2011年1月4日,我需要在?"内传递它,该怎么做?

问候,
Bishnu Karki


In the question mark, I need to pass values of DateTime but my DateTime is in string and in short date time format.
In such case how can I pass the parameter of DateTime type?

My string holding the date is in format 11/4/2011 and i need to pass it inside ''?'' so how to do this?

With regards,
Bishnu Karki

推荐答案

string sql = "select * from   tblVehicle where CAST(floor( CAST( Vehentrytime AS FLOAT ) )AS DATETIME) = 'yyyy-mm-dd'";



您需要使用"yyyy-mm-dd"格式(当然是字符串格式)传递日期时间.



You need to pass date time with ''yyyy-mm-dd'' format(of course in string format). This will serve your purpose.


不要像这样连接SQL字符串,请使用参数化查询

本文应该解释一切

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx [ ^ ]
Don''t concatenate SQL strings like this, use a parameterized query

This article should explain everything

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx[^]


只需使用参数化查询将其作为实际的DateTime传递(在这种情况下,问题就消失了),或者先将字符串转换为DateTime,然后将其转换回与SQL兼容的日期时间字符串"yyyy-MM" -dd hh:mm:ss.

就个人而言,我会选择前者-它更容易,更自然.
Just pass it through either as a actual DateTime using a Parametrized query - in which case the problem goes away - or by converting the string into a DateTime first, then convert it back to an SQL compliant datetime string "yyyy-MM-dd hh:mm:ss".

Personally, I would go for the former - it'';s a lot easier and more natural.


这篇关于如何在这些sql中传递datetime的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆