What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12475 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > SQL Server integration with CLR aka SQL CLR (Part I - Introduction and background)

SQL Server integration with CLR aka SQL CLR (Part I - Introduction and background)

Article posted by Vishvvas on 12/26/2011 | Views: 2637 | Category: .NET Framework | Level: Advance | Points: 250 red flag


This article discusses the feature of integrating the CLR (Common Language Runtime) into the SQL server. This is first write-up of multi-part series on this subject and is mostly an excerpt from referenced article/s.

Introduction and background

 

It’s quite a while after the SQL Server integration with CLR aka SQL CLR has hit the scene with SQL 2005. As a knee jerk reaction, language and DB developers/ administrators had a lot of hue and cry and the myths, misconceptions about it made more rounds in developers circle than the real SQL CLR features and usage.

Especially DB guys had lot of apprehensions about the security and performance. Also there was fear of it ringing the death bells for TSQL(Transact- SQL).

Fortunately, in reality SQL CLR is not like what it was taken for. It is not to replace TSQL rather it can better be made work in conjunction with TSQL. The overall developer community has still to embrace SQL CLR it its fullest abilities.

The security and performance are also the topics which have come up because of lack of understanding of SQL CLR. We will discuss these points as we go ahead.


Objective

To have a refreshed look at the SQL server integration with CLR by exploring What is SQLCLR, Why SQLCLR, and through few good examples, we would try to understand the power of SQL CLR

What is SQL CLR?

CLR i.e. Common Language Runtime is no more a black box for .net community and to reiterate, it is the heart of .NET framework which provides execution environment for code, provides services for type safety, exception handling, thread management, compilation (JIT), and memory management. The code which targets and runs in CLR is termed as managed code.

With SQL 2005 appearing at the developer’s desk, CLR hosting in SQL server was an additional feature introduced by Microsoft. This hosting enabled language users to author (and develop) the database items like stored procedures, user defined functions, triggers, UDT (user defined types) and aggregates in the managed code itself. This can be deployed for SQL server (where in the CLR feature is enabled). This deployment can be done through Visual Studio and also through TSQL, resulting in creating assembly in SQL server.

The overall steps to implement SQL CLR are as following





Why SQL CLR?


We are still to go through some demonstrations; still we can discuss why one should look forward to, while settling for SQL CLR.

1.    Benefits of managed code

Compilation and Performance: Everybody knows that the managed code gets compiled to native code before execution (read about JIT for further details) giving the advantage on performance. TSQL is not managed code and hence doesn’t have this facility. Managed code can outscore T SQL for complex tasks like string operations, regular expressions and it can take advantage of BCL for file access, math operations, cryptography etc.

Security: The managed code has implements security essentials through Code Access Security (CAS), code links and application domains which in turn help to prevent certain operations. To comply with this, SQL 2005 (and above) employs CAS to secure such managed code. This security and safety can result into better alternative for extended stored procedures.

Better Programming Model: No doubt that the .Net framework languages are richer than T SQL in terms of constructs (i.e. types like arrays, collections, classes, for each loops etc. for data computation and manipulation) and programmability.

2.    Standardized development

As Visual studios for .net, embed such database development, developers have consistent tools for database development, debugging as they would have for other .net framework services and components.

3.    Testability through development environment

The feature for testing such database objects in SQL CLR is added into Visual Studio eliminating the need for accessing the database management and environment.

T SQL or Managed Code

Choosing between T SQL and managed code is not very intriguing task as it would sound. As mentioned earlier we are aware of the benefits of managed code and simultaneously we know what T SQL offers. The optimal approach would be use managed code for complex or CPU sensitive operations and also when libraries in .NET framework needs to be used and T SQL for data access mechanism.

Another advantage of managed code is that it can sit on client computer (nowadays client computers are also powerful and their utilization can be improved) while T SQL can’t.

Extended Stored Procedures and Managed Code

Extended stored procedures are ways to extend the functionality of SQL server wherein a function can be loaded and executed dynamically within a dll facilitating the actions and communications outside the SQL server.

The obvious choice when extended stored procedures are compared with managed code, is the managed code because of type safety, thread management and synchronization and seamless integration.

Summary and Conclusion

This article focuses on the theoratical part of SQL server integration i.e. what is SQL CLR And why SQL CLR. The SQL CLR is discussed in concise manner to throw light on the benefits and the steps to create such project. As usual any new technology poses challenges to architects and developers as when to use such technology and why. To help on this front, the process of choosing between the T SQL, extended stored procedure and SQL CLR (as a managed code) is discussed briefly. So definitely T SQL is going to stay (who knows for how long though).

Hope this helps to refresh our know-how of SQL CLR and readies us to delve further.

In the next part, we would see SQL CLR in action.

HAPPY PROGRAMMING!!!

Reference

Please see this link.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:18 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, May 30, 2011
Level:Bronze
Status: [Member]
Biography:Over 16 years of experience, worked across different technologies and programming languages like PB,VB, ASP. C++, C#, VB.NET
>> Write Response - Respond to this post and get points
Related Posts

This article describes how to create XML file and also shows various ways of reading XML File in .NET using XmlTextReader, XmlDocumenet, XPathDocument, DataSet and XmlDataSource control.

This article has 12 important FAQ's and will cover Unit testing, load testing, automated testing, database testing and code coverage.

Regex has been the most popular and easiest way of writing validations. The only big problem with regex has been the cryptic syntax. Developers who are working on projects with complicated validation always refer some kind of cheat sheet to remember the syntaxes and commands.

The purpose of this article is to create a simple Task Management System(TMS) that will help the user to create a task, edit the task and to view the same by using MEF 4.0, WCF , Entity Framework 4.0 with some architectural patterns etc.

This article demonstrates how to copy DLL from Global Assembly Cache.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 3:21:22 AM