OleDB只返回DbNull,我做错了什么?

发布于 2024-08-02 16:34:21 字数 897 浏览 1 评论 0原文

我有以下代码:

// personCount = 7291; correct value
int personCount = (int)new OleDbCommand("SELECT COUNT(*) AS [count] FROM [Individual]", _access).ExecuteScalar();
List<Person> people = new List<Person>();

OleDbCommand personQuery = new OleDbCommand("SELECT * FROM [Individual]", _access);

using (OleDbDataReader personReader = personQuery.ExecuteReader())
{
    int curPerson;

    while (personReader.Read())
    {
        curPerson++;
        // This runs several times
        if (personReader.IsDBNull(0)) continue;
        // [snip] create a new Person and add it to people
    }
    // at this point, curPerson == 7291 but the list is empty.
}

这是我的确切代码。字段 0 是主键,因此永远不应该为 null,但从数据库返回的每一行都将所有字段设置为 DBNull!我看不出我做错了什么,有人能解释一下吗?

我的连接字符串是:

Provider=Microsoft.Jet.OLEDB.4.0;数据源=C:\path\to\database.mdb

I've got the following code:

// personCount = 7291; correct value
int personCount = (int)new OleDbCommand("SELECT COUNT(*) AS [count] FROM [Individual]", _access).ExecuteScalar();
List<Person> people = new List<Person>();

OleDbCommand personQuery = new OleDbCommand("SELECT * FROM [Individual]", _access);

using (OleDbDataReader personReader = personQuery.ExecuteReader())
{
    int curPerson;

    while (personReader.Read())
    {
        curPerson++;
        // This runs several times
        if (personReader.IsDBNull(0)) continue;
        // [snip] create a new Person and add it to people
    }
    // at this point, curPerson == 7291 but the list is empty.
}

This is my exact code. Field 0 is the primary key, so should never be null, but every single row being returned from the database has all the fields set to DBNull! I can't see what I'm doing wrong, can anyone shed some light on this?

My connection string is:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\path\to\database.mdb

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

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

发布评论

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

评论(1

○闲身 2024-08-09 16:34:21

由于某种原因,使用 * 列选择器会使列变得混乱。使用特定列表可以解决此问题。我仍然很好奇为什么会发生这种情况。

固定版本:

OleDbCommand personQuery = new OleDbCommand("SELECT [ID], [Surname], ... FROM [Individual]", _access);

For one reason or another, using the * column selector was jumbling columns. Using a specific list fixes this. I'm still curious as to reasons why this might happen.

Fixed version:

OleDbCommand personQuery = new OleDbCommand("SELECT [ID], [Surname], ... FROM [Individual]", _access);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文