Several Effective Results Sets (MARS) is a separate ability put-out which have ADO.Internet 2.0 that enables that do several issues or held actions using one commitment. The result is several send-simply, read-only effects set. Within the earlier incarnations from ADO.Web, you can execute only 1 ask at any given time playing with one connection. Now, with the advent of SQL Host 2005 and you will ADO.Online 2.0, your quickly can also be play multiple analytical batches in one single partnership. This article demonstrates how to utilize MARS for the a keen ASP.Internet Page. In addition discusses the issues where MARS is acceptable and also have overall performance pros.
This is MARS
By using DataReader on your own applications, you already have heard of dreaded mistake message: “There was already an open DataReader of it Union and therefore have to be closed earliest.” MARS enables you to stop that it content by permitting one to open several SqlDataReader objects using one connection. MARS permits a software to own more than one SqlDataReader unlock on the a link when for every exemplory case of SqlDataReader was already been off an alternate command. For every single SqlCommand target you add contributes an extra session toward partnership.
By default, MARS is present simply towards is the earliest SQL Host version to support MARS. By default, MARS is enabled once you make use of the categories in the Program.Data.SqlClient namespace for connecting to SQL Host. not, you additionally normally explicitly handle this particular feature by using a term partners on your union sequence. Such, you could explicitly put the latest MultipleActiveResultSets trait in the union sequence to help you True as follows:
- Manage a good SqlConnection target and you can initialize they with the suitable connection string.
- Open the connection making use of the Unlock type the latest SqlConnection object.
- Carry out individual SqlCommand items into necessary variables to execute this new ask. Whenever you are undertaking the SqlCommand things, be sure to representative all of them with the fresh new in earlier times composed SqlConnection target.
- After you’ve developed the SqlConnection object, then you can invoke the fresh new ExecuteReader sort of new SqlCommand target to execute the newest question.
- Ultimately, intimate the SqlConnection object by the executing the fresh new Intimate method.
MARS Execution
The example this particular article gift ideas demonstrates how to make use of ple displays categories and you can device information regarding Northwind database. For each classification you access on the categories dining table, then you have a tendency to inquire these products dining table (with the group ID because the a disagreement) to go back the items that belong in that group. You will observe simple tips to use brand new because of it circumstance.
Earliest, create another Website entitled ed ple.aspx so you can they. Today, customize the password regarding the ple.aspx file to look for instance the after the:
Take a closer look at precisely what the contours regarding the above code perform. Basic, brand new code retrieves the relationship string online.config file utilizing the following the line:
The above distinct password retrieves the connection sequence in the ConnectionStrings part of the net.config file. The partnership sequence was stored in the internet.config as follows:
Up coming, the brand new code initializes the fresh new categorySql and productsSql variables to the compatible SQL statements. Following, it creates an exemplory case of brand new SqlConnection target through good “using” report. Then, it opens up the connection string by calling this new Open form of the fresh SqlConnection target. 2nd, they inspections this new ServerVersion possessions of the SqlConnection to see whether the new SQL Server supporting MARS. If your ServerVersion property yields a primary sorts of “09”, your properly can be believe that SQL Server helps MARS.
The latest password second brings instances of the fresh new SqlCommand target and assigns these to the fresh categoryCommand and productCmd parameters, correspondingly. Apart from that, what’s more, it contributes the brand new CategoryID parameter into the productCmd variable. After that, it executes the brand new ask contained in the categoryCommand target by the invoking the new ExecuteReader particular this new categoryCommand escort services in Kent object. They captures the outcomes of your SQL inquire execution inside the good SqlDataReader adjustable then loops using all of the details in that SqlDataReader object.
The code contributes the category label with the PlaceHolder handle compliment of an excellent LiteralControl immediately after which retrieves products for every single group of the invoking the fresh new ExecuteReader particular this new productCmd target. When your productReader object include people valid rows, it simply research attach one object with good dynamically created GridView handle. If you’re carrying out this new GridView control, moreover it establishes some qualities of GridView manage, such as Obvious, BackColor, and ForeColor. Should your productReader object doesn’t have legitimate rows, it simply displays the message: “Zero Situations Utilized in these kinds.”
For individuals who lookup towards significantly more than Websites setting playing with an internet browser, you will observe brand new yields exhibited inside Contour step one.