.NET SqlClient and Dates

Mondeo

Well-known member
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have this statement

SELECT * FROM vwAllMatrixVehicles WHERE created >= 2007/8/9

Using query analyser in sql management studio it returns about 80 rows.

Trying to use it in ASP.NET with the SqlCommand class the same statement returns 0 rows.

Ive double checked my code, its fine. If I replace the select statement with something else that doesnt use a date its fine.

Does SqlClient handle dates differently somehow?

Thanks
 
Its probably down to the date format being interpreted differently between ASP.Net and SQL Management studio.

Try putting the month as a full string (i.e. either August or September depending) and see if that alters the results. Also how is the string being generated in the ASP.Net page?
 
Originally I was just building the string with concatenation, but for this example I just put it directly into a string

Dim sql as string = "SELECT * FROM vwAllMatrixVehicles WHERE created >= 2007/8/9"

Its very puzzling???
 
What do you get if you tru
[highlight=sql]
Dim sql as string = "SELECT * FROM vwAllMatrixVehicles WHERE created >= 2007/9/8"
[/highlight]
 
Spot on. If I reverse the month and day it works fine. SqlClient must split the string up and pass the values individually, perhaps as parameters.

Thanks a lot
 
Rather than concatenating strings use a parameterized query - that way you dont need to worry about different systems trying to interpret the strings correctly.
 
Back
Top