Using a DataSet
Use a DataSet populated by a SqlDataAdapter object when:
- You require a disconnected memory-resident cache of data, so that you can pass it to another component or tier within your application.
- You require an in-memory relational view of the data for XML or non-XML manipulation.
- You are working with data retrieved from multiple data sources, such as multiple databases, tables, or files.
- You want to update some or all of the retrieved rows and use the batch update facilities of the SqlDataAdapter.
- You want to perform data binding against a control that requires a data source that supports IList.
If you use a SqlDataAdapter to generate a DataSet or DataTable, note the following:
- You do not need to explicitly open or close the database connection. The SqlDataAdapter Fill method opens the database connection and then closes the connection before it returns. If the connection is already open, Fill leaves the connection open.
- If you require the connection for other purposes, consider opening it prior to calling the Fill method. You can thus avoid unnecessary open/close operations and gain a performance benefit.
- Although you can repeatedly use the same SqlCommand object to execute the same command multiple times, do not reuse the same SqlCommand object to execute different commands.