简体   繁体   English

从MS Access数据库中获取Long后,使用OleDbDataReader.GetInt64()时获取System.InvalidCastException

[英]Getting System.InvalidCastException when using OleDbDataReader.GetInt64() after fetching Long from MS Access database

I've been working on a project (with Visual Studio 2013) in which I need to retrieve information from a MS Access 2007 database stored locally. 我一直在研究一个项目(使用Visual Studio 2013),在该项目中我需要从本地存储的MS Access 2007数据库中检索信息。 I'm using OleDb to deal with the connection at the moment. 我目前正在使用OleDb处理连接。 The database has a single table, with several fields. 数据库只有一个表,其中包含多个字段。 I am trying to retrieve the value from SID - which is the primary key, set to Auto-Number as a Long Integer from within Access. 我正在尝试从SID检索值-这是主键,在Access中将它设置为Auto-Number作为Long Integer。

Here's my problem: when the OleDbDataReader has finished executing and I try to retrieve the result (using the GetInt64 method) I get this error: 这是我的问题:当OleDbDataReader完成执行并且尝试检索结果(使用GetInt64方法)时,出现此错误:

An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll System.Data.dll中发生了类型为'System.InvalidCastException'的未处理异常

Additional information: Specified cast is not valid. 附加信息:指定的演员表无效。

This happens regardless of whether I'm assigning the result to a value or not. 无论我是否将结果分配给一个值,这种情况都会发生。 Changing the call to GetInt32 makes it work, but I've no idea why! 更改对GetInt32的调用使其可以工作,但我不知道为什么!

Could anybody shed light on why this is the case? 有人可以阐明为什么会这样吗?

I've searched extensively on here and elsewhere, mostly they suggest the field type is not set to Long Integer in Access, but mine is already, so I don't think that's the issue. 我已经在这里和其他地方进行了广泛的搜索,多数情况下他们认为字段类型未在Access中设置为Long Integer,但是我的已经存在,所以我认为这不是问题。 None of the other solutions seem to apply or work. 其他解决方案似乎都不适用或不起作用。

I've extracted the problem code from my main application, and stripped away all non-essential code, and still get the error, here is the simple version: 我从主应用程序中提取了问题代码,并剥离了所有不必要的代码,但仍然收到错误,这是简单的版本:

        // Set up connection and test query
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Combobulator\\Desktop\\Testing Databases\\Reagents Database\\Reagents.mdb");
        string query = "SELECT SID FROM MolTable WHERE [Chemical Name] = 'Acetyl Chloride'";

        OleDbCommand command = new OleDbCommand(query, connection);

        try {
            connection.Open();
            OleDbDataReader reader = command.ExecuteReader();

            if (!reader.HasRows) {
                System.Console.WriteLine("Returned no rows.");
                Environment.Exit(-1);
            }

            // Find all matching entries
            while (reader.Read()) {
               reader.GetInt64(0);   // This is where the error is thrown
            }

            // Close the reader and connection
            reader.Close();
        } catch (OleDbException e) {
            System.Console.WriteLine("Error: " + e.Errors[0].Message);
        } finally {
            connection.Close();
        }

I'm totally stumped, this is the first time I've had to ask online for help. 我很沮丧,这是我第一次不得不在线寻求帮助。

Thanks for taking the time to read! 感谢您抽出宝贵的时间阅读!

EDIT: I forgot to mention, this isn't just me confusing the length of Long Integer in Access is it? 编辑:我忘了提及,这不仅是我混淆了Access中Long Integer的长度,是吗? I assume that it's 64-bit, please correct me if I'm wrong. 我认为它是64位,如果我写错了,请纠正我。

I forgot to mention, this isn't just me confusing the length of Long Integer in Access is it? 我忘了提一下,这不仅仅是让我混淆Access中Long Integer的长度,是吗? I assume that it's 64-bit, please correct me if I'm wrong. 我认为它是64位,如果我写错了,请纠正我。

Yup, I think that's exactly the problem. 是啊,我想这也正是问题。 For example, from "Field types in MS Access" : 例如,从“ MS Access中的字段类型”中

Integers in Access come in 1, 2 and 4 byte varieties. Access中的整数分为1、2和4个字节。 The single byte number is named Byte (Range 0-255), the two-byte number is named Integer (-32768 to 32767) and then there is the Long Integer (-2 billion to 2 billion). 单字节数字称为Byte(范围0-255),两个字节数字称为Integer(-32768至32767),然后是Long Integer(-20亿至20亿)。

( This site agrees.) 本网站同意。)

MS documentation is thin on the ground, but HansUp found this "Introduction to data types and field properties" that includes: MS文档尚不多,但HansUp发现此“数据类型和字段属性简介”包括:

Field Size 栏位大小

  • ... ...
  • Long Integer — Use for integers that range from -2,147,483,648 to 2,147,483,647. 长整数—用于范围从-2,147,483,648到2,147,483,647的整数。 Storage requirement is 4 bytes. 存储要求为4个字节。

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

相关问题 从数据库填充列表-System.InvalidCastException - Populating list from Database - System.InvalidCastException 使用COM接口时System.InvalidCastException - System.InvalidCastException when using a COM interface System.InvalidCastException:尝试在 C# 中使用实体框架将对象添加到 SQL 数据库时 - System.InvalidCastException: When trying to add an object to a SQL database using Entity Framework in C# MS Dynamics CRM System.InvalidCastException - MS Dynamics CRM System.InvalidCastException SQL 服务器 (ORM) select 来自数据库的数据 - System.InvalidCastException - SQL Server (ORM) select data from database - System.InvalidCastException 从 xib 文件加载视图时获取 System.InvalidCastException - Getting System.InvalidCastException while loading View from a xib file 反序列化 JSON 时,我收到 `System.InvalidCastException` - When deserializing JSON I'm getting `System.InvalidCastException` 使用Sqlite作为数据库创建DataModel的System.InvalidCastException - System.InvalidCastException for DataModel Creation using Sqlite as Database 使用自定义ValueConverter时Select上的System.InvalidCastException - System.InvalidCastException on Select when using custom ValueConverter 在C#中获取System.InvalidCastException - Getting System.InvalidCastException in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM