|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionFirst of all I would like to congratulate Microsoft Development Team for their superb and innovative technology using which we can use managed code written in any of the .net supported language into Ms SQL Server 2005 Stored Procedure. If I make it simple then "Now you can call any function written in .net class library in your SQL Server stored procedure". This technology will allow us to use the features of .net language with the extendibility of SQL Server. DescriptionI got a article of using MSMQ from SQL Server in codeproject written by a gentlemen regarding MSMQ, and Messaging was used from SQL SP. I tried to show the constraints and limitation of using such assemblies and the reguired configuration in SQL Server 2005, so that the developers can start with. And I am sharing the practical problems which I faced in the actual implementation of this. 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 your server is ready to run managed code under its runtime. But let me tell you it's not the magic but it's the same technology being used by the .net framework. The only important part is that now SQL Server also is able to execute code on CLR. Isn't it great? Before writing this article I did a lot of Research on this, I got a lot of articles explaining the same. But I faced a lot of problems because things were scattered. Now I am trying to simulate the whole procedure in this article. I did this study because of my specific requirements. I was using Message Queuing Service of Microsoft in C# .net. But after some time I got a requirement in which I need to send a message from Stored Procedure to MSMQ Queue. And till that time for me it was possible by using System. Messaging namespace of .net runtime only. I know most of the developers are wondering about MSMQ. Don't worry I won't go into the details of that right now. We will start with a simple application which will use a simple string returning function of C# .net class library. Let's start with the class library. using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Data.SqlTypes; using System.Data.SqlClient; using Microsoft.SqlServer.Server; namespace ManagedCodeAndSQLServer { public class BaseFunctionClass { #region "Default Constructor" public BaseFunctionClass() { } #endregion #region "Welcome Function" /// <summary> /// This function will be called from the SQL Stored Procedure. /// </summary> /// Name /// <returns>Welcome Message</returns> [SqlProcedure] public static void GetMessage(SqlString strName, out SqlString strMessge) { strMessge = "Welcome," + strName + ", " + "your code is getting executed under CLR !"; } #endregion } } Important PointsI hope you are able to notice some new things in the above code. First of all the Now build the project and leave it because your work from the .net class library point of view is over. Now come to your SQL Server again. I hope you still remember that we enabled CLR integration in our server at the start of the article. If not, then don't wait for me to come and enable it. ALTER DATABASE TestingCLR SET TRUSTWORTHY ON GO Now Register CREATE ASSEMBLY ManagedCodeAndSQLServer AUTHORIZATION dbo FROM 'E:\Important\SQL\ManagedCodeAndSQLServer\ManagedCodeAndSQLServer\bin\ Debug\ManagedCodeAndSQLServer.dll' WITH PERMISSION_SET = UNSAFE GO
CREATE ASSEMBLY Messaging AUTHORIZATION dbo FROM 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll' WITH PERMISSION_SET = UNSAFE GO To use the normal functionality we need not to registered dll from .net runtime as in our case Now in your server explorer, go to your TestingCLR database, select Programmability and in Assemblies you will find an assembly registered with the name given by you i.e., "HelloDotNet". Now you are almost done. You should know the full path (namespace convention) of the function that you are going to use from that assembly. In our case it is: ManagedCodeAndSQLServer.BaseFunctionClass and GetMessage is our function to be used. Now I am creating a simple Stored Procedure to use this assebmly as following: CREATE PROCEDURE usp_UseHelloDotNetAssembly
@name nvarchar(200),
@msg nvarchar(MAX)OUTPUT
AS EXTERNAL NAME ManagedCodeAndSQLServer.[ManagedCodeAndSQLServer.
BaseFunctionClass].GetMessage
GO
To execute the Procedure: DECLARE @msg varchar(MAX) EXEC usp_UseHelloDotNetAssembly 'Kittu And Tannu',@msg output PRINT @msg You will get the following output: Welcome,Kittu And Tannu, your code is getting executed under CLR ! Try it and enjoy…….. TC……
|
||||||||||||||||||||||