Datta
Datta

Reputation: 559

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 send that as a parameter, this does not work.

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

Thanks

Upvotes: 0

Views: 3121

Answers (2)

Vikky
Vikky

Reputation: 764

in vb

dim str as string

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503290

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.

Upvotes: 4

Related Questions