|
Accessing .NET Components From SQL Server
|
|
In the articles titled
Using COM Objects in SQL Server and
Extending SQL Server with COM Objects, we saw the basics of how to
access and use COM objects from SQL Server. With the advent of .NET, it is but
natural for us to see if SQL Server can call into .NET objects. Unfortunately,
you cannot, as
described in the following article. But don't loose heart!! You can access
.NET objects in an indirect way using the COM Interop facility provided
by .NET. Note that Microsoft makes no promise for supporting your problems
under this method, but anyways, it is good to know that you can access .NET
components if need be. In this example, we will see how to exploit this
feature.
The steps for accessing .NET objects from SQL Server is
quite straight-forward. The following are the steps:
This does look like a lot of steps, but in reality, it is quite simple :-) Let's explore each step in more detail by writing a simple .NET component that returns some information about the AppDomin in which it is running. This value is packaged as a string and sent back to the caller. Step - 1: Creating the .NET Class The first step is to create the .NET class. Since this component will ultimately interact with a COM component, it is better to define an interface and implement a class from this interface. Implementing functionality through interfaces has a major benefit for COM clients. .NET keeps interfaces consistent with previous versions when generating the COM Callable Wrapper (CCW). This helps keeps changes from breaking the COM clients. Here is the code for the .NET class.
I've called this project HelloNET.
Step - 2: Creating a strong name for
the assembly
The next step is to sign the assembly so that we can deploy it in the GAC. For this, we need to create a strong name using the SN.EXE tool and reference it inside the assembly. Modify the AssemblyInfo.vb file as shown.
The highlighted portions of the assembly information file
are the changes that I've made.
Step - 3: Generate a type library
In order to make a .NET component visible to a COM client,
we need to register the assembly using the REGASM utility.
This command line tool adds information about the class to the system registry
so that COM clients can use the .NET class transparently. To register the
assembly, execute the following command in the project directory.
Step - 4: Registering the assembly
in the GAC
In order for any COM client to see the .NET assembly, we
need to register the assembly in the Global Assembly Cache (GAC). Assemblies
registered in the GAC are called shared assemblies. If you
want only one COM client to work with this assembly, rather than registering
the same in the GAC, you can copy the assembly into the application directory.
Such assemblies are called private assemblies. To run a
private assembly from the SQL Server environment, the assembly must
be in the BINN directory of SQL Server. To register an assembly in the
GAC, execute the following command in the project directory.
Step - 5: Calling the .NET Assembly
from SQL Server
Here is the SQL Server code to access the VB DLL.
The mode of calling is quite simple. We simply create the
.NET object by referring to its ProgID (solution name and project name) and
then call the method in it. This in turn calls the .NET assembly and generates
the following output.
You can wrap these calls in a stored procedure and then
call this stored procedure. Another interesting concept is to wrap these calls
inside an extended stored procedure. Here is
an article that exposes a .NET component as an extended stored
procedure written by Kumar Gaurav Khanna.
OK, that was quite easy and interesting! Being able to access .NET code from
SQL Server opens the door to writing lot of interesting code that uses .NET
objects. Keep in mind though that the performance of your application cannot be
guaranteed to be fast. Note that there is an interop call involved apart from
the SQL Server to COM call. So do not write complex .NET code that might
interfere with the actual usage of SQL Server, which is to provide relational
access to data.
But, have fun with this capability!!
|
| Home |