Use datareader or dataset to operate data-transferred from Microsoft

Source: Internet
Author: User
Tags sql server query

Use datareader or dataset?

We often hear people ask this question: "in ASP. NET web applicationsProgramShould I use the datareader class or the dataset class ?" In manyArticleAnd I often see this misunderstanding in the newsgroup posts, that is, the datareader (abbreviation of sqldatareader or oledbdatareader) is better than dataset. Sometimes I see the opposite. In fact, Microsoft has created these two data access classes because they are all what we need. Each class has its own advantages and disadvantages. You can choose which one to use based on the application environment.

This article gives a clear description of the selection of the two, so that you can get some guidance on selecting the datareader class or dataset class when using ASP. NET. In a client-based Windows form application environment, these rules may change. When talking about these things, let's assume that you have used the datareader and dataset classes and are familiar with them.

Use datareader class
The following is an ideal condition for using the datareader class: the data you read must be new, so you must read the data from the database every time you need it. Creating a datareader class does not consume a lot of memory, but as the load increases, the performance on the dataset will also increase quickly (refer to the article in Visual Studio magazine in the resource ).
Your requirements for each row of data are simple. The best example is to simply bind a datareader to a web control, such as a DataGrid or dropdownlist.
You only need to access XML data from the database in the form of forward-only and read-only. In this case, you can use the excecutexmlreader () method of the sqlcommand object to obtain an xmlreader class (equivalent to the XML version of datareader ). This requires an SQL Server query that uses the for XML clause, or an ntext field that contains valid XML.
You plan to make several repeated calls to the database to read a small piece of information. In this case, the performance data we mentioned above will be greatly improved.
Indeed, many of the more powerful functions of the dataset class are only applicable to client-based Windows form applications, such as the ability to establish relationships between multiple tables. In many cases, the dataset class is more advantageous than the datareader class, and in some cases, you simply cannot use the datareader class.

Use dataset class
In the following situations, you should consider using the dataset class: you have built a web service, which uses the data you read as the return value. Because datareader classes must be connected to databases, they cannot be serialized into XML or sent to a Web Service caller.
You need to sort or filter data. Before using a dataview object (presented as the defaultview attribute of the able class, which contains a dataset class) to sort or filter data, we first try to use SQL queries (such as where and order by statements) to implement these functions, and use a more lightweight and faster datareader class. However, sometimes this method cannot be used, or you cannot use datareader when you need to sort or filter data multiple times.
For the same request, you need to traverse data multiple times. You can only loop in datareader once. If you want to bind multiple servercontrol classes to the same dataset, it is better to select dataset. The datareader class cannot be bound to multiple servercontrol classes because it only reads forward data. In this case, if you want to use datareader, you must read the data twice from the database.
You need to store data, which may be used by subsequent page requests. If the data is only used by the person who requests it, 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 in a cache (I suggest using the latter method, because it supports time period and callback )). Because the datareader class must always open a connection to the database, and it can only save one row of data at a time, they cannot be saved in cross-page requests.
You need to implement special time-consuming functions for each element of a result set. For example, if you read a column of zip code from a database and want to call a web service to obtain detailed weather information for each region, it would be better to select dataset. This is because when you use the datareader class, the connection to the database will not be released back to the connection pool before closing the datareader class. A small latency between thousands of page requests may result in a high access volume of Web applications, consuming available connections. On the contrary, dataset can read all the data at the front end, close the connection with the database immediately, and return it to the connection pool. Therefore, other page requests can use this connection.
You need to load and process XML data in a two-dimensional example. The dataset class is useful for XML, because you can use dataview for XML and sort and filter the basic data, just like processing a database result set. However, you must 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 by any oledb data provider (which may point to a database or not to a database), the DataSet object can directly load data 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, we can see that the dataset class has more functions than the datareader class, which allows you to use them in more cases. However, this does not mean that you are always using the dataset class. A considerable part of the tasks you need to complete in ASP. NET belong to the category of datareader.

Even so, there is no doubt that the dataset class plays an important role in many ASP. NET web applications in terms of importance or complexity. You can minimize database round-trips through wise caching to reduce the "performance damage" of the dataset class ". Datareader and dataset are both important parts of a successful ASP. NET web application. What's important is that we need to know when and where to best use them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.