With a DataReader or a dataset?

Source: Internet
Author: User
Tags filter bind implement sql server query sort time limit access visual studio
I often hear people ask this question: "Should I use a DataReader class or a DataSet class in a ASP.net Web application?" "I often see this misconception in many articles and newsgroup postings that DataReader (SqlDataReader or OleDbDataReader abbreviations) are better than datasets. Sometimes I also see the opposite story. In fact, Microsoft created the two data access classes because they were all we needed. Each class has its advantages and disadvantages, and you can choose which one to use depending on your application environment.

This article makes a very clear discussion on the choice of the two, so that you can get some guidance in choosing the DataReader class or DataSet class when using ASP.net. These rules may change in the context of a client-based Windows Form application. When I do these things, let's say that you've used DataReader and dataset classes and are familiar with them.

Using the DataReader class
The following is the ideal condition for using the DataReader class: The data you read must be new, so you must read it from the database every time you need data. Creating a DataReader class does not consume a lot of memory, but as the load increases, the performance on the dataset is increased quickly (the article in Visual Studio magazine in the reference Resource).

Your need for each line of data is simple. The best example of this situation is simply to bind DataReader to a Web control, such as a DataGrid or DropDownList.

You only need to access XML data from the database in a forward-only (forward-only), read-only form. In this case, you can use the SqlCommand object's Excecutexmlreader () method to get a XmlReader class (equivalent to an XML version of DataReader). This requires a SQL Server query that uses a FOR XML clause, or a 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 earlier will improve a lot.

Indeed, many of the features that make the dataset class more powerful apply only to client-based Windows Form applications, such as the ability to establish relationships among multiple tables. In many cases, the DataSet class is more advantageous than the DataReader class, and in some cases you cannot use the DataReader class at all.



Using DataSet classes
In the following scenario, you should consider using the DataSet class: You build a Web service that uses data that you read as a return value. Because the DataReader classes must remain connected to the database, they cannot be serialized into XML or sent to the caller of a Web service.

You need to sort or filter the data. Before you can sort or filter data by using a DataView object (which is rendered as a DefaultView attribute of a DataTable class, which contains a dataset class), we first try to implement these features with SQL queries, such as where and order by statements, and use a lighter, faster DataReader class. However, it is sometimes not possible to use this method, or you cannot use DataReader when you need to sort or filter the data multiple times.

For the same request, you need to traverse the data multiple times. You can only cycle once in the DataReader. If you want to bind multiple Servercontrol classes to the same dataset, it is better to select a dataset. 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 two data from the database.

You need to store the data, and subsequent page requests may use the data. If the data is only used by the dedicated person requesting it, you can save the dataset class in a session variable. If the data can be accessed by anyone, you can save it in a application variable, or keep it in the cache (I recommend using the latter method because it supports the time limit and callback (callback)). Because the DataReader class must always open a connection to the database, and it can only hold one row of data at a time, they cannot be saved in a cross-page request.

You need to implement special, time-consuming functions for each element of a result set. For example, if you read a list of ZIP codes from a database and want to get detailed weather information for each region by invoking a Web service, it would be better to select a dataset. This is because when you use the DataReader class, the connection to the database is not released back to the connection pool until the DataReader class is closed. A potentially small delay between thousands of page requests can result in a high amount of traffic to the Web application, which consumes the available connections. Instead, the dataset can read all the data on the front end and immediately close the connection to the database, returning it to the connection pool, so that other page requests can be connected with this.

You need to load and process XML data in a two-D paradigm. The dataset class is useful for XML because you can use DataView for XML, sort and filter the underlying data, just as you would a database result set. However, it should be noted that there are many classes in the System.Xml namespace that you can use for more complex XML operations.

Your data source is not a database. Although OleDbDataReader can be used for any OLE DB data provider (which may point to a database or not to a database), the DataSet object can load data directly from an XML file and interpret its schema dynamically. The dataset class can also write XML data back to a data stream or to a file.



From the above, we can see that dataset analogy DataReader classes have more functionality, which allows you to use them more often. But that doesn't mean you're always using a dataset class. A significant portion of the tasks that you need to accomplish in asp.net belong to the DataReader category.

However, there is no doubt that DataSet classes play an important role in many ASP.net Web applications, from the point of view of degree or complexity. You can minimize the "performance damage" of the dataset class by minimizing the database round-trip by judicious caching. Both the DataReader and the dataset are important parts of a successful asp.net Web application. The important thing is that we need to know when and where to use them best.




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.