OleDB only returns DbNull, what did I do wrong?

I have 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 it should never be null, but each row returned from the database has all the fields set to DBNull! I do not see what I am doing wrong, can anyone shed some light on this?

My connection string:

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

+3
source share
1 answer

For one reason or another, using a column selector *meant mixing columns. Using a specific list eliminates this. I'm still wondering why this could happen.

:

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

Source: https://habr.com/ru/post/1715683/


All Articles