简体   繁体   中英

MySQL Connector & date type 0000-00-00

Please sorry for my English http://dev.mysql.com/doc/connector-net/en/connector-net-programming-datetime-null.html Apparently .NET can't take 0000-00-00 date. Im try to use code from page in my code:

 for (int i = 0; i < dataReader.FieldCount; i++) list.Add(new List<string>());

            DateTime myTime;
            try
            {
                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        if (tablename == "orders")
                        {
                            if (dataReader.IsDBNull(dataReader.GetOrdinal("sell_date"))) MessageBox.Show("111");
                        }
                        list[i].Add(dataReader[i] + "");
                    }
                }
            }
             catch(Exception e)
            {
                 MessageBox.Show("Ошибка: " + e.GetType().ToString() + "\nСообщение: " +e.Message.ToString());
            }

Same error: Error: MySql.Data.Types.MySqlСonversionException Message: Unable to convert MySQL date/time value to System.DateTime

Why MessageBox.Show("111"); don't work? Field sell_date is contain 0000-00-00

0000-00-00 is not a valid DateTime value

To resolve this issue you have to configure the MySQL .NET Connector ( here you can find all parameters ).

You have two ways:

  1. Add AllowZeroDateTime=true in the connection string. This allow you to manage 0000-00-00 value using a special object called MySqlDateTime. You can get this type of object from the MySqlDataReader
  2. Add ConvertZeroDateTime=true in the connection string. In this way all 0000-00-00 values will be converted to DateTime.MinValue

I prefer the second.... I hope this will be useful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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