Talk about Asp.net website optimization 1: SqlDataReader and DataSet Selection

Source: Internet
Author: User

 

After the release of this article, I found that there are more than N friends who like the cloud. Others say that database connection is more expensive than memory, and you also say that database connection is more expensive than memory, can low memory costs become an excuse for us not optimizing code?

 

 

 

 

This is a question that will be asked during almost every interview. In development using. NET, you should use SqlDataReader or DataSet to read data and understand the two.

 

Let's talk about my understanding of the two:

 

1. SqlDataReader: provides a stream-only method for reading rows from the SQL Server database. This class cannot be inherited. This object can be used to read-only data from the database. The so-called read-only, can be understood as one end to death, do not hit the south wall, do not go back. Just like the wheat cutover, this one is gone after it passes. (Do not understand that the data in the database is read-only)

 

So under what circumstances will we use the SqlDataReader object?

 

First, when reading an object from a database, we can use SqlDataReader.

 

SqlDataReader can be used to read a series of (list) data.

 

Note that. When you use SqlDataReader to read data, you need to explicitly Close it. Otherwise, it will be connected to the database, and the memory usage will not be said, but it is also prone to problems.

 

If it is destroyed after use, it will no longer occupy memory and other resource-consuming items!

 

2. DataSet: MSDN (DataSet is the central concept of ADO. NET. DataSet can be regarded as a database in memory, and DataSet is an independent data set independent of the database. The so-called independence means that, even if the data link is disconnected or the database is closed, DataSet is still available, and DataSet uses XML internally to describe the data, because XML is a data description language that is platform-independent and language-independent, and can describe data with complex relationships, such as parent-child relationship data, therefore, DataSet can actually accommodate data with complex relationships and is no longer dependent on database links .)

 

Vernacular explanation: DataSet is a data set that is stored in the memory. It is equivalent to a database (data set) that is stored in the memory and does not depend on the database.

 

Compared with SqlDataReader, the advantage of DataSet is that the database is disconnected. DataSet is still stored in the memory and can be operated at will, such as deleting row, merging, appending, and modifying a data.

 

Note that since DataSet can immediately close the connection to the database after reading the data, DataSet will exist in the memory. So before the GC is automatically recycled, it occupies system resources. For reading the list, it is obviously not wise to store such a pile of data in the server memory.

 

The server memory is limited. If a large number of datasets are not collected by GC in the program, the consequences ..........

 

 

 

 

 

Selection and selection of SqlDataReader and DataSet during development

 

As mentioned above, this question is always asked during almost every interview. I believe that very much's children's shoes know the differences between the two, and even reach the extreme.

 

However, I have seen most programs. Including the existing projects of my new company, I can hardly find the shadows of SqlDataReader. Why?

 

Here I have a question, that is, why do you choose DataSet instead of SqlDataReader to read data? Obviously, if the answer is more convenient than DataSet, It is very nonsense!

 

I was wondering if the understanding of DataSet and SqlDataReader is not deep enough for all children who like DataSet? I don't think so. Maybe everyone is not familiar with conceptual things, or in order to pursue development efficiency, or you may think that modifying or deleting a list read from DataSet in a program is the so-called "you can operate on it at will ".

 

One thing I need to correct is that this "random operation" is not "random operation". I think DataSet can be freely operated out of the database and in the memory: "You can perform a series of operations on the data in the DataSet. For example, merge data, statistical data, modify data, delete data, and so on (of course, the example here can be inaccurate) and then return a modified DataSet ". In fact, I see the following situation: the data list is displayed only on the page (screen), and the data in each row is followed by a modification or deletion. In this case, DataSet is still used.

 

About the above questions. You can use SqlDataReader instead, because there is no operation on the list in memory. Why not use SqlDataReader? I think that DataSet has only a little advantage over SqlDataReader (maybe I don't understand it enough, maybe there are other advantages), but in the above scenarios, he is completely lost and useless, why do we need to consume a little bit of system resources from memory?

 

 

 

The advantages of SqlDataReader over DataSet I believe you are very aware of it. But in fact few people prefer DataSet to use SqlDataReader. I don't know why.

 

Maybe a lot of people are still at the level of graduation! Or why.

 

 

 

When I see an existing project in the company

 

A.) No SqlDataReader. DataSet is used for SqlDataReader.

 

B) There are a large number of page resends that can be avoided

C.) There are a lot of useless viewstates.

 

D. The GridView used for displaying the data list of the asp.net website.

 

I changed my signature to "my internal injuries !"

 

 

 

You may not agree with the above statement, but it does not matter. I accept your suggestions modestly with my learning attitude.

 

Welcome to shoot bricks.

Author Zhou zhaokun

Related Article

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.