开发者

Passing date as parameter for query in visual basics

开发者 https://www.devze.com 2023-03-13 16:56 出处:网络
I want to pass a date as parameter in vb to query the data from the table. If I hardcore the value in the query it works fine for me, but if I pass it as parameter to query, like I am getting the data

I want to pass a date as parameter in vb to query the data from the table. If I hardcore the value in the query it works fine for me, but if I pass it as parameter to query, like I am getting the data from edit text and trying to开发者_如何学Go send that as a parameter, this does not work.

SELECT * 
 FROM VehicleAnalogParamDownload2
 WHERE Vapd2_Date between 'From_date.Text' And 'To_Date.Text'

Thanks


Don't embed the values directly in your SQL statement.

Use a parameterized query instead. That way you don't need to worry about different date/time formats, and for other parameters you can avoid SQL injection attacks. Your SQL would look something like:

SELECT * FROM VehicleAnalogParamDownload2
WHERE Vapd2_Date between @FromDate And @ToDate

and you'd then fill in the @FromDate and @ToDate parameter values programmatically.

See the documentation for SqlCommand.Parameters for an example.


in vb

dim str as string

str="SELECT * FROM VehicleAnalogParamDownload2 WHERE Vapd2_Date between" & 
     From_date.Text & "' And  '"& 'To_Date.Text'"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号