Dataset indicates a dataset, which is cached in memory. Can contain multiple tables
Datset is non-connection-oriented when connecting to the database. Read all tables to the SQL Buffer Pool and disconnect from the database.
Datareader is connected to the database. When reading a table, you can only read the table forward. After reading the data, the user decides whether to disconnect the table.
The data in the distributed system may use dataset as the data carrier, because dataset is the data structure that stores the data, and datareader is not responsible for saving the data, it is only responsible for reading data from the data source to the local. It is not a data structure, but a high-level encapsulation of network communication components. Dataadapter only uses datareader to read data from the data source and add it to dataset to save it. If we use datareader separately, we can also write data to the business class or dataset. That's just to select different data carriers based on business needs. In fact, all the data we get from the database will be obtained through datareader, but dataadapter encapsulates all this.
What is the difference between dataset and datareader objects?
Dataset can be processed offline and rolled back and forth. datareader cannot be processed offline and is read-only forward. However, the dataset can store various database objects, such as table triggers, quickly. datareader can only store cursor records.
Dataset can be updated back to the original database, but datareader cannot;
Dataset can forword previus, while datareader can only be FW;
Datareader is similar to a forward cursor record set.
Dataset is called a dataset! It is the main embodiment of the disconnected database connection between ado.net and ADO! Datereader is a client that only forwards the cursor. The two fields are different! After reading data! If you want to make frequent changes, you can use dataset, and dataset also supports serialization, can be combined with XSLT! Web development!
Datareader is more inclined to read data quickly! Data with large data volumes may be used more frequently! <[3]> ADO. NET provides the following two objects for retrieving relational data and storing it in memory: DataSet and datareader. Dataset provides a relational representation of In-memory data, a set of data including some tables (these tables contain data, Sort data, and constrain data), and relationships between tables. Datareader provides a fast, forward-only, read-only data stream from the database.
When dataset is used, dataadapter (or commandbuilder) is often used to interact with the data source. When using dataset, you can also use dataview to sort and filter data in dataset. You can also inherit from dataset to create a strong-type dataset, which is used to expose tables, rows, and columns as strong-type object attributes.
The following topics involve the best time to use dataset or datareader, how to optimize access to the data they contain, and how to optimize the techniques for using dataadapter (including commandbuilder) and dataview.
Dataset and datareader
When designing an application, consider the level of functionality required by the application to determine whether to use dataset or datareader.
Dataset is required to perform the following operations through the application:
R is used to navigate between multiple discrete tables in the result.
R operations are performed on data from multiple data sources (for example, mixed data from multiple databases, an XML file, and a workbook.
R exchanges data between layers or uses XML Web Services. Unlike datareader, dataset can be passed to a remote client.
R reuse the same set of records to improve performance (such as sorting, searching, or filtering data) through caching ).
R each record requires a large amount of processing. Extended processing of each row returned by datareader will prolong the necessary time to serve the datareader connection, which affects the performance.
R uses XML operations to operate data, such as extensible style sheet language conversion (XSLT conversion) or XPath query.
Use datareader in applications in the following cases:
R does not need to cache data.
The result set to be processed by R is too large to be stored in the memory.
Once R needs to access data in a forward-only and read-only manner.
Note: When filling in dataset, dataadapter uses datareader. Therefore, replacing dataset with dataadapter improves performance by saving dataset memory usage and filling the cycles required by dataset. Generally, this performance improvement is symbolic. Therefore, design decisions should be based on the required functions. <[4]>
I often hear this question: "should I use the datareader class or the dataset class in ASP. NET web applications ?" I often see this misunderstanding in many articles and newsgroups, 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.