This sample demonstrates how to use the Multiple Active Result Set (MARS) feature. MARS lets you execute multiple commands on the same connection, even though all of the results from executing one or more of the commands might not be completely returned to the client yet.
Default Location: drive:\Program Files\Microsoft SQL Server 2005 Samples\Engine\
Data Access\ADO\MARS
Scenario
AdventureWorks Cycles wants to raise the standard cost and list price of their most popular bicycles because the prices for those bicycles have increased due to increases in paint cost. The price increase will vary depending on the color of the paint.
Languages
Transact-SQL, Visual C# and Visual Basic.
Features
The MARS sample uses the following features of SQL Server 2005 and the Microsoft .NET Framework version 2.0:
| Application Area | Features |
|---|---|
|
Overall |
MARS, ADO.NET 2.0, Transact-SQL Stored Procedures |
Prerequisites
Before running this sample, make sure the following software is installed:
-
SQL Server 2005 or SQL Server 2005 Express Edition (SQL Server Express). You can download SQL Server Express from this Microsoft
Web site .
-
The Adventure Works database, which ships with SQL Server 2005. Adventure Works can also be downloaded from this Microsoft
Web site .
-
The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005 and are also included with the Microsoft .NET Framework SDK 2.0.
-
.NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See Installing the .NET Framework SDK.
Building the Sample
To build the sample do the following:
Build the sample
-
CD to the install directory and execute the following at a .NET Framework or Microsoft Visual Studio 2005 command prompt:
sn -k keypair.snk -
Compile the sample by using Visual Studio and the solution file located in the
CSdirectory, or use the following msbuild command line in a command prompt window:msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\ MARS.sln
Running the Sample
To run the sample do the following:
Run the sample
-
Open the
scripts\install.sqlscript by using SQL Server Management Studio. Execute the contents of that file, or run the following command in a command prompt window:sqlcmd -E -I -i Scripts\install.sql -
In a command prompt window, locate the
CS\MARS\bin\debugdirectory and run the following command:mars
Removing the Sample
To reset the data modified by this sample and remove the sample do the following:
Remove the sample
-
Open the
Scripts\cleanup.sqlby script using SQL Server Management Studio. Execute the contents of that file, or run the following command in a command prompt window:sqlcmd -E -I -i Scripts\cleanup.sql
Comments
Always consider whether it would be more efficient to use JOINs in a server-side query or update instead of using MARS. For example, suppose you need to know the sales order ID, customer ID, product number, order quantity, and line item total for every sales order detail record for March 2004. Although you could write that query with MARS, it is more efficient to write that query using a JOIN, as demonstrated.
| Copy Code | |
|---|---|
SELECT SOH.SalesOrderID, SOH.CustomerID, SOD.ProductID, SOD.OrderQty, SOD.LineTotal FROM Sales.SalesOrderHeader as SOH JOIN Sales.SalesOrderDetail as SOD ON SOH.SalesOrderID = SOD.SalesOrderID WHERE SOH.OrderDate >= CONVERT(datetime, '20040301') AND SOH.OrderDate < CONVERT(datetime, '20040401') ORDER BY SOH.SalesOrderID; | |