Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When connection strings contains "Allow Zero Datetime=True" MySqlConnector fails to read dates from database #597

Closed
MiloszKrajewski opened this issue Dec 12, 2018 · 4 comments
Assignees
Labels

Comments

@MiloszKrajewski
Copy link

@MiloszKrajewski MiloszKrajewski commented Dec 12, 2018

Versions

Runtime: NET Framework 4.7.1
MySqlConnector: 0.48.1
MySQL: 5.7.21 (docker@ubuntu)

Symptoms

When connection strings contains "Allow Zero Datetime=True" MySqlConnector fails to read dates from database, even something like select now().

Throws:

Test method x.AdHocTests.TryWithAllowZeroDateTime threw exception: 
System.InvalidCastException: Specified cast is not valid.
    at MySql.Data.MySqlClient.MySqlDataReader.GetDateTime(Int32 ordinal) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 200
   at x.AdHocTests.TryWithAllowZeroDateTime(Boolean allowZeroDateTime)

Code

[TestClass]
public class AdHocTests
{
    [TestMethod]
    [DataRow(false)]
    [DataRow(true)]
    public void TryWithAllowZeroDateTime(bool allowZeroDateTime = false)
    {
        var connectionString =
            "server=localhost;port=3306;database=x;uid=x;password=x";
        if (allowZeroDateTime)
            connectionString += ";Allow Zero Datetime=True";
        var queryText = "select now()";

        using (var connection = new MySqlConnection(connectionString))
        using (var command = new MySqlCommand 
            { Connection = connection, CommandText = queryText })
        {
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                reader.Read();
                var _ = reader.GetDateTime(0);
            }
        }
    }
}

Expected results

Test passes in both cases

Actual results

Test passes for false (no "Allow Zero Datetime") but fails for true (with "Allow Zero Datetime").

@bgrainger
Copy link
Member

@bgrainger bgrainger commented Dec 12, 2018

This was a regression introduced in d35322f.

@bgrainger bgrainger self-assigned this Dec 12, 2018
@bgrainger bgrainger added the bug label Dec 12, 2018
@bgrainger
Copy link
Member

@bgrainger bgrainger commented Dec 12, 2018

Fixed in 0.48.2.

Note that calling MySqlDataReader.GetDateTime() when AllowZeroDateTime=True is potentially a bad idea anyway, as the column can contain values (such as 0000-00-00) that aren't expressible as a System.DateTime value.

You may wish to write instead:

var mySqlDateTime = reader.GetMySqlDateTime(0);
if (mySqlDateTime.IsValidDateTime)
{
    var dateTime = mySqlDateTime.GetDateTime();
    // use dateTime...
}

@MiloszKrajewski
Copy link
Author

@MiloszKrajewski MiloszKrajewski commented Dec 12, 2018

Thanks. Impressive timing!

@bgrainger
Copy link
Member

@bgrainger bgrainger commented Dec 12, 2018

Thanks for a well-written bug report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants