C# .NET - Passing Date value to SQL - Asked By suresh krishna on 29-Jun-12 07:40 AM

Earn up to 10 extra points for answering this tough question.
 SQL server Date culture is  UK Culture.....

How to pass the date value from c - sharp ?

Chintan Vaghela replied to suresh krishna on 30-Jun-12 01:49 AM

Hi Frndz,

 

Functionality:  Pass Date value dd/mm/yyyy to Sql server

 

 

To achieve this task,

 

First set Culture in UK format

 

CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;

 

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

 

After then Parse to Date in dd/mm/yyyy format ( your string Date must be in dd/mm/yyyy format)

 

   DateTime dtTemp = DateTime.ParseExact(dateString, "dd/MM/yyyy", null);

 

Now Store dtTemp Value to DB table

 

 

 

Full Logic     :

 

 

        CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;

        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

        string dateString = "30/12/2012";

        // in order not to have to specify a FormatProvider

        DateTime dtTemp = DateTime.ParseExact(dateString, "dd/MM/yyyy", null);

 

 

 

Hope this helpful!

Thanks

 

 

 

[)ia6l0 iii replied to suresh krishna on 01-Jul-12 09:46 AM
Although there are many ways of achieving this, the best way to do this is to store the UTC date time in SQL Server database. Do your regional transactions in the client or the service side. Do not store non-UTC values in SQL Server.
suresh krishna replied to Chintan Vaghela on 02-Jul-12 04:47 AM
Thanks a lot ......
 Its works me
Kamal Ramavat replied to suresh krishna on 26-Dec-12 09:02 AM

To get current date in c# use DateTime.Now.ToString()............ there is also some different methods under Now.... use what is appropriate for u .

first u must have to set date value in UK culture.... then

if u r using a procedure it is easier..... say proc. name  saveCurrentDate

String s=DateTime.Now.ToString(); 

String queryString="saveCurrentDate '"+s+"'"; //// or String queryString="insert int myTable(mydate) values ('"+s+"')";

 

hope it will hellped u