繁体   English   中英

OleDbDataReader.GetString异常-指定的转换无效

[英]OleDbDataReader.GetString Exception - Specified cast is not valid

当我的C#(.NET 3.5)应用程序尝试访问MS Access 2007数据库时, OleDbReader.GetString()方法引发异常:

指定的演员表无效。

我究竟做错了什么?

OleDbCommand cmd = null;
OleDbDataReader reader = null;
String queryString = "SELECT ids.ENUM_H, bas.[BAS BACnet Object Type/Instance] FROM [OV2 BAS] AS bas INNER JOIN [OV2 RefID] AS ids ON bas.[Ref ID] = ids.[Ref ID]";

this.Open();

try
{
    cmd = new OleDbCommand(queryString, this._conn);
    reader = cmd.ExecuteReader();

    if (!reader.HasRows)
    {
        Exception e = new Exception("Read of mapping table returned no results.");
        throw e;
    }
    else
    {
        while (reader.Read())
        {
            Int32 index;
            String classTypeString = null; // = reader.GetString(reader.GetOrdinal(MappingTable.OBJECT_IDENTIFIER_COLUMN_NAME)).Substring(0, 2);
            int it = reader.GetOrdinal(MappingTable.OBJECT_IDENTIFIER_COLUMN_NAME);
            string st = reader.GetString( it );  // <-- **Exception is thrown here** <--
            st = st.Substring(0,2);
            String classIdString = reader.GetString(reader.GetOrdinal(MappingTable.OBJECT_IDENTIFIER_COLUMN_NAME)).Substring(2);

            index = Convert.ToInt32(classIdString);
            ClassIds[index, 0] = reader.GetString(reader.GetOrdinal("ENUM_H"));
            ClassIds[index, 1] = classTypeString;
        }
    }
}
catch (Exception e)
{
    Console.WriteLine("ERROR: " + e.Message);
    Console.WriteLine(e.ToString());
    throw e;
}

this.Close();

我知道Open()Close()方法有效。 我的查询或我处理结果的方式出了问题。 谢谢。

好的,所以reader.IsDBNull(1)返回true ...,这意味着该字段的特定行中没有数据。

您需要弄清楚这意味着什么,并适当地处理它。 您可能需要修改查询以不包括此类行,或者使用reader.IsDBNull检测此类行并采取适当措施,例如,使用字段的默认值。

有时由于单元格格式而发生。 如果您的单元格格式设置为常规 ,则应将其更改为文本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM