Readme_MARS Sample

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This sample works only with SQL Server 2005 and SQL Server 2008. It will not work with any version of SQL Server earlier than SQL Server 2005.

This sample demonstrates how to use the Multiple Active Result Set (MARS) feature. MARS lets you run multiple commands on the same connection, even though all of the results from running one or more of the commands might not be completely returned to the client yet.

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.

Default Location: C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Data Access\ADO\MARS

SQL Server samples and sample databases must be downloaded and installed before you can view or work with them. For more information, see Considerations for Installing SQL Server Samples and Sample Databases.

Scenario

Adventure Works 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 and the .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 or SQL Server Express. You can download SQL Server Express from the SQL Server Samples and Downloads Web site.
  • The AdventureWorks database which is included with SQL Server, and is also available at the SQL Server Developer Web site.
  • The SQL Server Database Engine samples. These samples are included with SQL Server. You can download the latest version of the samples at the SQL Server Developer Web site.
  • .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See Installing the .NET Framework Documentation.

Building the Sample

If you have not already created a strong name key file, generate the key file using the following instructions.

To generate a strong name key file

  1. Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

    -- or --

    Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

  2. Use the change directory command (CD) to change the current directory of the command prompt window to the folder where the samples are installed.

    Note

    To determine the folder where samples are located, click the Start button, point to All Programs, point to Microsoft SQL Server, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\100\Samples.

  3. At the command prompt, run the following command to generate the key file:

    sn -k SampleKey.snk

    Important

    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.

To build the sample, do the following:

Build the sample

  1. Compile the sample by using Visual Studio and the solution file located in the CS directory, or use the following MSBuild command line in a .NET Framework or Microsoft Visual Studio 2005 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

  1. Open the scripts\install.sql script by using SQL Server Management Studio. Run the contents of that file, or run the following command in a command prompt window:

    sqlcmd -E -I -i Scripts\install.sql

  2. In a command prompt window, locate the CS\MARS\bin\debug directory 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

  1. Open the Scripts\cleanup.sql by script using Management Studio. Run 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, you might 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 by using a JOIN, as demonstrated.

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;