日期:2014-05-17  浏览次数:20841 次

Unable to convert MySQL date/time value to System.DateTime
C#操作MyS经常时经常出现Unable to convert MySQL date/time value to System.DateTime的异常。MySql的连接字符串已经加上Allow Zero Datetime=True,仍然无用。语句:update deviceDoorsEmp set IsDownload=1,DownloadTime='2012-02-11 08:34:20' where DeviceID=10 and empID='11590' ,数据库表中DownloadTime属性为‘datetime’,为什么是经常出现异常,如果错误,应该每次都报异常,跪求大神回复。

------解决方案--------------------
应该用Convert.ChangeType转换一下,目标类型是PropertyInfo.PropertyType

if (rowElement[columnElement.ColumnName] == DBNull.Value)
 {
 property.SetValue(element, null, null);
 }
 else
 {
 object rTargetValue=Convert.ChangeType(rowElement[columnElement.ColumnName],property.PropertyType);
 property.SetValue(element,rTargetValue , null);
 }
------解决方案--------------------
引用:
应该用Convert.ChangeType转换一下,目标类型是PropertyInfo.PropertyType

if (rowElement[columnElement.ColumnName] == DBNull.Value)
 {
 property.SetValue(element, null, null);
 }
 else
 {
 object rTargetValue=Convert.ChangeType(rowElement[columnElement.ColumnName],property.PropertyType);
 property.SetValue(element,rTargetValue , null);
 }