Skip to content
New issue

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

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

Already on GitHub? # to your account

Row.GetString(int Ordinal) throws exception when columnType can not be directly cast to string. #406

Closed
robertbeuligmann opened this issue Nov 29, 2017 · 3 comments

Comments

@robertbeuligmann
Copy link

Due to the way MySqlConnector is casting values from GetValue(int) it throws

"Unable to cast object of type 'System.Int64' to type 'System.String'."

when trying to convert a LongLong columnType when calling MySqlConnector.Core.Row.GetString()

Mysql.Data will attempt to convert any row to a string when calling Mysql.Data.DataReader.GetString(int) by calling the final values .ToString() method.

@robertbeuligmann robertbeuligmann changed the title Row.GetString(int Ordinal) throws exception when columnType can not be directly cast to string. Row.GetString(int Ordinal) throws exception when columnType can not be directly cast to string. Nov 29, 2017
@bgrainger
Copy link
Member

Yes, currently MySqlConnector only allows GetString for TEXT etc. columns. A workaround is to call GetValue(int).ToString() to get a (culture-sensitive) rendering.

@bgrainger
Copy link
Member

I added numeric/string conversion tests here: mysql-net/AdoNetApiTest@3d8dbc6

While MySql.Data does perform implicit conversions to string for numeric types, SqlClient and npgsql do not.

I've decided to deliberately use the more strict behaviour because implicit conversions to string are a notorious source of locale-dependent bugs, and because GetValue(x).ToString() is an available workaround.

@bgrainger
Copy link
Member

MySql.Data 8.3.0 now deprecates the implicit conversion to string for non-textual columns:

Calling the MySqlDataReader.GetString() method now throws an InvalidCastException exception if the associated column is not a string type.

The OldGetStringBehavior connection option was added to allow (by setting it to true) restoring previous behavior by logging a deprecation warning instead of throwing the exception. This option defaults to false.

This option will be removed in the near future, potentially in version 9.0.0.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

No branches or pull requests

2 participants