VB.NET Sql statement with datetime filter

Hey,

I want to select some rows from a SQL server database using vb.NET. The selection is based on a datetime field in Vb.NET (the datetime should be between two values which are colums in my sql table). I can't seem to find the correct format.

The statement should be something like

Select * from Table Where dotNetDate Between DateColumn1 and DateColumn2

If tried this in vb.net but it returns nothing...

sqlCmd = New SqlCommand("SELECT * FROM Table WHERE '" + pdatDate.ToString("d/MM/yyyy hh:mm:ss") + "' between FromDateTime and ToDateTime", objSQLConn)

Tnx for the help.
LVL 5
tsayAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is the proper way to avoid all the problems:
http://www.knowdotnet.com/articles/defaultparameters.html
http://www.knowdotnet.com/articles/dynamicsqlparameters.html

sqlCmd = New SqlCommand("SELECT * FROM Table WHERE @d between FromDateTime and ToDateTime", objSQLConn)
sqlCmd.Parameters.Add("@d", pdatDate )
0
 
Jupiler78Commented:
Hi tsay,

try to compare it to each column: where pdatDate.ToString("d/MM/yyyy hh:mm:ss") > fromdatetime and pdatDate.ToString("d/MM/yyyy hh:mm:ss") < todatetime

Cheers!
0
 
mazkotCommented:
try this, vb seems to have this touch and go feelin when using the "+"

dim strdate as string =pdatDate.ToString("d/MM/yyyy hh:mm:ss")
sqlCmd = New SqlCommand("SELECT * FROM Table WHERE '" & strdate & "' between FromDateTime and ToDateTime", objSQLConn)
0
 
tsayAuthor Commented:
Tnx angelIII.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.