Implementing Managed code in SQL Server 2005 using Visual Studio.NET 2005 - Introduction to Managed Code
(Page 2 of 5 )
The code that gets executed in a "managed" environment is referred to as Managed Code. Code that you develop with a language compiler that targets the runtime (in this case, it is CLR) is called managed code; it benefits from features such as cross-language integration, cross-language exception handling, enhanced security, versioning and deployment support, a simplified model for component interaction, and debugging and profiling services.
Managed and unmanaged programming models are dissimilar in many respects. The following figure (Fig 1) shows the defining characteristics of each model.

The following sections explain each of those characteristics in more detail.
Coding models
Unmanaged objects always communicate through interfaces; managed objects and classes can pass data directly without implementing interfaces. By default, COM interop generates a class interface to expose managed functionality through an interface to COM when the object or class does not implement one.
Error handling mechanisms
COM methods usually return an HRESULT, indicating that the call succeeded or failed. Managed code incorporates exceptions. By default, COM interop maps managed exceptions to failure HRESULTs.
Identities
GUIDs identify a specific unmanaged type and provide no location information for that type. Strong names consist of a unique assembly name in addition to a type name. Because the assembly name uniquely identifies the type, you can reuse a type name across multiple assemblies. An assembly also introduces publisher key, version, and location information to a managed type. Interoperation services generate GUIDs and are strong-named as required.
Type compatibility
Types vary between managed and unmanaged code, and also among languages.
Type definitions
If you are accustomed to working with type libraries, you know that they contain only public types. Moreover, a type library is optional. In the managed programming model, type information is mandatory for all types. Interoperation services provide tools that convert type libraries to metadata in assemblies, and metadata to type libraries.
Type safety
Unmanaged compilers provide no type checking on pointer types, making the code susceptible to potentially harmful activity. In general, managed code requires a higher level of trust. Programmers can continue to use pointers in managed code, although the code has restrictions due to its unsafe behavior. Interoperation services prevent untrusted, managed code from accessing unmanaged code.
Versioning
COM interfaces are immutable. If you change an interface, you must rename it with a new GUID. Managed types can evolve, keeping the same name.
We have covered CLR, Managed Code and other terms. Let us examine the practical way of implementing Managed Code inside the SQL Server 2005 database.
Next: Creating Managed Code using Visual Studio.NET 2005 >>
More MS SQL Server Articles
More By Jagadish Chaterjee