Optimizing ado.net2.0 Data connections with multiple active result sets

Source: Internet
Author: User
Tags memory usage

Read the summary Welcome to discuss the use of Mars! With the Mars technology described here, you can perform multiple database queries on a single connection. And the code that relies on Mars technology is simpler and easier to read, and allows you to reduce memory usage and eliminate performance bottlenecks in certain programs while developing a Web application in a dataset. Don't you want to try Mars?

The multiple active result set (Sets, referred to as Mars) is a new feature of Ado.net 2.0-it allows multiple database queries or stored procedures to be executed on a single connection. As a result, you can get and manage multiple, forward-only, read-only result sets on a single connection. Prior to Mars, a separate connection was required for each result set. Of course, you also have to manage these connections and have to pay the appropriate memory and the high congestion bottleneck costs of potential applications-especially in the Web applications in the dataset.

The first business database to support Mars is SQL Server 2005, and in this article you will learn more about how to use this powerful tool that is easily added to ado.net development.

When accessing a data result set in a previous version of SQL Server, you need to create additional SqlConnection objects to work with SqlCommand. You need to track all of your result sets and their sqlconnections connections that cause inefficiencies and the "There is already a open DataReader associated with this Connection" error. Now, with the Mars technology, all this has become the past.

First, preparatory work

In the beginning, you need to set up a new connection string to start multiple active connections. Naturally, we name this setting "Multipleactiveresultsets" and use it as follows:

String connectionString = "Data Source=DBSERVER;" +
"Initial Catalog=AdventureWorlds;IntegratedSecurity=SSPI;" +
"MultipleActiveResultSets=True";

Its default setting is "false", and you can explicitly disable it-if you want to pass "false" to this multipleactiveresultsets set.

Follow these steps to establish and launch Mars and Ado.net 2.0:

1. Create a connection string as shown above.

2. Create a SqlConnection object and initialize it with the connection string.

3. Use its open () method to open the SqlConnection object.

4. Create a new SqlCommand object for each query you want to execute. Associate them with the SqlConnection object you created and opened above.

5. Use the appropriate command (for example, if you want to read the results of the query, you can use ExecuteReader (), or use ExecuteNonQuery () to update, and so on) to execute the query.

6. When finished, close the SqlConnection object.

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.