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

后端 未结 9 861
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 22:04

I am using ibatis and C#. i get a result from a select query that has CreatedDate as one of the field. The Datatype of CreatedDate in Mysql is Date. I assign the result set

相关标签:
9条回答
  • 2020-12-08 22:27

    One thing who works too is changing your reader action. I was having this problem when I wrote

    string myvar = reader.GetString(0);
    

    Then I used to write this

    object myvar = reader.GetValue(0);
    

    And no more error.

    0 讨论(0)
  • 2020-12-08 22:30

    You need to do the simple change with the connection string you added for MySql database. i.e CONVERT ZERO DATETIME = TRUE

    <add name="NAMEOFYOURCONNECTIONSTRING" connectionString="server's HOSTNAMEorIP(i.e Localhost or IP address);user id=USER_ID(i.e User ID for login Database);Pwd=PASSWORD(i.e User Password for login Database);persistsecurityinfo=True;database=NAMEOFDATABASE;Convert Zero Datetime=True" providerName="MySql.Data.MySqlClient"/>
    
    0 讨论(0)
  • 2020-12-08 22:35

    I solved my problem by setting the column's default value as null data rather than using 0000-00-00 00:00:00:

    update table set date = null
    
    0 讨论(0)
  • 2020-12-08 22:41

    This worked for me:

    SELECT 
        IF(tb.Date1 = '0000-00-00 00:00:00', NULL, tb.Date1) AS ValidDate
    FROM MyTable AS tb
    
    0 讨论(0)
  • 2020-12-08 22:42

    The problem in the format, actually mysql have a different format (yyyy-mm-dd) for the date/time data type and to solve this problem use the mysql connector library for .net from here http://dev.mysql.com/downloads/connector/net/ it will give other data type for the date/time called MysqlDateTime

    or you can format the date/time data in your sql statement using DATE_FORMAT(date,format) you can get more details from here http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

    but i don't recommend this because you will loose the power of date/time data type (for example you can't compare) because now you will convert it to string but i think it will be useful in the reports

    0 讨论(0)
  • 2020-12-08 22:45

    This issue happened to me when a date column in my table had been set to 0 rather than null - there was a bug in my code that.

    Looking at the table I could see that the field had been set to a date of 000-00-00 - once I had reset the offending field to null the issue went away.

    0 讨论(0)
提交回复
热议问题