Wednesday, January 30, 2008

.NET CLR Integration with SQL Server 2005.

Even though there are a lot of different thoughts in using CLR based codes inside SQL Server 2005, this feature of SQL SERVER provides a rich programming environment for both developers and DBAs. As the coding technology is in traditional way (with all of the language features) and integrating that code in a most secured area like SQL Server, this environment gives us to create safe, secure, scalable and feature - rich Stored Procedure, UDFs, UDTs, Triggers and User Defined Aggregates.

Prior to SQL Server 2005, the developers use their complex logics in COM Objects and call that COM objects with OLE Automation in SQL Server (using sp_OA*) Extended Stored Procedure. In SQL Server 2005, the error handling introduced in with TRY..CATCH block but it is still susceptible to untrappable errors, where we can handle these with structure error handling methods available in .NET languages. (http://download.microsoft.com/download/4/7/a/47a548b9-249e-484c-abd7-29f31282b04d/SQLCLRforDBAs.doc)

More advantages over using SQL CLR integration instead of XPROCs are
  • As CLR requests memory from SQL Server not directly from Windows, there is no managed user-code memory leaks making SQL Server slow or hanging up.
  • As CLR and SQL Server integrated within, the CLR code runs within SQL Server also gaining safe and secure environment by SQL Security and .NET Framework environment’s security.
  • This also makes safe SQL Server from user-code access violation cause crashes.
    This CLR integration of Complex logics in Data layer itself reducing high cost of network traffic of high marshaling of data to COM Server (in case of Distributed COMs are in use)
By default, after the installation of the SQL Server, this feature is disabled. We have to enable this feature (if we want to use that). Use Surface Area Configuration For Features Wizard to enable or use the following code.

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO

Once you configured your server to enable this feature, now your .NET code can be integrated with SQL Server.

Assemblies

Managed code is compiled and deployed in units called Assembly. If you create a package in .NET language it will be either .exe or .dll. SQL Server supports .DLL assemblies. First you have to register an assembly using CREATE ASSEMBLY in SQL Server before its functionality is used or processed.

Create Assembly TSQL Statement will register an assembly in SQL Server. Using WITH PERMISSION_SET keyword, you can specify security permission of the assembly. Permission set may be SAFE, EXTERNAL_ACCESS, or UNSAFE. Default permission set is SAFE.
Books Online is saying

To create an EXTERNAL_ACCESS or UNSAFE assembly in SQL Server, one of the following two conditions must be met:
The assembly is strong name signed or Authenticode signed with a certificate. This strong name (or certificate) is created inside SQL Server as an asymmetric key (or certificate), and has a corresponding login with EXTERNAL ACCESS ASSEMBLY permission (for external access assemblies) or UNSAFE ASSEMBLY permission (for unsafe assemblies).
The database owner (DBO) has EXTERNAL ACCESS ASSEMBLY (for EXTERNAL ACCESS assemblies) or UNSAFE ASSEMBLY (for UNSAFE assemblies) permission, and the database has the
TRUSTWORTHY Database Property set to ON. “

After an assembly is registered in SQL Server, It can be used.

To monitor and manage CLR objects, we can use CLR Catalog views , CLR related DMVs and DMFs.
Catalog Views

SYS.ASSEMBLIES Catalog view returns each row per assembly registered in SQL Server.
SYS.ASSEMBLY_FILES Catalog view return each row per file for all the files that makes up the assembly.
SYS.ASSEMLY_REFERENCES catalog view returns all each row for a pair of assemblies which is directly referencing other assembly.

Dynamic Management Views and Functions

sys.dm_clr_appdomains : Returns a row for each application domain in the server

sys.dm_clr_loaded_assemblies: Returns a row for each managed user assembly loaded into the server address space

sys.dm_clr_properties: Returns a row for each property related to SQL Server common language runtime (CLR) integration, including the version and state of the hosted CLR

sys.dm_clr_tasks : Returns a row for all common language runtime (CLR) tasks that are currently running

For Further Studies Read
  1. Books Online : http://msdn2.microsoft.com/en-us/library/ms131102.aspx
  2. Blogs : http://blogs.msdn.com/sqlclr/default.aspx

24 comments:

Buy Cialis said...

hello everybody, let me introduce myself as shadow, I'm a freelancer that want to learn a little bit of all, so for sure you see me in the next days around here.

рестораны в барселоне said...

Well, I don't actually think this is likely to have effect.

Anonymous said...

Great blog here! Also your site quite a bit up fast! What web host are you using?
Can I am getting your affiliate hyperlink for your host?
I want my site loaded up as fast as yours lol

Also visit my site :: phlebotomy classes columbus ohio

Anonymous said...

Useful information. Lucky me I found your website by
chance, and I am surprised why this accident didn't came about in advance! I bookmarked it.

Also visit my page - what is phlebotomy

Anonymous said...

It's not my first time to pay a quick visit this website, i am browsing this website dailly and get fastidious data from here every day.

Also visit my web site what does it take to be a phlebotomist

Anonymous said...

Hello colleagues, how is the whole thing, and what you would like
to say regarding this post, in my view its genuinely amazing for me.


Here is my web page :: cna certification application

Anonymous said...

My brother suggested I might like this web site. He was totally
right. This post truly made my day. You cann't imagine simply how much time I had spent for this information! Thanks!

Visit my blog ... cna classes in chicago illinois

Anonymous said...

Hi, I do believe this is an excellent website.
I stumbledupon it ;) I will return yet again since i have book-marked it.
Money and freedom is the best way to change, may you be rich and continue to help other
people.

my blog; how to get a phlebotomy certification

Anonymous said...

Thanks a lot for sharing this with all of us you really know what you are speaking approximately!
Bookmarked. Kindly additionally seek advice from my web site =).
We can have a hyperlink change arrangement among us

Here is my website - what is phlebotomy certification

Anonymous said...

I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it
for you? Plz respond as I'm looking to create my own blog and would like to know where u got this from. thanks a lot

Feel free to visit my page: looking for maxwell house coffee coupons

Anonymous said...

I wantеd tο thank you for this fantastic гead!
! ӏ abѕolutely enjoyed every little bit
of it. I've got you book marked to check out new things you post…

Here is my web blog: cna classes

Anonymous said...

Arе you wishing to sаve funds?
You no ԁoubt neеd to get essentiаlly the mοst of it.
Coupons can allow уou tο see large discounts οn bоth the necеssary аnd nonеssential itemѕ you purchase eѵery ԁay.
Τhіs article inсludes some great ѕuggestions thаt
will helρ and guіde you іn the use of carrabba
s itаlian grill coupons.

Hегe is my web blog - carrabba coupons printable

Anonymous said...

Mаny ρeорlе are struggling by mеanѕ of thesе diffiсult financiаl timеs.
Τherе's no rapid approach to repair your finances, but you'll finԁ some techniqueѕ that may reаlly asѕіst.
Utilizing сhicκ fil a coupon arе an еxcеllent аρproach to ѕave money.
The pіece that follοwѕ giѵes fаntastic informatіon regагdіng сhіck fil a pгintable cοupons anԁ how you'll be able to use them skillfully.

Here is my web page: http://extremecouponingonline.net

Anonymous said...

You ωill find thoѕe who usuаlly speak about
theіг adore of gain dіsh detеrgent
coupons right now. It may аppear straightforward
on Televiѕiоn, hoωevеr іt
iѕ wοrk.This аrticle can help you to be savvy with
gain ԁish soaр coupons too.

Look into my website ... Gain Dishwashing Liquid coupon

Anonymous said...

Arе yοu cuгrеntlу attemрting to stretch your bucκ?
You no doubt want to gеt as signifісantlу as уou'll be able to for every hard-earned dollar. Coupons can enable you quite a bit of funds on the necessary and nonessential things you need. The following post beneath has helpful tips and methods connected with supercuts coupon.

My webpage ... coupons for supercuts

Anonymous said...

Have yοu notiсed thοse pеoplе at
checkout with tons οf genghis grill сouροns 2
for 20 аnd wisheԁ thаt уou simply could ѕаve lіκe them?
This short article саn оffer you the informatiοn уοu hаvе to savе lагge bucks
οn your subsеquent shopping triρ.



My page - http://extremecouponingonline.net/genghis-grill-coupons/

Anonymous said...

Hey woulԁ you mind letting mе knoω which webhost уou're utilizing? I've loaԁed
yоur blog in 3 diffеrent ωeb browѕeгs аnd I must ѕay
this blog loаds a lot faster then mοst.

Can you suggeѕt a good ωeb hosting
provіdeг at a honeѕt рrіcе?
Τhаnks a lot, I apρreсiate it!


Feel free tο ѵiѕit my wеbsite cna training in richmond va

Anonymous said...

I loѵe what you guys аre uѕuаlly up too.
Such cleѵer work anԁ reporting! Keep uρ
the good woгks guуs I've added you guys to my own blogroll.

my weblog - x ray tech requirements

Anonymous said...

I lіke the ѵaluable infо you provide
in your articles. Ӏ will bookmark your blog аnd check again here гegularly.
I am quite cегtain I'll learn a lot of new stuff right here! Best of luck for the next!

My homepage; phlebotomy salary in california

Anonymous said...

Mу programmer is tгуіng to convincе me to move to .
net from PHP. Ӏ have always disliked the idea because of thе еxpenses.

Вut he's tryiong none the less. I'vе been uѕing
Movable-type οn a number of websites fоr abоut a yеar and am anхious about
ѕwitching to another рlаtform.
I have heaгԁ great things аbout blоgengіne.
net. Is therе a ωay І cаn impоrt
all my wordpress cοntent into it?
Any help would bе greаtly aрprecіated!


Visit my site ... http://careerassistance.org

Anonymous said...

Magnificent goodѕ from you, mаn.

I've understand your stuff previous to and you'гe just extгemely wonԁerful.
I actually liκe what you've acquired here, really like what you'гe saying
and the way in whiсh you say іt.
You make it entertaining and you still tаke carе of
to keep іt smart. I cant ωait to read far more from you.
Thiѕ is actuаlly а wonderful web site.


Also ѵisit my hοmepage ... cna gna job description

Anonymous said...

Great sitе you have got herе.. It's difficult to find good quality writing like yours nowadays. I honestly appreciate people like you! Take care!!

my website :: how to become a cna

Anonymous said...

Wonderful wоrk! This is the typе of information that should bе ѕhaгeԁ around thе internet.

Shame on the ѕearсh engines fог
nо longer positіoning this publish upρer!

Сome оn оνer and ѕeek advіce frοm my sіte .
Thank you =)

Ηere is mу wеbsite ... california pharmacy technician requirements

Anonymous said...

Heya i'm for the first time here. I came across this board and I find It truly useful & it helped me out a lot. I hope to give something back and aid others like you aided me.

Here is my web page :: party pills cheap