c# - How to convert a SQL date to a DateTime? -


i have column date type in sql database. how can convert c# datetime , again sql date?

c# date reader

datetime date1; datetime.tryparse(reader["datecolumn"], out date1); 

to insert date

string date1="2013-12-12";  datetime date2; datetime.tryparse(reader["datecolumn"],out date2);  sqlcommand cmd= new sqlcommand("insert table (datecolumn) values(@date2)",connection); cmd.parameters.addwithvalue("@date2",date2.date); 

tryparse returns true on successful casting false otherwise.

vb

to date reader

dim date1 date=ctype(reader("datecolumn"),date) 

to insert date

 dim date1 string="2013-12-12" 'you can date html input of type date   dim cmd new sqlcommand("insert table (datecolumn) values(@date1)",connection)  cmd.parameters.addwithvalue("@date1",ctype(date1, date)) 

note:code written in vb. can write c# equivalent of above code. function name remains same in both vb , c#. ctype not available in c#(though same explicitly casting variable eg. date1=(datetime)reader("datecolumn"); recommend using tryparse doesn't throw exception on unsuccesful parses/casting.

syntax

date.tryparse(reader("datecolumn"), date1) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -