.NET DataTable skips rows on Load(DataReader)

After fiddling around quite a bit I found that the DataTable.Load method expects a primary key column in the underlying data. If you read the documentation carefully, this becomes obvious, although it is not stated very explicitly.

If you have a column named “id” it seems to use that (which fixed it for me). Otherwise, it just seems to use the first column, whether it is unique or not, and overwrites rows with the same value in that column as they are being read. If you don’t have a column named “id” and your first column isn’t unique, I’d suggest trying to explicitly set the primary key column(s) of the datatable before loading the datareader.

Leave a Comment