Passing date as parameter for query in visual basics
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'"
精彩评论