System.FormatException:使用OdbcDataReader.GetFieldValue输入字符串的格式不正确

时间:2015-12-03 13:49:35

标签: vb.net ado.net odbc formatexception

我有一个如下的查询

SELECT NAME, MARKS, COUNT(*) AS TOTAL FROM STUDENTS GROUP BY NAME, MARKS

在迭代OdbcDataReader时遇到System.FormatException,代码和堆栈跟踪如下:

Using dataReader As System.Data.Odbc.OdbcDataReader = CType(cmdODBCCommand.ExecuteReader(), System.Data.Odbc.OdbcDataReader)
    While (dataReader.Read())
        Dim stuName As String = dataReader.GetFieldValue(Of String)(0)
        Dim stuMarks As String = dataReader.GetFieldValue(Of String)(1)
        Dim stuCount As Int32 = dataReader.GetFieldValue(Of Int32)(2)<-- Exception here
        ' The entire database contains only 10 rows still getting the format exception.
        ' Also tried like below
        'Dim x = dataReader.Item("Name")
        'Dim y = dataReader.Item("Marks")
        'Dim z = dataReader.Item("TOTAL") <-- Exception here
        'Dim stuCount As Int64 = dataReader.GetFieldValue(Of Int64)(2)<-- Exception here
    End While
End Using

Strack Trace:

System.FormatException was caught
  HResult=-2146233033
  Message=Input string was not in a correct format.
  Source=mscorlib
  StackTrace:
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
       at System.Data.Odbc.OdbcDataReader.internalGetInt64(Int32 i)
       at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i, TypeMap typemap)
       at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)
       at System.Data.Common.DbDataReader.GetFieldValue[T](Int32 ordinal)
  InnerException: 

是否有人知道这个问题以及如何摆脱这个问题?

我已经尝试了以下网址中提到的所有解决方案:

Input string was not in a correct format. Convert.ToInt32(variable)

Not able to cast string to int. Error msg: Input string was not in a correct format

Input string was not in a correct format?

0 个答案:

没有答案