首页 > 技术文章 > Unable to convert MySQL date/time value to System.DateTime问题解决方案

congcongdi 2018-08-03 11:40 原文

原因:可能是该字段(date/datetime)的值默认缺省值为:0000-00-00/0000-00-00 00:00:00,这样的数据读出来转换成System.DateTime时就会有问题;

解决办法:在连接字符串中添加Allow Zero Datetime=True

private static String mysqlcon = "Database=.;Data Source=127.0.0.1;User Id=root; password=****;CharSet=utf8;Allow Zero Datetime=True";

若需要对此列数据进行判断处理,我是这样写的:

 if (dts.Rows[i]["StartDate"].ToString().Equals("0000-00-00 00:00:00") || dts.Rows[i]["EndDate"].ToString().Equals("0000-00-00 00:00:00"))

 

推荐阅读