C# 在 VS2010 中无法将 MySQL 日期/时间值转换为 System.DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17002522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:20:04  来源:igfitidea点击:

Unable to convert MySQL date/time value to System.DateTime in VS2010

c#asp.netmysqlasp.net-mvc

提问by nielsv

when I want to load data from my mysql database I always get this error:

当我想从我的 mysql 数据库加载数据时,我总是收到这个错误:

Unable to convert MySQL date/time value to System.DateTime

无法将 MySQL 日期/时间值转换为 System.DateTime

This is my connectionstring:

这是我的连接字符串:

<add name="gdmwebsiteEntities" 
     connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=root;database=gdmwebsite&quot;" 
     providerName="System.Data.EntityClient" />

I've already added "Convert Zero Datetime=True" add the end of my connectionstring but nothing changed.

我已经在我的连接字符串的末尾添加了“转换零日期时间=真”,但没有任何改变。

    <add name="gdmwebsiteEntities" 
     connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=root;database=gdmwebsite&quot;
     Convert Zero Datetime=True
      Allow Zero Datetime=True"
     providerName="System.Data.EntityClient" />

Is there another method for fixing this?

有没有其他方法可以解决这个问题?

采纳答案by Soner G?nül

Set both of these configurations in your connection string.

在您的连接字符串中设置这两个配置。

"Convert Zero Datetime=True"
"Allow Zero Datetime=True"

http://bugs.mysql.com/bug.php?id=26054

http://bugs.mysql.com/bug.php?id=26054

Please check manual under connect options and set "Allow Zero Datetime" to true, as on attached pictures, and the error will go away.

请检查连接选项下的手册并将“允许零日期时间”设置为 true,如附加图片,错误将消失。

Check this also: http://mdid.org/mdidwiki/index.php?title=Unable_to_convert_MySQL_date/time_value_to_System.DateTime_exception

也检查一下:http: //mdid.org/mdidwiki/index.php?title=Unable_to_convert_MySQL_date/time_value_to_System.DateTime_exception

To fix this problem, either replace all invalid timestamps with NULL or a valid timestamp, or add Allow Zero Datetime=true

要解决此问题,请将所有无效时间戳替换为 NULL 或有效时间戳,或添加 Allow Zero Datetime=true