This project is read-only.

Readme_Currency Web Service Sample


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 Currency Web Service sample for SQL Server demonstrates how to invoke a Web service from server-side common language runtime code. Suppose that there is a Web server available somewhere which can supply current currency exchange information. This sample demonstrates how to expose the result of calling that Web service as a common language runtime-based table valued function (TVF). This table valued function could be used, for example, to periodically insert new rows into the Sales.CurrencyRate rate table in the AdventureWorks database. A simple implementation of the Web service is provided to demonstrate the complete scenario, but the data used in the sample is fictitious.

When you compile samples that use SQL Server's CLR integration, the path of the version of the Microsoft .NET Framework compiler that was distributed with SQL Server must be the first Framework directory in the PATH environment variable. The location of the compiler that is included with SQL Server will be 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 version number of the .NET Framework that was included with SQL Server.

The default installation directory is C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Programmability\CLR\CurrencyWebService.
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 wants to invoke a Web service from server-side common language runtime code and use the results returned by invoking the Web service.


Transact-SQL and Visual C#.


The Currency Web Service sample uses the following features of SQL Server:

Application Area Features
Overall Invoking Web services; common language runtime table valued functions; SOAP


Before running this sample, make sure the following software is installed:
  • SQL Server. This sample is not supported for SQL Server Express.
  • 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.
  • Internet Information Service (IIS), include all necessary components of IIS which are necessary for ASP.NET applications.

Building the Sample

Before you build the sample, you must create the strong-name key file ExternalSampleKey.snk, as follows:
  1. Open a Microsoft Visual Studio or .NET Framework command prompt.
  2. Change directories to C:\Program Files\Microsoft SQL Server\100\Samples.
  3. Type the following command: * sn -k ExternalSampleKey.snk *To build the sample, do the following:
Build the sample
  1. For Windows XP and Windows 2003, if you haven't already done so using Windows Explorer click right on the CS\CurrencyWebService directory and enable web sharing for that directory.
  2. For Windows Vista if you haven't already done so set up the web application for the web service using the following steps:
  3. Click Start / Control Panel / Administrative Tools / Internet Information Services (IIS) Manager.
  4. In the Connections tree control open up the local machine and then the Web Sites folder. Click right on Default Web Site and click Add Application... Specify CurrencyWebService for the Alias, and specify the full (absolute) path to the CS\CurrencyWebService directory for the Physical path. Click OK.
  5. Grant the NETWORK SERVICE account the default permissions (Read & execute, List folder contents, and Read) for the CS\CurrencyWebService directory.
  6. If you want to see how the client web service proxy in CS\CurrencyWebClient\service.cs file was created using the WSDL.exe tool, look at the scripts\RebuildWebReference.cmd file.
  7. 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\CurrencyWebService.sln This solution compiles both the web server code and the web client code.
  8. Make sure that the AdventureWorks database is installed.
  9. At the command prompt change to the CS\CurrencyWebClient\bin\debug directory and execute the scripts\sgen.cmd batch file. This creates the CurrencyWebClient.XmlSerializers.dll library necessary to avoid dynamically generating the serializers used during the invocation of the web service. By creating this assembly and registering it with SQL Server it is possible to invoke a web service using only External Access rather than Unsafe permissions which is highly desirable.
  10. 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 location where the samples were installed. Also do this for Scripts\InstallDB.sql.
  11. 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.
  12. Open the scripts\installDB.sql file 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\InstallDB.sql This script creates the AdventureWorksCurrency database which contains the fictitious currency conversion data used by the currency web service.
  13. Open the scripts\installCS.sql. 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 does the following:
  14. Removes any stored procedures and functions that are about to be defined, if they exist.
  15. Drops the assembly registration, if it already exists.
  16. Ensures that the strong name used to sign the assemblies has the appropriate external access permissions.
  17. Registers the assembly with the web service client.
  18. Registers the assembly with the static serializers needed to avoid dynamically creating the serializers during the invocation of the web service.
  19. Registers the CLR table valued function which invokes the web service.
  20. Creates a Transact-SQL stored procedure called by the web service.

Running the Sample

To run the sample, do the following:
Run the 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 at the command prompt: sqlcmd -E -I -i Scripts\test.sql

Removing the Sample

To remove the sample, do the following:
Remove the 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 at the command prompt: sqlcmd -E -I -i Scripts\cleanup.sql


Normally for security reasons it is better to invoke Web services in the middle tier of an application. One reasonable reason to invoke Web services from the data tier would be to exchange data between SQL Server and an application running on some non-Microsoft server such as an IBM mainframe which might only be conveniently accessible via Web services. The security concerns can be mitigated by having both servers behind two layers of firewall. Web services that transmit sensitive information should use the https protocol instead of the http protocol.
The common language runtime for SQL Server 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
Other Resources
Common Language Runtime (CLR) Integration Programming Concepts
Help and Information
  • Getting SQL Server 2008 Assistance* © 2008 Microsoft Corporation. All rights reserved.

Last edited Aug 24, 2009 at 10:54 PM by bonniefe, version 10


No comments yet.