OleDB只返回DbNull,我做错了什么?
我有以下代码:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于某种原因,使用
*
列选择器会使列变得混乱。使用特定列表可以解决此问题。我仍然很好奇为什么会发生这种情况。固定版本:
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: