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

发布于 2024-12-13 11:49:31 字数 1641 浏览 1 评论 0原文

当我的 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() 方法可以工作。我的查询或处理结果的方式有问题。谢谢。

When my C# (.NET 3.5) application attempts to access a MS Access 2007 database the OleDbReader.GetString() method is throwing an exception:

Specified cast is not valid.

What am I doing wrong?

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();

I know that the Open() and Close() methods work. Something is wrong with my query, or the way I am processing the results. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久隐师 2024-12-20 11:49:31

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

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

Okay, so reader.IsDBNull(1) is returning true... which means there's no data in that particular row for that field.

You need to work out what that means, and handle it appropriately. You may want to modify the query to not include such rows, or use reader.IsDBNull to detect such rows and act appropriately, e.g. using a default value for the field.

左岸枫 2024-12-20 11:49:31

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

Sometimes it happens because of Cell format. If your Cell format is set to General you should change it to Text .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文