I am using the following code (Variant DataReader):

public DataTable dtFromDataReader(list<String> lstStrings)
{

    OleDBConn_.Open();
    using (OleDbCommand cmd = new OleDbCommand())
    {

        DataTable dt = new DataTable();
        OleDbDataReader reader = null;
        cmd.Connection = OleDBConn_;
        cmd.CommandText = "SELECT * from TableX where SUID=?";

        foreach (String aString in lstStrings)
        {
            cmd.Parameters.AddWithValue("?", aNode.SUID);
            reader = cmd.ExecuteReader();

            if (reader != null)
                dt.Load(reader);
            cmd.Parameters.Clear();
        }
        return dt;
    }
}

and compare it to (Variant DataAdapter):

public DataTable dtFromDataAdapter(list<String> lstStrings)
{
    dt = new DataTable();

    foreach (string aString in lstStrings)
    {
        sOledb_statement = String.Concat("SELECT * FROM TableX where SUID='", aString, "'");
        OleDbDataAdapter oleDbAdapter;
        using (oleDbAdapter = new OleDbDataAdapter(sOledb_statement, OleDBConn_))
        {
            GetOleDbRows = oleDbAdapter.Fill(dt);
        }
    }
}

When i connect to an offline database (microsoft access) my reading time is (~1.5k retrieved items):

  • DataReader 420 ms
  • DataAdapter 5613 ms

When reading from oracle server (~30k retrieved items):

  • DataReader 323845 ms
  • DataAdapter 204153 ms (several tests, times do not change much)

Even changing the order of the commands (dataadapter before datareader) didn't change much (i thought that there may have been some precaching..).

I thought DataTable.Load should be somewhat faster than DataAdapter.Fill?

And i still believe, even though i see the results, that it should be faster. Where am i losing my time? (There are no unhandled exceptions..)

有帮助吗?

解决方案

Your comparison isn't really an Adapter vs DataReader with the way you have the code setup. You are really comparing the Adapter.Fill vs DataTable.Load methods.

The DataReader would normally be faster on a per-record basis because you would be traversing the records one at a time and can react accordingly when you read each record.

Since you are returning a DataTable in both instances, the Adapter.Fill method would probably be the optimal choice to use. It was designed to do just that.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top