简体   繁体   English

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

[英]Unable to convert MySQL date/time value to System.DateTime

I get this error:我收到此错误:

Unable to convert MySQL date/time value to System.DateTime无法将 MySQL 日期/时间值转换为 System.DateTime

while I am trying to fetch the data from a MySQL database.当我试图从 MySQL 数据库中获取数据时。 I have the date datatype in my MySQL database.我的 MySQL 数据库中有日期数据类型。 But while retrieving it into my datatable, it get the error above.但是在将它检索到我的数据表中时,它得到了上面的错误。

How can I fix this?我怎样才能解决这个问题?

您必须将Convert Zero Datetime=True添加到您的连接字符串中,例如:

server=localhost;User Id=root;password=mautauaja;Persist Security Info=True;database=test;Convert Zero Datetime=True

If I google for "Unable to convert MySQL date/time value to System.DateTime" I see numerous references to a problem accessing MySQL from Visual Studio.如果我在 google 上搜索“无法将 MySQL 日期/时间值转换为 System.DateTime”,我会看到许多对从 Visual Studio 访问 MySQL 的问题的引用。 Is that your context?那是你的上下文吗?

One solution suggested is:建议的一种解决方案是:

This is not a bug but expected behavior.这不是错误而是预期的行为。 Please check manual under connect options and set "Allow Zero Datetime" to true, as on attached pictures, and the error will go away.请检查连接选项下的手册并将“允许零日期时间”设置为 true,如附加图片,错误将消失。

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

我添加了Convert Zero Datetime=TrueAllow Zero Datetime=True并且它工作正常

Pull the datetime value down as a string and do a DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyyy", culture, styles);将日期时间值作为字符串下拉并执行DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyyy", culture, styles); You would just need to set the date format up for the date you are returning from the database.您只需要为从数据库返回的日期设置日期格式。 Most likely it's yyyy-MM-dd HH:mm:ss .很可能是yyyy-MM-dd HH:mm:ss At least is is for me.至少对我来说是。

Check here more info on the DateTime.ParseExact在此处查看有关DateTime.ParseExact更多信息

I also faced the same problem, and get the columns name and its types.我也遇到了同样的问题,并获取了列名及其类型。 Then cast(col_Name as Char) from table name.然后从表名转换(col_Name as Char)。 From this way i get the problem as '0000-00-00 00:00:00' then I Update as valid date and time the error gets away for my case.通过这种方式,我得到的问题是 '0000-00-00 00:00:00' 然后我更新为有效日期和时间,错误消失了。

Let MySql convert your unix timestamp to string.让 MySql 将您的 unix 时间戳转换为字符串。 Use the mysql function FROM_UNIXTIME( 113283901 )使用 mysql 函数 FROM_UNIXTIME( 113283901 )

Rather than changing the connection string, you can use the IsValidDateTime property of the MySqlDateTime object to help you determine if you can cast the object as a DateTime .您可以使用MySqlDateTime对象的IsValidDateTime属性来帮助您确定是否可以将对象转换为DateTime ,而不是更改连接字符串。

I had a scenario where I was trying to load data from an "UpdateTime" column that was only explicitly set when there was an update to the row (as opposed to the InsertedTime which was always set).我有一个场景,我试图从“UpdateTime”列加载数据,该列仅在对行进行更新时才显式设置(而不是始终设置的 InsertedTime)。 For this case, I used the MySqlDataReader.GetMySqlDateTime method like so:对于这种情况,我使用了MySqlDataReader.GetMySqlDateTime方法,如下所示:

using (MySqlDataReader reader = await MySqlHelper.ExecuteReaderAsync(...))
{
    if (await reader.ReadAsync())
    {
        DateTime? updateTime = reader.GetMySqlDateTime("UpdateTime").IsValidDateTime ? (DateTime?)reader["UpdateTime"] : null;
    }
}

在 Stimulsoft 报告中,将此参数添加到连接字符串中(右键单击数据源-> 编辑)

Convert Zero Datetime=True;

You can make the application fully compatible with the date and time that is used by MySql.您可以使应用程序与 MySql 使用的日期和时间完全兼容。 When the application runs at runtime provide this code.当应用程序在运行时运行时提供此代码。 First Go to the Application Events.首先转到应用程序事件。 In the list of tools在工具列表中

  1. Go to the project前往项目
  2. Project Properties项目属性
  3. Select the Application tab选择应用程序选项卡
  4. View Application Events查看应用程序事件

This will open a new file.这将打开一个新文件。 This file contains code used at the start of the application.此文件包含在应用程序启动时使用的代码。

Write this code in that new file:在该新文件中写入以下代码:

 Partial Friend Class MyApplication

    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
        My.Application.ChangeCulture("en")
        My.Application.ChangeUICulture("en")
        My.Application.Culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
        My.Application.Culture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd"
        My.Application.Culture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
        My.Application.Culture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
    End Sub


End Class

if "allow zero datetime=true" is not working then use the following sollutions:-如果“允许零日期时间=真”不起作用,则使用以下解决方案:-

Add this to your connection string: "allow zero datetime=no" - that made the type cast work perfectly.将此添加到您的连接字符串中: “allow zero datetime=no” - 这使得类型转换工作完美。

When you're using EF edmx with MySql, please review the Precision attribute in the entitytype.当您将 EF edmx 与 MySql 一起使用时,请查看实体类型中的 Precision 属性。

<EntityType Name="table">
  <Key>
    <PropertyRef Name="Id" />
  </Key>
  <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
  <Property Name="date_field" Type="datetime" Precision="0" Nullable="false" />
</EntityType>

If you made changes in the datetime field, it could be possible you're missing this attribute in the property.如果您在日期时间字段中进行了更改,则您可能在属性中缺少此属性。

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

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