简体   繁体   English

MySqlDateTime到System.DateTime的转换

[英]MySqlDateTime to System.DateTime conversion

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. 好的,我一般对数据库和C#还是陌生的,但是我正在使用一段代码将数据集数据导出到Excel文件,并且日期/时间格式存在问题。 I'm using the MySQL connector so the rowtype is MySql.Data.Types.MySqlDateTime. 我正在使用MySQL连接器,因此行类型为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? 是否有任何快速方法可以将其转换为System.DateTime,以便可以将其直接插入case语句中?

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. 它抛出一个MySql.Data.Types.MySqlDateTime未处理的异常: 代码项目预先感谢您的任何帮助。

Just call GetDateTime() . 只需调用GetDateTime()即可

What I do, is to format the output in the query to the C# date time format, and parse the reults: 我要做的是将查询中的输出格式化为C#日期时间格式,并解析结果:

MySql Command MySql命令

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

C# 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: UPDATE FROM COMMENT假定appdate列为日期一:

SELECT appref, DATE_FORMAT(appdate, '%Y-%m-%d %T') as 'appdate'  FROM applications WHERE id > 810000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM