views:

104

answers:

2

Ok I'm very new to databases and C# in general, but I'm using a piece of code that exports dataset data to an Excel file, and its taking issue with the date/time format. I'm using the MySQL connector so the rowtype is MySql.Data.Types.MySqlDateTime. Is there any quick way to convert it into System.DateTime so I can slot it straight into the case statement?

Here's a link to the code I used, I copied it verbatim so I've not copied and pasted it here. It throws a MySql.Data.Types.MySqlDateTime not handled exception: Code Project Thanks in advance for any help.

A: 

What I do, is to format the output in the query to the C# date time format, and parse the reults:

MySql Command

select DATE_FORMAT(NOW(), '%Y-%m-%d %T') as 'Date';

C#

DateTime x;
DateTime.tryParse(results["Date"], out x);

If you need more explanation just comment and ask :)

UPDATE FROM COMMENT Presuming that the appdate column is the date one:

SELECT appref, DATE_FORMAT(appdate, '%Y-%m-%d %T') as 'appdate'  FROM applications WHERE id > 810000
Psytronic
Haha yes please! My original MySQL query is (SELECT appref,appdate FROM applications WHERE id > 810000), how would I put that into my select statement? Thanks!
bowsa
Spot on! Thanks very much :)
bowsa
-1. This is a poor-performing alternative to simply reading the documentation. You're requiring the database engine to waste time formatting datetimes as strings (for every row of the result set, which could be huge), just so the client code can waste even more time parsing the strings back to datetimes. It's actually **easier** (in terms of code) to do the better thing and just call GetDateTime() on the MySqlDateTime object.
P Daddy
@P DaddyThis was the way I was told how to do it. When I started using the mysql connector there wasn't any documentation, if I recall correctly. But thanks for letting me know.
Psytronic
@P Daddy - The table has around 258,000 rows, and it performs fine. Although I'm not using the parse part as I didn't need it.
bowsa
Well, you can lead a horse to water...
P Daddy
Or, as you did, you can vaguely tell a horse where the water is, knowing that the horse is in fact a horse, and as such does not understand English. :)
bowsa
I linked to the proper function in my answer.
P Daddy
A: 

Just call GetDateTime().

P Daddy