DATABASE

SQL Server 2008 : Using the CLR - CLR and Managed Code Explained

10/11/2010 2:15:58 PM
The CLR is the execution environment provided by the Microsoft .NET Framework. It is likely that you had to install the .NET Framework at one time or another, usually as a prerequisite for installing an application. The .NET Framework is a new-generation application platform, and many modern applications, including SQL Server, use it as the execution environment. The .NET Framework provides developers with the ability to compile applications written in a .NET-compliant language. The .NET Framework serves as runtime for these applications. Figure 1 shows the key components of the .NET Framework.
Figure 1. The .NET Framework


The .NET Framework is installed on an operating system and is used to access the operating system’s services during runtime. There is a version of the .NET Framework for 32-bit and 64-bit Windows platforms, as well as some other operating systems like Windows CE and Windows Mobile. Application services exposed by applications like SQL Server, IIS, WMI, COM+, and many more are available to the developer through the .NET Framework. The Base Class Library provides commonly used functionality to developers in the form of classes and namespaces. We will examine the Base Class Library in more detail later in this chapter.

Developers usually use the Visual Studio .NET Integrated Development Environment (IDE) to create .NET applications. However, the IDE is not absolutely necessary, and it is possible to create simple text source code files and compile them using a .NET compiler. You can create .NET applications in any .NET-compliant language to suit the specific functionality you wish to implement. Over 60 major language families are available for the .NET Framework, each with many variants and branches. The most popular .NET languages are C# and Visual Basic .NET. Support for these languages is available out-of-the-box when you install Visual Studio .NET.

The .NET Framework includes a compiler component for each .NET-compliant language. This component compiles the source code written in the specific high-level language to the lower-level Microsoft Intermediate Language (MSIL). When the application is running, the CLR translates MSIL to native machine instructions. This is done by a component known as just-in-time (JIT) compilation. It is important to understand that no matter which .NET language you develop your application in, it will be compiled to the one common language—MSIL. This is from where the word common in Common Language Runtime originates.

The CLR is responsible for executing MSIL code. The CLR also manages the code that runs within it by providing the code with runtime services. Figure 2 outlines some of the services provided by the CLR.

Figure 2. Common Language Runtime Services


The CLR manages code execution by providing mandatory stability and security-checking engines, as well as resource management. For example, the Garbage Collection service provided by the CLR ensures optimal memory management. Thread management provides parallel processing capabilities. The Base Class Library support, error management, and debugging services create a consistent development environment for all .NET languages.

Code that runs within the CLR is known as managed code, as the CLR provides it with management services like those mentioned above. The term is often used to distinguish between .NET code and native code (for example, code written using Visual Basic 6 or C++). Native code runs outside the CLR and is referred to as unmanaged code. Unmanaged code is not subject to CLR services, like code access security. Within SQL Server, all objects that use CLR integration are written as managed code. Extended stored procedures (XPs) are unmanaged code and are written using C++.

Additionally, XPs are deprecated, and support for this feature is likely to be removed in future versions of SQL Server.

The CLR abstracts essential services such as memory and security management away from developers, allowing them to concentrate on the specific functionality of their code.

New & Noteworthy...: Versions of the .NET Framework Supported by SQL Server 2008

SQL Server 2008 supports assemblies targeted at .NET Framework 2.0 or later. During installation of SQL Server 2008, .NET Framework 2.0 Service Pack 1 will be installed automatically. SQL Server 2008 does not support assemblies targeted at .NET Framework 1.0 and .NET Framework 1.1.

Visual Studio 2008 is the latest version of Visual Studio—the Microsoft IDE used to create .NET Framework applications. Visual Studio 2008 can be used to create a variety of applications, and one of the key new features is support for Language Integrated Query (LINQ). Both Visual Studio 2005 and Visual Studio 2008 can be used to create assemblies containing CLR objects for SQL Server 2008 CLR integration. Visual Studio 2003 and Visual Studio .NET 2001 cannot be used.

Other  
  •  SQL Azure : Database Growth-Management Strategies
  •  SQL Azure : Database-Migration Strategies
  •  SQL Server 2005 : Implementing Service Broker
  •  SQL Server 2005 : Importing and Exporting Data
  •  Understanding Snapshot Isolation
  •  SQL Server 2008 : Programming Objects - Implementing Triggers
  •  SQL Server 2008 : Programming Objects - Implementing Stored Procedures
  •  SQL Server 2008 Command-Line Utilities : The sqlservr Command-Line Utility
  •  SQL Server 2008 Command-Line Utilities : The sqldiag Command-Line Utility
  •  SQL Server 2008 Command-Line Utilities : The bcp Command-Line Utility
  •  SQL Server 2008 Command-Line Utilities : The tablediff Command-Line Utility
  •  SQL Server 2008 : Programming Objects - Implementing Functions
  •  Surviving Changes to Columns
  •  Surviving Changes to the Signature of a Stored Procedure
  •  Exploring the T-SQL Enhancements in SQL Server 2005 : The PIVOT and UNPIVOT Operators
  •  Exploring the T-SQL Enhancements in SQL Server 2005 : Common Table Expressions
  •  SQL Azure Data Access
  •  SQL Azure Architecture
  •  SQL Server : Transactions and Exceptions
  •  SQL Server : Exception Handling
  •  
    Most View
    Programming Hashing Algorithms (part 4) - Hashing Streamed Data
    Implementing Client Access and Hub Transport Servers : Understanding the Client Access Server (part 2)
    Year End 2012 - Reviews & Rankings (Part 2)
    Oracle Coherence 3.5 : Planning Your Caches - Backing maps
    Letter Of The Month – November 2012 (Part 2)
    PlayStation Vita - The most powerful portable console yet
    Adobe Story Plus - Collaborative Screenplay Software
    Blind SQL Injection Exploitation : Using Response-Based Techniques
    Silverlight Recipes : Managing XAML Resources
    Toshiba Portege Z830 - Flexi Thin
    Top 10
    Windows Phone 8 In-Depth Review (Part 6)
    Windows Phone 8 In-Depth Review (Part 5)
    Windows Phone 8 In-Depth Review (Part 4)
    Windows Phone 8 In-Depth Review (Part 3)
    Windows Phone 8 In-Depth Review (Part 2)
    Windows Phone 8 In-Depth Review (Part 1)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 5)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 4)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 3)
    Xiaomi Phone 2 - High-End Specifications In A Surprisingly Cheap Package (Part 2)