Skip to content

MySqlDateTime should implement IConvertible #798

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

Closed
fish-li opened this issue Apr 14, 2020 · 2 comments
Closed

MySqlDateTime should implement IConvertible #798

fish-li opened this issue Apr 14, 2020 · 2 comments
Assignees

Comments

@fish-li
Copy link

fish-li commented Apr 14, 2020

The same testcase,
MySqlConnector is failed, MySql.Data works!

[TestMethod]
public void Test_DateTime_DataType()
{
    string connectionString1 = "Server=FishDbServer;Database=MyNorthwind;Uid=user1;Pwd=qaz1@wsx";
    string connectionString2 = "Server=FishDbServer;Database=MyNorthwind;Uid=user1;Pwd=qaz1@wsx;Allow Zero Datetime=True;Convert Zero Datetime=True;";

    object result1, result2;

    using(MySqlConnection conn1 = new MySqlConnection(connectionString1)){
        conn1.Open();

        MySqlCommand command1 = conn1.CreateCommand();
        command1.CommandText = "select now()";
        result1 = command1.ExecuteScalar();
    }


    using( MySqlConnection conn2 = new MySqlConnection(connectionString2) ) {
        conn2.Open();

        MySqlCommand command2 = conn2.CreateCommand();
        command2.CommandText = "select now()";
        result2 = command2.ExecuteScalar();
    }

    Assert.AreEqual(typeof(DateTime), result1.GetType());

    // set "Allow Zero Datetime=True", result is MySqlDateTime
    Assert.AreEqual(typeof(MySql.Data.Types.MySqlDateTime), result2.GetType());


    DateTime time1 = (DateTime)Convert.ChangeType(result1, typeof(DateTime));

    // MySqlConnector throw error: System.InvalidCastException: Object must implement IConvertible.
    // MySql.Data, It works!
    DateTime time2 = (DateTime)Convert.ChangeType(result2, typeof(DateTime));

    // MySqlConnector
    // public struct MySqlDateTime : IComparable

    // MySql.Data
    // public struct MySqlDateTime : IMySqlValue, IComparable, IConvertible

    Assert.AreEqual(time1, time2);
}
@bgrainger
Copy link
Member

Implement using IConvertible.ToDateTime() => GetDateTime();; all other conversions should throw InvalidCastException.

@bgrainger
Copy link
Member

Added in 0.64.0.

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

No branches or pull requests

2 participants