Why is DataTable faster than DataReader

前端 未结 4 1640
轻奢々
轻奢々 2020-11-30 06:12

So we have had a heated debate at work as to which DataAccess route to take: DataTable or DataReader.

DISCLAIMER I am on the DataReader side and the

4条回答
  •  北海茫月
    2020-11-30 06:44

    SqlDataAdapter.Fill calls SqlCommand.ExecuteReader with CommandBehavior.SequentialAccess set. Maybe that's enough to make the difference.

    As an aside, I see your IDbReader implementation caches the ordinals of each field for performance reasons. An alternative to this approach is to use the DbEnumerator class.

    DbEnumerator caches a field name -> ordinal dictionary internally, so gives you much of the performance benefit of using ordinals with the simplicity of using field names:

    foreach(IDataRecord record in new DbEnumerator(reader))
    {
        artifactList.Add(new ArtifactString() {
            FormNumber = (int) record["FormNumber"],
            FormOwner = (int) record["FormOwner"],
            ...
        });
    }
    

    or even:

    return new DbEnumerator(reader)
        .Select(record => new ArtifactString() {
            FormNumber = (int) record["FormNumber"],
            FormOwner = (int) record["FormOwner"],
            ...
          })
        .ToList();
    

提交回复
热议问题