0

I've been successfully using the following code to load data from an ODBC connection into a C# DataTable for some time without issues:

public static DataTable ExecuteSqlSelect(string sql, string connectionString)
        {
            var result = new DataTable();
            using (var connection = new OdbcConnection(connectionString))
            {
                connection.Open();

                var command = connection.CreateCommand();
                command.CommandText = sql;
                var dbReader = command.ExecuteReader();
                result.Load(dbReader);

                connection.Close();
            }

            return result;
        }

However, I now have a MySql table with a column of type JSON. When I try to load data from that table with that method, I get the following error:

Unknown SQL type - 0.

I assume this is because the JSON type is not recognized by C#'s DataTable. Is this correct? And more importantly: is there a solution/workaround for this?

EDIT: I'm not trying to convert a JSON string to a DataTable, as a commenter suggested... I'm trying to load a SQL table that contains a columns of MySQL type 'JSON' into a DataTable. I don't need JSON parsing, it would be fine if I just got the raw JSON string into the DataTable.

EDIT 2: both MySql and the ODBC connector are the latest version: 8.0.11

15
  • 1
    By any chance, is your code affected by this bug: bugs.mysql.com/bug.php?id=12514 Commented Jun 19, 2018 at 15:03
  • Possible duplicate of How to convert json into datatable? Commented Jun 19, 2018 at 15:15
  • @MoStar not a duplicate, I've edited the question to explain it better Commented Jun 19, 2018 at 15:29
  • You should provide the version of MySQL database and MySQL Connector/ODBC that you are using. Would you add this information on your question too? Commented Jun 19, 2018 at 15:33
  • 1
    My suggestion comes from this link : forums.mysql.com/read.php?37,650722,651027#msg-651027 - I couldn't find any reference to JSON datatype in the Release Notes. I don't know if modifying the query to cast the column as char/varchar is possible - if so that may be a workaround for you. Commented Jun 19, 2018 at 15:43

2 Answers 2

4

Thanks to PaulF's suggestion in the comments, I was able to solve this. Since the ODBC driver doesn't properly support JSON, you have to cast the column to text, directly in the query. So if before I had:

SELECT col1, col2, jsonCol FROM table;

I replaced it with:

SELECT col1, col2, CAST(jsonCol as CHAR(256)) as jsonCol FROM table;

this converts the column to normal text and it is then correctly loaded into the DataTable.

Sign up to request clarification or add additional context in comments.

Comments

2

I think it is more an issue of the JSON datatype not being supported by the ODBC driver - my suggestion coming from this link : https://forums.mysql.com/read.php?37,650722,651027#msg-651027 - I couldn't find any reference to JSON datatype in the Release Notes.

You could try casting the column to char/varchar as a workaround for you.

2 Comments

Thanks, accepted this because it's you that pointed me in the right direction, if anyone needs the full solution I've posted it here: stackoverflow.com/a/50932457/300741
You beat me to moving my comments to an answer - it is good that you have posted the updated query with the cast shown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.