VB.NET Sql statement with datetime filter


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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is the proper way to avoid all the problems:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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)
tsayAuthor Commented:
Tnx angelIII.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.