Abdurahman Almatrodi
asked on
DateTime in C# with SQL 2008
Dear
I am using C# to save DateTime into SQL 2008. In my application the format is correct dd/MM/yyyy, but when inserted to database it changed to MM/dd/yyyy.
I tried to add in Program.cs:
CultureInfo ci = new System.Globalization.Cultu reInfo("en -GB");
System.Threading.Thread.Cu rrentThrea d.CurrentC ulture = ci;
System.Threading.Thread.Cu rrentThrea d.CurrentU ICulture = ci;
But, it did not work.
I found a solution to change it as DateTime.ToString("s") and worked as I want. But, I don't want to use conver function.
I need to set the default all the time in SQL to be dd/MM/yyyy
How to accomplish this?
And, Why is this happening?
btw, the format of my pc is dd/MM/yyyy
I am using C# to save DateTime into SQL 2008. In my application the format is correct dd/MM/yyyy, but when inserted to database it changed to MM/dd/yyyy.
I tried to add in Program.cs:
CultureInfo ci = new System.Globalization.Cultu
System.Threading.Thread.Cu
System.Threading.Thread.Cu
But, it did not work.
I found a solution to change it as DateTime.ToString("s") and worked as I want. But, I don't want to use conver function.
I need to set the default all the time in SQL to be dd/MM/yyyy
How to accomplish this?
And, Why is this happening?
btw, the format of my pc is dd/MM/yyyy
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
gustav is correct, the yyyy/mm/dd format will never cause you a problem, and you wont worry about sqlserver or your code understanding the day as month or month as day.
if you are using Parameters, then just give it as a datetime object, and you wont have to worry about the format anymore.
if you are using Parameters, then just give it as a datetime object, and you wont have to worry about the format anymore.
ASKER
Really, it is not easy to understand the issue in easy way.
Gustav:
This is what I done:
DateTime.ToString("yyyy-MM
Is this the best way to deal with datetime?
You can build your SQL in many ways, but the format is right except for the missing dot:
"yyyy-MM-dd hh:mm:ss.tt"
and the final string must be wrapped in single-quotes.
/gustav
"yyyy-MM-dd hh:mm:ss.tt"
and the final string must be wrapped in single-quotes.
/gustav
ASKER
Dear
What is the "dot" means?
The separator for milliseconds - as demonstrated here:
http://bytes.com/topic/sql-server/answers/80150-inserting-datetime-milliseconds-sql-server
/gustav
http://bytes.com/topic/sql-server/answers/80150-inserting-datetime-milliseconds-sql-server
/gustav
and if you want to formate it then
System.Datetime.Now.toStri