Introduction
In this article I am going to talk about a cool new feature of SQL Server 2005 called "CLR Integration", its advantages and how to program database objects using managed code with a simple project in Visual Studio 2005 beta 2.
What is CLR Integration?
The common language runtime which is the core .NET Framework component is now integrated with SQL Server 2005 code named Yukon and called as CLR Integration. So what it means to developers is, the database objects like user defined types (UDT), user defined functions (UDF), tables, stored procedures and triggers can be built using C#, VB.NET or any .NET supported languages. Developers can leverage the rich features of the managed code such as cross language integration, object life time management, code access security, etc., for programming database objects. And also the object -oriented capabilities of the .NET supported languages. TSQL is good at data access and management but it's not a full fledged programming language.
SQL Server essentially acts as the operating system for the CLR when it is hosted inside SQL Server. The CLR calls low-level routines implemented by SQL Server for threading, scheduling, synchronization, and memory management. These are the same primitives that the rest of the SQL Server engine uses.
Advantages of CLR Integration
There are numerous advantages provided by the CLR integration in SQL Server 2005
- We can use the .NET Framework Base Class Libraries (BCL) while creating the stored procedure and triggers for complex execution logic. Also for string manipulation, cryptography and file management.
- We can write better code using the object - oriented capabilities such as encapsulation, polymorphism and inheritance provided by C#, VB.NET. We can make the code more organized and manageable.
- Managed code ensures type safety. Before the code gets executed, CLR verifies that the code is safe.
- Provides better memory management. The CLR calls SQL Server primitives for allocating and de-allocating its memory. Because the memory used by the CLR is accounted for in the total memory usage of the system, SQL Server can stay within its configured memory limits and ensure the CLR and SQL Server are not competing with each other for memory.
- In general managed code gives better performance but with the CLR integration there are some performance considerations. The performance varies depending on the context and usage of the managed code. For example, all memory intensive functions which do not access data are better to write in managed code for optimal performance. However the TSQL functions perform data access more efficiently than CLR integration.
TSQL Vs. CLR Integration
Depending on the context you have to take the decision whether to use TSQL or a managed code. I have stated the situations to make your decision simple.
Use CLR integration in the following situations.
- When the program requires complex logic and this can be achieved using the object orientation, exception handling and complex conditional constructs.
- When the program requires usage of .NET Base Class Library (BCL) for cryptography, handling file system, calling web services or any other tasks that are not impossible with TSQL.
- When the program will be CPU intensive. Since managed code is always compiled so it runs more efficiently.
- Before using any extended stored procedures, check if the same functionality can be achieved using managed code. If so then go for managed code for type safety.
Use should use TSQL for creating and managing the database objects with its procedural language features. As it's highly optimized for it. Don't use managed code anytime when it just needs to access the data and nothing else.
Join the conversation! Your thoughts help the community grow.