Some times we need to refer the .NET Framework assemblies and namespaces that are not by default available for development of CLR database in SQLServer. In such scenarios we can register the assemblies in to the SQLServer and then can access them in our CLR objects. The System.Management namespace/assembly which is not available in SQLServer 2005 can be registered using the following Transact-SQL statement. |
While registering the assembly in to SQLServer one thing you should make sure that the right folder and version of the .NET Framework are being referenced. CREATE ASSEMBLY Management FROM
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll'
WITH permission_set=unsafe;
Go
It has to be noted that the assembly must be declared as Unsafe since it accesses resources that are outside of SQL Server. Once you have done registering the assembly in to the SQL Server, then you also required to add reference to the new assembly that has been registered on the SQL Server in your Database project. Since the function references objects that are not in SQL Server, you also need to declare the project Unsafe (Project | Options | Database | Permission Level). |