Tuesday, April 17, 2012

Execute .NET Code under SQL Server 2005

Hi all,
let us start with a interesting topics - registering .Net Dll into Sql Server 2005 and call its methods.
Before starting, you have to enable the managed code execution feature of the SQL Server which is disabled by default. To enable it, execute the following code as a query in your SQL Server query editor.
sp_configure 'clr enable', 1
GO
RECONFIGURE
GO
Now Set trust worthy option of particular database
ALTER DATABASE TestingCLR SET TRUSTWORTHY ON
GO
Now register sqlServerTest.dll under that using the following code:
CREATE ASSEMBLY SqlServerTest
AUTHORIZATION dbo
FROM 'E:\Important\SQL\sqlServerTest\sqlServerTest\bin\debug\sqlServerTest.dll'
WITH PERMISSION_SET = UNSAFE
GO
please note here if you are using some functionality into your Dll like sending mails or messages.
for example if you have implemented system.messging then you need to registered assembly for system.messaging like below.
CREATE ASSEMBLY Messaging
AUTHORIZATION dbo
FROM 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll'
WITH PERMISSION_SET = UNSAFE
GO
Now call this registered Dll functions using stored procedures like below..
CREATE PROCEDURE usp_UseSqlAssembly
AS
BEGIN
 @results = SqlServerTest.getResults()
END
do changes according to your need....
Happy coding...

No comments:

Post a Comment