.NET Table Adapters: Get vs. Fill?
Asked Answered
F

4

12

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!

Frump answered 5/10, 2008 at 18:45 Comment(0)
J
3

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.

Joannajoanne answered 14/10, 2008 at 11:43 Comment(0)
V
4

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
Vereen answered 12/2, 2009 at 20:48 Comment(0)
U
3
  • Get when you only want a single DataTable.
  • Fill when you want to add additional DataTables into a single DataSet.
Upraise answered 5/10, 2008 at 19:51 Comment(0)
J
3

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.

Joannajoanne answered 14/10, 2008 at 11:43 Comment(0)
V
1

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.

Verditer answered 5/10, 2008 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.