c# - Insert, select and update DateTime -



c# - Insert, select and update DateTime -

i have table time column named time , datatype date.

in asp.net want query insert date, , select between 2 date.

i seek this:

string info = datetime.now.toshortdatestring(); //date= 28/10/2014 -> dd/mm/yyyy string comando = "insert illustration (date) values '" +data+ "+"'";

and used query select between 2 dates

select * illustration date >= '25/10/2014' , date <= '28/10/2014'

i tried datatype varchar doesn't work. i'm trying datatype date.

but when i'm executing query insert error

conversion failed when converting date and/or time character string.

somebody please can help me? problem have save time table.

if possible want format of date: dd/mm/yyyy

update:

i'm having problem update row have date(date) , varchar(name) code:

string comando = "update illustration set name=@name info = @date"; sqlcommand cmd = new sqlcommand(comando, connect); cmd.parameters.add("@name", nome); cmd.parameters.add("@date", sqldbtype.date).value = data; cmd.executenonquery();

the messange error is: "string or binary info truncated. statement has been terminated."

you should never concatenate sql commands do! opens them sql injection attacks.

instead - use parameters! gets rid of lot of conversion issues.

so in case, should use:

string comando = "insert illustration (date) values (@dateparam)";

and need set @dateparam on sqlcommand:

cmd.parameters.add("@dateparam", sqldbtype.date).value = yourdatavaluehere

and should take care of issues!

if want select - again, use parameters!

select * illustration date >= @fromdate , date <= @todate

when run c#.

if utilize t-sql straight (in mgmt studio), utilize iso-8601 format yyyymmdd indepdent of dateformat and/or language settings -

select * illustration date >= '20141025' , date <= '20141028'

this works on any version of sql server , any dateformat, language , regional settinsg.

c# sql sql-server tsql ado.net

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -