Solved

Create DLL for SQL Server

Posted on 2014-01-21
20
1,313 Views
Last Modified: 2014-01-26
I've created a class in VB.Net. Now I want to turn it into a class library, register it with SQL Server and call it from a stored procedure there. What are the steps?
0
Comment
Question by:TimHudspith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 43

Accepted Solution

by:
Eugene Z earned 300 total points
ID: 39796507
what is your sql server version?

if fresh one use this step-by-step example

'The Hello World sample demonstrates the basic operations that are involved in creating, deploying, and testing a simple common language runtime (CLR) integration-based stored procedure.'

http://technet.microsoft.com/en-us/library/ff878250.aspx
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 39796509
here is the entry point for the CLR integration:
http://msdn.microsoft.com/en-us/library/ms254498%28v=vs.110%29.aspx

the actual installation of the assembly is described here:
http://msdn.microsoft.com/en-us/library/ms345099%28SQL.100%29.aspx

that should be all, so far. just do it on a dedicated test server first, just to make sure you can easily stop/restart the sql service, and replace the assembly file as needed.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39796586
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:TimHudspith
ID: 39796778
I tried to create the assembly in SQL Server (2008) but got the message:

'CREATE ASSEMBLY failed because the assembly is built for an unsupported version of the CLR runtime.'

The .dll was created in Visual Studio 2010.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39796832
are you sure that you are targeting framework 3.5?

try creating a new test project like I explain in my article.
0
 

Author Comment

by:TimHudspith
ID: 39796856
I followed one of the earlier links to MSDN. I changed the compiler settings for the .dll to .Net 3.5 and tried again to create the assembly in SQL Server. It almost seemed to work but said that my class uses the System.Drawing namespace, which it doesn't recognise, so I need to load that, but I don't know how.

I also ran the code 'select * from sys.dm_clr_properties' and see that SQL Server 2008 uses .Net 2.0, so am wondering why setting my .dll to target framework 3.5 would work?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39796872
Normally, .Net Framework 3.5 is installed on SQL 2008. Try to target 2.0 to test.
0
 

Author Comment

by:TimHudspith
ID: 39796977
Still the same.

Is there a way that I can get SQL Server to reference .Net 3.5, instead of 2.0?
And/or how can I load up the missing assembly (System.Drawing)?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39796981
install it on the server
0
 

Author Comment

by:TimHudspith
ID: 39796999
System.Drawing assembly or .Net 3.5 as a whole?

Can you tell me how to do it?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39797062
you cannot install part of it. check http://www.microsoft.com/en-us/download/details.aspx?id=21
0
 

Author Comment

by:TimHudspith
ID: 39797099
I don't need to download it. I already have .NET 4.0 on my machine, but SQLCLR is using the 2.0 framework. How do I get it to change?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39797168
could it just be that you have different SP on your PC and on your server?

do you have SQL server installed locally so that you can fully try it first on your PC?
0
 

Author Comment

by:TimHudspith
ID: 39797229
I'm taking a different route, creating the DLL using the SQL CLR Project template as per your website, although I've hit a hurdle right at the start. When I get to add the database reference, it lists my server, but the dropdown list is empty, none of my databases is shown.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39797237
do you have SQL server installed locally so that you can fully try it first on your PC?
0
 

Author Comment

by:TimHudspith
ID: 39797242
OK, ignore that. I just needed to add "\SQLexpress" to the server reference.
0
 

Author Comment

by:TimHudspith
ID: 39797376
Run into the same problem: the System.Drawing assembly is not a listed reference there either. Apparently it IS possible to load individual assemblies into SQL Server, as follows:

CREATE ASSEMBLY [System.Drawing] AUTHORIZATION dbo 
FROM 'C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.Drawing.dll'
WITH PERMISSION_SET = UNSAFE

Open in new window

In this case I got the message that the owner must have UNSAFE ASSEMBLY permission and that the database must have the TRUSTWORTHY property on. I fixed the TRUSTWORTHY property, and am now trying to find out how to change the UNSAFE ASSEMBLY setting.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39797401
sorry but I cannot help you on that topic
0
 
LVL 43

Assisted Solution

by:Eugene Z
Eugene Z earned 300 total points
ID: 39797742
see
PERMISSION_SET = { SAFE | EXTERNAL_ACCESS | UNSAFE }
ALTER ASSEMBLY
http://technet.microsoft.com/en-us/library/ms186711(v=sql.100).aspx
0
 

Author Comment

by:TimHudspith
ID: 39809897
To set unsafe assembly permission I had to change the database owner to the system admin account. Then it worked.

USE <DatabaseName>
GO
EXEC sp_changedbowner ‘sa’
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question