Description:
Hi,
Using the MySQL .NET Connector (v. 5.0.7) the code attached will generate an exception. I know about the "Convert Zero DateTime" and/or "Allow Zero DateTime" options, but my point is, that you should be able to retrieve the object (as a MySQLDateTime value?) and deal with it appropriately without relying on a certain connection string option being passed. I would not have a problem if a later cast/conversion to DateTime resulted in the same Exception being thrown (as the code in DateTime IConvertible.ToDateTime would do).
The code will also throw an exeption if you attempt to use "dr.IsDBNull(i)" instead of the indexer as this also relies on field retrival.
How to repeat:
class Program {
static void Main(string[] args) {
using(MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;UID=someone;PWD=something;PORT=3306;"))
using(MySqlCommand cmd = new MySqlCommand("", con)) {
cmd.Connection.Open();
cmd.CommandText =
@"CREATE TABLE IF NOT EXISTS `test`.`test` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`datetime` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO `test`.`test`(datetime) VALUES ('0000-00-00 00:00:00')";
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT * FROM `test`.`test`";
using(IDataReader dr = cmd.ExecuteReader()) {
while(dr.Read()) {
for(int i = 0; i < dr.FieldCount; i++) {
object o = dr[i];
}
}
}
}
}
}
Suggested fix:
Modify code in GetValue from
if (!dt.IsValidDateTime && connection.Settings.ConvertZeroDateTime)
return DateTime.MinValue;
else if (connection.Settings.AllowZeroDateTime)
return val;
else
return dt.GetDateTime();
to
if(!dt.IsValidDateTime)
{
if(connection.Settings.ConvertZeroDateTime)
return DateTime.MinValue;
else
return val;
}
else
{
return dt.GetDateTime();
}