views:

1435

answers:

4

I always seem to use Get when working with data (strongly typed or otherwise) from the database and I have never really needed to use Fill although I just as easily could use Fill instead of get when pulling out and updating data.

Can anyone provide guidance as to the implications and gotchas of each method?

In what situations is it preferable to use one or the other?

Any performance implications?

Thanks in advance for the answers! I love this community!

+1  A: 

The only difference is that GetData instantiates a table for you, Fill will fill an existing table.

It depends if you want or need to instantiate the DataTable. I often use Fill when filling a certain table member of a DataSet I already instantiated.

Aidan Ryan
+2  A: 
  • Get when you only want a single DataTable.
  • Fill when you want to add additional DataTables into a single DataSet.
martin
+2  A: 

A particular gotcha of Fill, if the table already contains data is that you could get unique index exceptions when, for example, the query returns a row whose primary key is already in the table.

I've worked with a lot of data-bound Windows Forms code where edit controls or a grid on the form is bound to a table and then Fill is used to load more rows from the database to the table. This can cause some interesting event firing sequences and intermittent errors from experience.

Using Get to retrieve a new table with the new results then rebinding the form to the new table can avoid situations like this.

I doubt there is much performance difference between the two unless using Fill on a table with existing rows. In this case the table's BeginLoadData method is ignored which would normally have delayed event firing and index rebuilding until the end.

Jason Stangroome
+2  A: 

Using Fill can be great for debugging exceptions because the DataTable passed into the method can be interrogated for more details. Get does not return in the same situation.

Tips:

  • DataTable.GetErrors() returns an array of DataRow instances that are in error
  • DataRow.RowError contains a description of the row error
  • DataRow.GetColumnsInError() returns an array of DataColumn instances in error
Jonathan Webb