Is DataReader or DataSet?

zhaozj2021-02-16  92

I often hear someone asking this question: "What should I use in the ASP.NET web application or a DataSet class?" I often see such misunderstandings in many articles and newsgroups, that is, DataReader ( The abbreviation of SqlDataReader or OLEDBDataReader is better than DataSet. Sometimes I will also see the opposite statement. In fact, Microsoft created these two data access categories because they all we need. Each class has its advantages and insufficient, which you can choose to use according to the application environment.

This article has made a clear story on the choice of the two, allowing you to get some guidelines when using ASP.NET to choose the DataReader class or DataSet class. These rules may change in a client-based Windows Form application environment. When I am doing these, assuming that you have used DataReader and DataSet classes and are familiar with them.

Use the DataReader class

Here is the ideal condition to use the DataReader class: The data you read must be new, so you must read from the database every time you need data. Creating a DataReader class does not consume many memory, but with the increase of the load, the performance on the DataSet will also improve quickly (refer to the article in Visual Studio Magazine in the resource).

Your demand for each line is simple. The best example of this is to simply bind DataReader to a web control, such as DataGrid or DropdownList.

You only need to access XML data from the database in only forward-only, read-only form. In this case, you can get an XMLReader class (equivalent to the XML version of DataRead) with the ExcecuteExmlReader () method of the SQLCommand object. This requires a SQL Server query using the for XML clause, or a NTEXT field containing a valid XML.

You plan to read a few duplicate calls to the database to read a small piece of information. In this case, the performance data we mentioned earlier will have a greater improvement.

Indeed, making the DataSet class more powerful features only apply to the client-based Windows Form application, such as building a relationship between multiple tables. In many cases, the DataSet category is more advantageous than the DataReader class, and in some cases, you can't use the DataReader class at all.

Use the DataSet class

In the following case, you should consider using the DataSet class: You build a web service, the data it uses is the data you read as the return value. Because the DataReader class must be kept to the database, they cannot be serialized into the XML, and they cannot be sent to a caller of a Web Service.

You need to sort or filter data. In order to use a DataSet class to sort or filter data with a DataSet class, we use a DataSet class to sort or filter data, we try to implement these features with SQL queries (such as WHERE and ORDER BY statements) and use lighter Plated, faster DataReader class. However, sometimes it is not possible to use this method, or when you need to sort or filter data multiple times, you cannot use DataReader.

For the same request, you need to traverse data multiple times. You can only loop it in DataReader. If you want to bind multiple ServerControl classes to the same dataset, choose Dataset to be better. The DataReader class cannot be bound to multiple ServerControl classes because it is only read forward. In this case, if you want to use DataReader, you must read both data from the database. You need to store data, and the subsequent page requests may be used. If the data is only requested for special people, you can save the DataSet class in a session variable. If the data can be accessed by anyone, you can save it in an Application variable, or save it in Cache (I suggest the latter method because it supports time period and callback). Because the DataReader class must turn the connection to the database, and it can only save a row of data at a time, so they cannot be saved across the page request.

You need to achieve special, time-consuming features for each element of a result set. For example, if you read a column postal code from a database and want to get detailed weather information in each region by calling a web service, then choose Dataset will be better. This is because when you are using the DataReader class, the connection to the database will not be released back to the connection pool before turning off the DataReader class. Potential a small delay between thousands of page requests will cause a high amount of access to a web application, thereby consumes a wide range of connections. Instead, DataSet can read all data at the front end and immediately turn off the connection with the database, returning it to the connection pool, so other page requests can be connected.

You need to load and process XML data in a two-dimensional example. The DataSet class is useful for XML because you can use the DataView for XML, sort and filter fundamental data, and the same deal with a database result set. However, it is important to note that there are many classes in the System.xml namespace, you can use them for more complex XML operations.

Your data source is not a database. Although OLEDBDataReader can be used for any OLEDB data provider (may point to a database, it may not point to a database), but the DataSet object can load data directly from an XML file and dynamically explain its schema. The DataSet class can also write XML data back to a data stream or a file.

From the above story, we can see that the DataSet class has more features than the DataReader class, which allows you to use them in more cases. But this doesn't mean that you are always using a DataSet class. You need a considerable part of the task that is done in the ASP.NET belong to DataReader.

Despite this, there is no doubt, from the perspective of importance or complexity,

DataSet

Class in a lot of

ASP.NET Web

It plays an important role in applications. You can minimize database round trips through a wise cache, reducing

DataSet

"Performance damage" of the class.

DataReader

with

DataSet

It's a successful

ASP.NET Web

Important components of the application. What is important is, what we need to know, where can we use them best.

转载请注明原文地址:https://www.9cbs.com/read-13319.html

New Post(0)