I have a table with time the column named time and the datatype is Date
.
In asp.net I want a query to insert the date, and another so select between 2 date.
You should NEVER concatenate together your SQL commands like you do! This opens them up to SQL injection attacks.
Instead - use parameters! This also gets rid of a lot of conversion issues.
So in your case, you should use:
string comando = "INSERT INTO example (date) VALUES (@DateParam)";
and then you need to set the @DateParam
on your SqlCommand
:
cmd.Parameters.Add("@DateParam", SqlDbType.Date).Value = YourDataValueHere
and that should take care of all your issues!
If you want to select - again, use parameters!
select *
from example
where date >= @fromDate and date <= @toDate
when you run this from C#.
If you use T-SQL directly (in Mgmt Studio), then use the ISO-8601 format YYYYMMDD
which is indepdent of any dateformat and/or language settings -
select *
from example
where date >= '20141025' and date <= '20141028'
This works on any version of SQL Server and with any dateformat, language and regional settinsg.