How to convert a SQL date to a DateTime?
Solution 1
A sql DATE
can be directly cast to a .net DateTime
and vice-versa.
to get it, use the SqlDataReader.GetDatetime Method
DateTime myDate = myDataReader.GetDateTime(myColumnIndex);
to set it, simply assign it to the value of the SqlParameter and use the .Date
property of the DateTime
Solution 2
c# To get date from 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 into table (dateColumn) Values(@date2)",connection);
cmd.Parameters.AddWithValue("@date2",date2.Date);
TryParse returns true on successful casting false otherwise.
VB
To get date from reader
Dim date1 as Date=CType(reader("dateColumn"),Date)
To insert date
Dim date1 as String="2013-12-12" 'you can get this date from an html input of type date
Dim cmd As New SqlCommand("Insert into table (dateColumn) Values(@date1)",connection)
cmd.Parameters.AddWithValue("@date1",CType(date1, Date))
NOTE:Code is written in VB. You can easily write the c# equivalent of the above code. The function name remains the same in both VB and c#. Also CType is not available in c#(though its the same as explicitly casting a variable eg. date1=(DateTime)reader("dateColumn");
I would recommend using TryParse
which doesn't throw any exception on unsuccesful parses/casting.
Syntax
Date.TryParse(reader("dateColumn"), date1)
Solution 3
string mydate= Convert.ToDateTime(reader["dateColumn"]).ToShortDateString().ToString());
These code is working for me.Try these one
petros
Updated on January 28, 2020Comments
-
petros about 4 years
I have a column with the
date
type in my SQL database. How can I convert it to C#DateTime
and then back again to a SQLdate
? -
thunderbird almost 11 years@newStackExchangeInstance Did you even read my answer before voting it down?? Do you even know what VB and C# are and how there is practically a very little difference between them??
-
Yuck almost 11 years@thunderbird Macintosh apples aren't Granny Smith apples, although they are both apples. If you wanted one and were given another you might be annoyed or at least confused / surprised.
-
thunderbird almost 11 years@Yuck i have given instructions on how to convert it to c#. Besides VB and C# is like coding in java and C++. If you know one, you got the other one. I will anyway translate it to c#.
-
code4life almost 11 years-1: you should provide c# examples. If you feel vb.net and c# are so similar, why can't you be bothered to provide a c# example?
-
It'sNotALie. almost 11 years@thunderbird I do, I just think that if the OP asks for C#, he should get C#
-
It'sNotALie. almost 11 yearsAnd also, Java and C++ is a TERRIBLE, TERRIBLE comparison. At least C++ is useful.
-
thunderbird almost 11 years@newStackExchangeInstance again i am talking about the basic syntax only. I agree i overlooked the c# part because i looked for it in the tag section when it normally should be.
-
It'sNotALie. almost 11 yearsAn error in your C# code: C# comments start with
//
, not with'
. -
thunderbird almost 11 years@newStackExchangeInstance give me a break i copy pasted my vb code and made changes to it.
-
Tarık Özgün Güner about 9 yearsThx. I am confused when msdn says "No conversions are performed; therefore, the data retrieved must already be a DateTime object."