C# and MS SQL Server: add System.Drawing assembly

Working with images in SQL Server 2005 is not easy because the System.Drawing.dll is not included in the assemblies with SQL Server.

This library was not added because it does not pass the CLR Verifier, and it has not been fully tested in SQL Server hosted environment (i.e. it could damage the stability and security of SQL Server). Careful consideration should be taken when attempting to manipulate images (or even load images from a file system or webservice).

To add the System.Drawing.dll assembly to SQL Server use the new create assembly statement. The database you are adding this assembly to must have its TrustWorthy property turned on. The following code demonstrates how to add System.Drawing.dll to the ImageGen database.

ALTER DATABASE yourDatabase
SET TRUSTWORTHY ON
GO
CREATE ASSEMBLY [System.Drawing]
FROM 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll'
WITH PERMISSION_SET = UNSAFE
GO

Now a managed stored procedure can be created that uses the System.Drawing.dll assembly as shown below (although do not forget to add the assembly reference in the VS project).

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Drawing;
using System.Drawing.Imaging;

Tags:

Leave a Reply

You must be logged in to post a comment.