Result Set Sample


Updated: 17 July 2006
Sometimes it is useful to be able to execute commands while reading through results of a query without opening a new connection and without reading all the results into memory. The Multiple Active Result Set (MARS) feature in ADO .NET 2.0 is a technology that can help you achieve that. Currently, MARS is not implemented for the in-process provider used for server-side programming. To work around this limitation, you can use server-side cursors. This sample demonstrates how to use server-side cursors to work around the lack of MARS support for server-side programming.

Note:
Using server-side cursors is expensive in terms of server resources and can sometimes prevent the query optimizer in Microsoft SQL Server from enhancing the performance of your queries. Therefore, you might consider rewriting your code to use JOINs whenever possible.



The API for this class is similar to a data reader except that you can move forward or backward throughout the result set, and other commands can be issued on the connection while the result set is open.
This implementation is highly simplified to make the sample easy to understand. A more efficient implementation would fetch multiple rows to avoid a database turnaround per row fetched.
Using this class can have a significantly smaller memory footprint than filling a dataset with all the results of a query, which is very important for server-side programming.
This sample also demonstrates using the "Allow partially trusted callers" attribute to indicate that the Result Set assembly is a library which may be safely called from other assemblies. This approach is a little more complex but much more secure than registering the calling assembly using the unsafe permission. It is more secure because registering the calling assembly as safe the calling assembly limits affecting resources off the server and prevents damage to the integrity of the server.
Install directory:
drive:\Program Files\Microsoft SQL Server\90\Samples\
Engine\Programmability\CLR\ResultSet\

Scenario

Jane is a developer for Adventure Works Cycles. She has to write software that reads data and performs other actions before all the data is returned from her server-side query.

Languages

Transact-SQL, Visual C# and Visual Basic.

Features

The ResultSet sample uses the following features of SQL Server.

Application Area Features
Overall CLR, server-side cursors, Transact-SQL


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 obtain SQL Server Express free of charge from the SQL Server 2005 Express Edition Documentation and Samples Web site.
  • The AdventureWorks database which is included with SQL Server 2005, and is also available at the SQL Server Developer Web site.
  • The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005. 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. For more information, see Installing the .NET Framework SDK.

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 2005, 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\90\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 ResultSet sample
  1. Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0.Run commands similar to the following at the command prompt: msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\ ResultSet.sln
  2. Make sure that the AdventureWorks database is installed.
  3. If you did not install the SQL Server Database Engine samples in the default location, modify the path in the CREATE ASSEMBLY part of the script in Scripts\InstallCS.sql to refer to the location where the samples were installed.
  4. If you are not an administrator for the SQL Server instance you are using, you must have an administrator grant you CreateAssembly permission to complete the installation.
  5. Open the scripts\installCS.sql or scripts\installVB.sql file, depending on whether you compiled the Visual C# project or the Visual Basic project, in SQL Server Management Studio. Run the script that is contained in the file, or run a command similar to the following in a command prompt window: sqlcmd -E -I -i Scripts\InstallCS.sql

Running the Sample

To run the ResultSet sample
  1. Open the scripts\test.sql file in Management Studio and run the script that is contained in the file, Or, run the following command in a command prompt window: sqlcmd -E -I -i Scripts\test.sql

Removing the Sample

To remove the ResultSet sample
  1. Open the scripts\cleanup.sql file in SQL Server Management Studio and run the script that is contained in the file. Or, run the following command in a command prompt window: sqlcmd -E -I -i Scripts\cleanup.sql

Comments

The CLR for SQL Server 2005 or SQL Server Express must be enabled for this sample to work correctly.
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.

Change History


Release History
5 December 2005 Changed content: Changed instructions for generating a key file, including the name and location of the key file.
17 July 2006 Added content: Added content describing security issues.


© 2007 Microsoft Corporation. All rights reserved.

Last edited Nov 6, 2008 at 10:25 PM by bonniefe, version 16

Comments

No comments yet.