Execute .NET Code under SQL Server 2005/2008

Follow the steps as given below

//1st create a sample C# library as like given below
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;

namespace ClassLibraryForSQL
{
    public class MyFirstSQlClass
    {

        public MyFirstSQlClass()
        {
       
        }
        [SqlProcedure]
        public static void GetMessage(SqlString message, out SqlString result)
        {
            result = "you have passed " + message + " string by your procedure;";
        }

    }
}
-------------------------------------


//Before starting, you have to enable the managed code execution feature of the SQL Server which is //disabled by default.

sp_configure 'clr enable', 1
go
reconfigure
go
--------------





//The assemblies that we are going to register should be registered in UnSafe mode by below 2 steps.
 
alter database master set trustworthy on
go
----------------------

create assembly ClassLibraryForSQL
authorization dbo
from 'E:\Chandra Test Application\Excel Management\MyTestMVCWithDI\ClassLibraryForSQL\bin\Debug\ClassLibraryForSQL.dll'
with permission_set=unsafe
go
--------------------



//Create procedure to execute your c# code

create procedure MyFirstSQLToCsharpProc
@mymessage nvarchar(200),
@myresult nvarchar(max) output
as
External Name ClassLibraryForSQL.[ClassLibraryForSQL.MyFirstSQlClass].GetMessage
go
-----------------

declare @myresultdats nvarchar(max)
exec MyFirstSQLToCsharpProc 'Hello Chandra', @myresultdats output

print @myresultdats


----------

you have passed Hello Chandra string by your procedure;


//please like and comment if it helps you. Cheers 

Comments

Popular posts from this blog

HOW TO Use the NumericUpDown Control

How to show help text in page on mouse move

Get control name in Page_Load event after post back by any control