我有以下代码:
// 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
答案 0 :(得分:2)
由于某种原因,使用*
列选择器是混乱的列。使用特定列表可以解决此问题。我仍然很好奇为什么会发生这种情况。
修正版:
OleDbCommand personQuery = new OleDbCommand("SELECT [ID], [Surname], ... FROM [Individual]", _access);