Readme_AdventureWorks Cycles CLR Layer Sample

11/05/2008 21:36:06

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.
The AdventureWorksSQLCLRLayer sample for SQL Server provides some useful utilities that form an extra layer of functionality on top of the base AdventureWorks sample database. The first utility creates contact records for various types of people involved in the AdventureWorks database. The contact information is specified by using XML and is passed to a C#-based stored procedure. The second utility defines a Currency user-defined data type by using C#. This user-defined data type encapsulates both an amount and a culture that helps to determine the correct way to render the amount as a currency value in that culture. The third utility provides a currency conversion function that returns an instance of the Currency user-defined data type. If the AdventureWorks database has a conversion rate from U.S. dollars (USD) to the currency that is associated with the specified culture, the conversion function returns a Currency user-defined data type with the converted rate and a culture that matches the culture requested. Otherwise, a Currency user-defined data type is returned with the original amount, which should be in USD, with the en-us culture. The utilities also demonstrate how to unregister and register common language runtime (CLR) methods and assemblies by using Transact-SQL.

The exchange rates used in this sample are fictitious and should not be used for actual financial transactions.

When you compile SQLCLR samples, note that the path of the version of the Microsoft .NET Framework compiler that is included with SQL Server must be the first .NET Framework directory in the PATH environment variable. The location of this compiler is either drive:\WINNT\Microsoft.NET\Framework\v2.0.xxxxx or drive:\Windows\Microsoft.NET\Framework\v2.0.xxxxx where drive is the installation drive, and xxxxx is the .NET Framework version that is included with SQL Server.
The default installation directory is C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Programmability\CLR\AdventureWorks.
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.


  • A developer needs a convenient way to represent, persist, and manipulate different monetary amounts in different currencies.
  • A developer needs a convenient way to create new contact information for employees, individual customers, store customers, and vendors and relate this information to other information stored in the AdventureWorks database.


Transact-SQL, Visual C#, and Visual Basic.


The SQLCLR layer sample uses the following features of SQL Server.

Application Area Features
Contact XML
Currency, ConvertCurrency CLR UDT, CLR user-defined function (UDF)


Before running this sample, make sure the following software is installed:
  • SQL Server or SQL Server Express. You can obtain SQL Server Express free of charge from the SQL Server Express Documentation and Samples Web site.
  • The AdventureWorks database that is available at the SQL Server Developer Web site.
  • The SQL Server Database Engine samples that are available 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.
Use the same procedure to create a key file named UnsafeSampleKey.snk if you have not done so already.
To build the sample, do the following:
Build the 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 a command similar to the following at the command prompt: msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\AdventureWorksCS.sln
  2. Ensure 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 to refer to 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 at the command prompt: sqlcmd -E -I -i Scripts\InstallCS.sql This script performs the following actions:
  6. Removes any stored procedures that are about to be defined if they exist.
  7. Drops the assembly registration if it already exists.
  8. Registers the assembly.
  9. Registers the CLR stored procedures.

Running the Sample

To run the sample, do the following:
Run the sample
  1. Load the Scripts\test.sql file in Management Studio and run it. Or, run the following command at the command prompt: sqlcmd -E -I -i Scripts\test.sql

Removing the Sample

Use the following procedure to remove the SQLCLR Layer sample:
Remove the sample
  1. Load and run Scripts\cleanup.sql in Management Studio. Or, run the following command at the command prompt: sqlcmd -E -I -i Scripts\cleanup.sql


The CLR for SQL Server 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.

See Also

CLR Programmability Samples
Help and Information
  • Getting SQL Server 2008 Assistance* © 2008 Microsoft Corporation. All rights reserved.

Last edited Nov 6, 2008 at 10:18 PM by bonniefe, version 9


No comments yet.