Mysql Connector Types
How do you deal with the types Mysql Connector returns? It returns an object but开发者_如何学运维 I have to find out the type it returns before I can use it. I tried (int)reader["SUM(column)"] and it turned out that the sum was actually a Decimal so it caused an exception. It causes exceptions whenever I try going from an object to a type that it is not causing me to have to figure out each type before I can use it.
MySqlClient.MySqlDataReader has a method GetDataTypeName(int i), that implements IDataRecord.GetDataTypeName(Int32), where 'i' is the zero based column index. The string values will correspond to the MySqlDbType Enumeration member names.
If you have control over the database scripting the safest option is to specifically cast the Query or Procedure data type in the query.
hey you are doing database development, you got to know each result type before you can use it in your code. It's absolutely normal to know beforehand what is the result of every query or stored procedure we call from .NET.
精彩评论