.NET Table Adapters: Get vs. Fill?

你。 提交于 2019-12-08 15:09: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!


回答1:


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.




回答2:


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



回答3:


  • Get when you only want a single DataTable.
  • Fill when you want to add additional DataTables into a single DataSet.



回答4:


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.



来源:https://stackoverflow.com/questions/172436/net-table-adapters-get-vs-fill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!