This project is read-only.

Large Object (LOB) Handling

[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 "Katmai". It will not work with any version of SQL Server earlier than SQL Server 2005.
The HandlingLOBUsingCLR sample for SQL Server demonstrates transferring large objects (LOBs) between SQL Server and a file system that is available to the server by using common language runtime (CLR) stored procedures. This sample demonstrates how to access files in server-side code, and then invoke both dynamic queries and stored procedures from CLR-based stored procedures. It also demonstrates how to unregister and register CLR methods and assemblies by using Transact-SQL.

When you compile SQLCLR samples, 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\HandlingLOBUsingCLR.


A developer wants to copy images between SQL Server and the file system on the server.


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


The HandlingLOBUsingCLR sample uses the following features of SQL Server:

Application Area Features
Overall CLR stored procedures; calling Transact-SQL stored procedures from CLR stored procedures; the VARBINARY(MAX) data type.


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 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 SDK.

Building the Sample

If you have not already created the strong-name key file ExternalSampleKey.snk, 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 Visual Studio 2005, point to Visual Studio Tools, and then click Visual Studio 2005 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. At the command prompt, use the change directory (CD) command to change the current folder of the command prompt window to the Samples folder. Note: To determine the folder where samples are located, click Start, 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 <systemdrive_>:\Program Files\Microsoft SQL Server\100\Samples.
  3. At the command prompt, run the following command to generate the key file: sn -k ExternalSampleKey.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 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\HandlingLOBUsingCLR.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 and Scripts\InstallVB.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 does the following:

Running the Sample

To run the sample, do the following:

Run the sample

  1. To read an image from the database, invoke a script similar to the following: exec GetPhotoFromDB 70, 'C:\Temp\', 'test6.gif' go Make sure the directory you specify exists.
  2. To write an image into the database, invoke a script similar to the following: exec PutPhotoIntoDB 70, 'C:\Temp\', 'test1.gif' go Any errors will be reported to the error.log file in the directory that you specify. For scripts that are similar to the ones in this procedure, see the Scripts\test.sql file.

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


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.
© 2007 Microsoft Corporation. All rights reserved.

Last edited Mar 6, 2007 at 9:00 PM by bonniefe, version 1


No comments yet.