
Recommendations for Accessing Data
The following sections provide recommendations for which data-access strategy to use with specific types of applications.
Windows Forms
In general, in a Windows Form, use a dataset. Windows Forms are typically used on rich clients where the form is not created and discarded (along with its data) with each user operation, as with Web Forms. Windows Forms applications also traditionally offer data-access scenarios that benefit from maintaining a cache of records, such as displaying records one by one in the form. For more information see, Creating Client Data Applications.
Specifically, use dataset under the following circumstances:
If you are working with the same records repeatedly, such as allowing a user to navigate between records.
If you are using the Windows Forms data-binding architecture, which is specifically designed to work with datasets.
For any of the other reasons listed under Web Forms above.
Use a TableAdapter query or data command under the following circumstances:
If you are getting a scalar value from the database
If you are performing a non-query operation, such as a DDL command.
If you are getting read-only data to display in a form — for example, creating a report. Stated differently, if there is no need to keep the data available after accessing it, use a data command.
Web Forms
In general, use data commands; to fetch data, use a data reader. Because Web Forms pages and their controls and components are recreated each time the page makes a round trip, it often is not efficient to create and fill a dataset each time, unless you also intend to cache it between round trips.
Use dataset under the following circumstances:
You want to work with multiple separate tables or tables from different data sources.
You are exchanging data with another application or a component such as an XML Web service.
You need to perform extensive processing with each record you get from the database. If you use a data command and data reader, processing each record as you read it can result in the connection being held open for a long period, which in turn can affect the performance and scalability of your application.
If your data processing involves interdependent records (for example, looking up information in related records).
If you want to perform XML operations such as XSLT transformations on the data.
If you prefer the ease of programming provided by datasets.
XML Web Services
XML Web services are ASP.NET Web applications, and therefore use the same model as Web Forms pages: the XML Web service is created and discarded each time a call is made to it. This suggests that the data-access model for an XML Web service is largely the same as it is for Web Forms. However, XML Web services are often middle-tier objects, and an important part of their purpose is often to exchange data with other applications across the Web.
Use a dataset if:
Your XML Web service sends and receives data — for example, sending it as the return value of a method and receiving it as a method argument. This is a fundamental choice in XML Web services; even if there are other reasons you might consider using a data command, data exchange with other components almost always means that you should use a dataset.
For any of the reasons listed above for Web Forms.
Use a data command (and if appropriate, a data reader) under the following circumstances:
The XML Web service is retrieving a scalar value.
The XML Web service is performing a non-query operation, such as a DDL command.
The XML Web service is calling a stored procedure to execute logic within the database.