Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting dates from SQL into C# DateTime objects
Message
From
16/07/2004 18:02:59
 
 
To
16/07/2004 13:58:34
Keith Payne
Technical Marketing Solutions
Florida, United States
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00924961
Message ID:
00925386
Views:
19
>>I can't figure out how to convert datetime field from Microsoft SQL Server into a C# DateTime object. I have found the DateTime.Parse and DateTime.ParseExact methods, but can't really figure out IFormatProvider. Once I have the string from Microsoft SQL, I would expect that, since it was a commonly used format, there would be an easy way, I just can't find it.
>>
>>(I just want to dump the datum from the database into a DateTime object. I don't have to worry about formatting it's output now.)
>
>David,
>
>Do you mean that the field in your SQL Server table is varchar? If it is DateTime, the SQL ADO.NET provider performs the conversion automatically.

Keith,

Thanks. I feel dense.

My data was in SQL as DateTime objects. I pull up a DataSet from the database using a SQL query, get the DataTable, and locate a row in the DataTable by primary key from which I want to extract a C# DateTime object. All I had to do was cast it.

Here is a sample of my relevant code:

 public DateTime getDate(int primaryKey)
 {
    string select = "primaryKey = " + primaryKey;
    DataRow[] dr = this.dataTable.Select(select);
    DateTime theDate = (DateTime)dr[0]["datefield"];
    return theDate;
 }

I was getting "Cannot implicitly convert type 'object' to 'System.DateTime'" compiler errors yesterday. Apparently, my brain healed last night while I slept. Thanks for visiting me in the neurology wing. I appreciate your help. Thanks for taking the time to answer my question.
David S. Alexander
Kettley Publishing
20271 SW Birch Street, 2nd Floor
Newport Beach, CA 92660-1752
Previous
Reply
Map
View

Click here to load this message in the networking platform