Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 10: Implementing Managed Code in the Database

Similar presentations


Presentation on theme: "Module 10: Implementing Managed Code in the Database"— Presentation transcript:

1 Module 10: Implementing Managed Code in the Database

2 Overview Introduction to the SQL Server Common Language Runtime
Importing and Configuring Assemblies Creating Managed Database Objects

3 Lesson 1: Introduction to the SQL Server Common Language Runtime
What Is the .NET Common Language Runtime? Advantages of Managed Code SQL Server 2005 CLR Integration Managed Code vs. Transact-SQL

4 What Is the .NET Common Language Runtime?
.NET Framework Class Library Support Thread Support COM Marshaler Type Checker Exception Manager Security Engine Debug Engine MSIL to Native Compilers Code Manager Garbage Collector Class Loader

5 Advantages of Managed Code
Description Programming language Choice of programming language Type safety Type safety through the common type system Security Security through code access security and role-based security Fast development Fast development because of extensive base class library Interoperability Interoperable with unmanaged code

6 SQL Server 2005 CLR Integration
Run managed code within a database by using in-process assemblies Create managed stored procedures, triggers, user-defined functions, user-defined types, and aggregates Integration benefits: Enhanced programming model Enhanced safety and security Common development environment Performance and scalability

7 Managed Code vs. Transact-SQL
Use managed code for: Procedures that feature complex logic Access to the .NET Framework class library CPU-intensive functions Use Transact-SQL for data access with only basic procedural logic

8 Lesson 2: Importing and Configuring Assemblies
What Is an Assembly? Syntax for Importing an Assembly Assembly Trust Levels Demonstration: Importing and Configuring an Assembly

9 What Is an Assembly? Compiled code containing types, methods, and metadata Unit of deployment for managed code Executes in-process within the SQLCLR DLL .NET Code Compiled Assembly SQLCLR

10 Syntax for Importing an Assembly
Specify a database object name for the assembly Reference the assembly file Set assembly trust level (optional) CREATE ASSEMBLY Contacts FROM 'C:\ContactsApp\Contacts.dll' WITH PERMISSION_SET = SAFE -- default value CREATE ASSEMBLY Contacts FROM 'C:\ContactsApp\Contacts.dll' CREATE ASSEMBLY Contacts

11 Assembly Trust Levels PERMISSION_SET Description Default setting SAFE
Recommended for most scenarios EXTERNAL_ACCESS Access external resources such as files or registry Use only if essential UNSAFE Unrestricted internal and external access Avoid due to potential security risk

12 Demonstration: Importing and Configuring an Assembly
In this demonstration, you will see how to import and configure an assembly

13 Lesson 3: Creating Managed Database Objects
Overview of Managed Database Objects Managed Stored Procedures, Triggers, and Functions Managed Aggregates and User-Defined Types Demonstration: Creating Managed Database Objects

14 Overview of Managed Database Objects
.NET Assembly SQL Server 2005 Namespaces Aggregate Functions User-Defined Types Classes Stored Procedures Triggers Methods User-Defined Functions

15 Managed Stored Procedures, Triggers, and Functions
Use CREATE PROCEDURE, TRIGGER, or FUNCTION Link to the assembly by using EXTERNAL NAME Use the database object as normal CREATE PROCEDURE Person.UpdatePhoneList AS EXTERNAL NAME Contacts.[Contacts.PhoneList].SaveList CREATE PROCEDURE Person.UpdatePhoneList AS Assembly Name Class Name with Namespace Method Name EXEC Person.UpdatePhoneList

16 Managed Aggregates and User-Defined Types
Use CREATE AGGREGATE or TYPE Link to the assembly by using EXTERNAL NAME Use the database object as normal CREATE AGGREGATE nvarchar(4000)) RETURNS nvarchar(4000) EXTERNAL NAME Utilities.[Utilities.Concatenate] CREATE AGGREGATE nvarchar(4000)) RETURNS nvarchar(4000) SELECT AccountNumber, dbo.Concatenate(SalesOrderNumber) Orders FROM Sales.SalesOrderHeader GROUP BY AccountNumber

17 Demonstration: Creating Managed Database Objects
In this demonstration, you will see how to: Create a managed user-defined function Create a managed user-defined type

18 Lab: Implementing Managed Code in the Database
Exercise 1: Importing an Assembly Exercise 2: Creating Managed Database Objects


Download ppt "Module 10: Implementing Managed Code in the Database"

Similar presentations


Ads by Google