c# - 从 MS Access 数据库获取 Long 后使用 OleDbDataReader.GetInt64() 时出现 System.InvalidCastException

标签 c# exception ms-access-2007 oledb

我一直在开发一个项目(使用 Visual Studio 2013),在该项目中我需要从本地存储的 MS Access 2007 数据库中检索信息。我现在正在使用 OleDb 来处理连接。该数据库有一个表,包含多个字段。我正在尝试从 SID 检索值 - 这是主键,在 Access 中设置为“自动编号”作为长整型。

这是我的问题:当 OleDbDataReader 完成执行并且我尝试检索结果(使用 GetInt64 方法)时,我收到此错误:

An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

Additional information: Specified cast is not valid.

无论我是否将结果分配给一个值,都会发生这种情况。 将调用更改为 GetInt32 可以使其工作,但我不知道为什么!

有人能解释为什么会出现这种情况吗?

我在这里和其他地方进行了广泛的搜索,大多数人都认为 Access 中的字段类型没有设置为长整型,但我的已经设置为长整型,所以我认为这不是问题。其他解决方案似乎都不适用或有效。

我已经从主应用程序中提取了问题代码,并删除了所有非必要的代码,但仍然收到错误,这是简单的版本:

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

我完全被难住了,这是我第一次不得不在线寻求帮助。

感谢您花时间阅读!

编辑:我忘了提及,这不仅仅是我混淆了 Access 中长整型的长度,是吗?我假设它是64位的,如果我错了,请纠正我。

最佳答案

I forgot to mention, this isn't just me confusing the length of Long Integer in Access is it? I assume that it's 64-bit, please correct me if I'm wrong.

是的,我认为这正是问题所在。例如,来自"Field types in MS Access" :

Integers in Access come in 1, 2 and 4 byte varieties. 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).

(This site同意。)

MS 文档很少,但 HansUp 发现了这个 "Introduction to data types and field properties"其中包括:

Field Size

  • ...
  • Long Integer — Use for integers that range from -2,147,483,648 to 2,147,483,647. Storage requirement is 4 bytes.

关于c# - 从 MS Access 数据库获取 Long 后使用 OleDbDataReader.GetInt64() 时出现 System.InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265006/

相关文章:

c# - MethodBody.LocalVariables 计数令人困惑

c# - 为什么 C++ 和 C# 的编译速度相差很大?

c# - 自定义中间件不处理调试运行 ASP.NET CORE 的异常

ms-access - 微软 Access 2007 : Specify Max Length for TextBox on a Form

ms-access - 有没有比 DLookup 更快的替代方案?

c# - 按字段分组

c# - 类型 'System.Int32' 的表达式不能用于方法 'System.Object' 的类型 'Boolean Equals(System.Object)' 的参数

java.util.InputMismatchException : For input string: "2147483648"

java - 处理异常的 throws 语句——Java

ms-access - 根据组合框选择过滤列表框