Readme_Assembly Cleanup 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 AssemblyCleanup sample contains a .NET Stored Procedure, written in Visual C#, that cleans-up unused assemblies in the current database by querying the metadata catalogs.

Its only parameter, visible_assemblies, is used to specify whether unused visible assemblies should be dropped or not. A value of 'false' means by default only unused invisible assemblies will be dropped, otherwise all unused assemblies will be dropped.

The set of unused assemblies are those assemblies that do not have any entry points defined (routines / types and aggregates) and there are no used assemblies referencing them directly or indirectly.

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\AssemblyCleanup.

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 copy images between SQL Server and the file system on the server.

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

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

  • SQL Server

If you have not already created the strong-name key file ExternalSampleKey.snk, generate the key file using the following instructions.

  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.


    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.

    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 <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 ExternalSampleKey.snk

    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:

  1. Compile the sample by using the provided Microsoft Visual Studio solutions or by using a command similar to the following at the command prompt:

    Sn -k keypair.snk

    Csc /reference:"C:\Program Files\Microsoft SQL Server\MSSQL.10.MSSQLSERVER\MSSQL\Binn\sqlaccess.dll" /target:library /debug /out:CS\AssemblyCleanup\Bin\Debug\AssemblyCleanup.dll CS\AssemblyCleanup\*.cs

  2. Make sure that the AdventureWorks database is installed, or create the database in which you want to install the samples.

  3. Load the Transact-SQL script in Scripts\installCS.sql or Scripts\installVB.sql into Management Studio and execute it. This file contains script to install and register the sample CLR objects into SQL Server.

To run the sample, do the following:

  1. To Load the Transact-SQL script in Scripts\test.sql into Management Studio and execute it. This script shows typical ways to use the sample.

  2. When finished with the sample, load the Transact-SQL script in Scripts\cleanup.sql into Management Studio and execute it. This script removes the sample.

To remove the sample, do the following:

  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.

Community Additions