"Unable to convert MySQL date/time value to System.DateTime"这是因为在日期列中有"0000-00-00"数据值,要修正这个问题,你可以把这些数据设为null,或者在连接字符串中设置"Allow Zero Datetime=True"
在链接MySQL的字符串中添加:Convert Zero Datetime=True 和 Allow Zero Datetime=True两个属性;
mysql连接字符串去掉allow zero datetime=true,否则返回的日期会是MySql.Data.Types.MySqlDateTime类型,而不是DataTime
转化失败
未将对象的实例引用到对象
CreateUnsupportedTypeException异常
解决方案:
把链接字符串中的Allow Zero Datetime=true 去掉
// var result = new { dt = ds.Tables[0], count = _totalcount };
//var jsonStr = JsonConvert.SerializeObject(result, Formatting.None);//有时间时,序列化报错,需删除链接字符串中的Allow Zero Datetime=true;
var dic = Utils.Dtb2ArrayList(ds.Tables[0]);
var result = new { dt = dic, count = _totalcount };
JavaScriptSerializer jss = new JavaScriptSerializer();
string str = jss.Serialize(result);
str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/", match =>
{
DateTime dt = new DateTime(1970, 1, 1);
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
dt = dt.ToLocalTime();
return dt.ToString("yyyy-MM-dd HH:mm:ss");
});
参考链接:
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25498312&id=4143024
http://stackoverflow.com/questions/15722761/allow-zero-datetime-mysql-connector-web-config-entity-framework
http://longtianyu1.blog.163.com/blog/static/9981965201328112635472/