Delen via


Send DataSet Sample

The Send DataSet sample demonstrates how to return an ADO .NET based DataSet within a server side common language runtime (CLR)-based stored procedure as a result set to the client. This is useful, for example, when such a stored procedure fills a DataSet using the results of a query, and then manipulates the data that is contained in that DataSet. Alternatively this is useful if the stored procedure creates and populates a DataSet from scratch.

The sample is composed of two classes, DataSetUtilities and TestSendDataSet. The method SendDataSet on the DataSetUtilities class implements a general-purpose way to transmit the contents of a DataSet instance to the client. The DoTest method that is defined on the TestSendDataSet class verifies that the SendDataSet method works by creating a DataSet and filling it with data from the usp_GetTwoBOMTestData Transact-SQL stored procedure. The usp_GetTwoBOMTestData runs the Transact-SQL stored procedure uspGetBillOfMaterials twice to recursively query for the bill of materials for two products specified as parameters to the usp_GetTwoBOMTestData stored procedure. Ordinarily, after filling the data set, the data would be modified before invoking SendDataSet to deliver the data within the data set as a result set to the client. For simplicity, this sample returns the data without modification.

Install directory: drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\SendDataSet\

Scenario

Jane is a developer for Adventure Works Cycles. She has collected information in one of her CLR based stored procedures in a DataSet, and wants to return the information contained in that DataSet back to her client application.

Languages

Transact-SQL, Visual C# and Visual Basic.

Features

The Send DataSet sample uses the following features of SQL Server.

Application Area Features

Overall

CLR, metadata, DataSet, stored procedures, CTE

Prerequisites

Before running this sample, make sure the following software is installed:

  • Microsoft SQL Server 2005 or Microsoft 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. 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, 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 Send DataSet sample

  1. Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run a command similar to the following at a command prompt:

    msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\ SendDataSet.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 portion of the script in Scripts\InstallCS.sql and Scripts\InstallVB.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 Microsoft 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 Send DataSet sample

  1. Open the scripts\test.sql file in Management Studio and run the script that is contained in the file. Or, execute the following command in a command prompt window:

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

Removing the Sample

To remove the Send DataSet sample

  1. Open the scripts\cleanup.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\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.