none
Does OleDbDataAdapter.Fill() Fill DataTable with Actual Data?

    Question

  • I'm creating objects from a DataTable, and I'm finding that the process takes a lot more time over a network connection than it does locally. The performance is basically the same whether I use a DataTable or Reader.

    The OleDbDataAdapter.Fill(dataTable) executes relatively quickly whether over the network or local, however there is a huge time difference with object construction.

    I assumed that OleDbDataAdapter.Fill(dataTable) actually filled the DataTable with the data from the database, but I'm guessing this is not the case since the construction time varies so widely while the OleDbDataAdapter.Fill(dataTable) method executes fairly quickly.

    Can someone verify that the OleDbDataAdapter.Fill(dataTable) method does not actually populate any data, and also perhaps offer any advice on a more efficient way of constructing objects from data housed in a database. Thanks, Matt

                    var oda = new OleDbDataAdapter(query, con);
                    var reportItemsDataTable = new DataTable();
                    oda.Fill(reportItemsDataTable);
    
                    foreach (var row in reportItemsDataTable.Rows)
                    {
                        var itemRow = (DataRow)row;
                        ReportItemUIElements.Add(new ReportItemUIElement(new ReportItem(itemRow)));
                    }
    

    Sunday, April 2, 2017 10:53 AM

All replies

  • >Can someone verify that the OleDbDataAdapter.Fill(dataTable) method does not actually populate any data

     DataAdapters do actually load the data from the database into the DataTable in memory, which you can easily verify by examining the DataTable after running Fill.

    >advice on a more efficient way of constructing objects from data housed in a database

    Profile your code to see why it takes so long.  Start with the loading the DataTable to separate the database access from the object construction.

    David


    Microsoft Technology Center - Dallas
    My blog

    Sunday, April 2, 2017 2:58 PM
  • Thanks for that. Very helpful. I had an idiotic setter that called a method that wrote a value to the database, and I was using the Setter in the constructor rather than the private field. Learn something everyday.
    Sunday, April 2, 2017 6:52 PM
  • A DataAdapter is a class which uses many methods from System.Data in other classes. 

    One of those is the DBDataReader which gets a so called ResultSet

    As everything is the DataAdapter not filling something, it creates a DataTable which is a bunch of references (even to itself therefore it looks so big in Quick View). Those references are all referencing objects in the ResultSet (or on whatever place as it is new). 

    You can generate a class around that datatable and then you get a strongly typed datatable which has the code to cast the objects to types.

    Keep in mind that unboxing as it is often called did cost time in 1985, now does the move of one pixel on a screen takes 1000000 more time than unboxing an item. 


    Success
    Cor


    Tuesday, April 4, 2017 6:40 AM