SQL Server 2005 The Common Language Runtime (CLR) Integration Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP Web Development.

Slides:



Advertisements
Similar presentations
Intro to Sql Server Yukon for Developers Wallace B. McClure Scalable Development, Inc. Building systems today that perform tomorrow.
Advertisements

Yukon – What is New Rajesh Gala. Yukon – What is new.NET Framework Programming Data Types Exception Handling Batches Databases Database Engine Administration.
DIGIDOC A web based tool to Manage Documents. System Overview DigiDoc is a web-based customizable, integrated solution for Business Process Management.
.NET Framework Overview
Tahir Nawaz Introduction to.NET Framework. .NET – What Is It? Software platform Language neutral In other words:.NET is not a language (Runtime and a.
ADO vs ADO.NET ADOADO.NET Client/server coupledDisconnected collection of data from data server Uses RECORDSET object (contains one table) Uses DATASET.
History of.Net Introduced by Microsoft Earlier technology was VC++ and VB VC++ comes with so many library and VB was so easy to use and not flexible to.
.NET Framework Overview Pingping Ma Nov 16 th, 2006.
Introduction to the C# Programming Language for the VB Programmer.
Thursday Evening 6.30VB.NET Introduction 7.30Break for food 8.00VB.NET Migration 8.45Q & A 9.00Done.
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
Introducing the Common Language Runtime for.NET. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
ASP.NET Programming with C# and SQL Server First Edition
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Introducing the Common Language Runtime. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution engine.
SQL Server 2005 CLR Integration ADO.NET 2.0 Mike Taulty
Introduction to SQL Server 2000 Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP Web Development MCP LebDev Vice President ©
René Balzano Technology Solution Professional Data Platform Microsoft Switzerland Database Development with SQL Server Data Tools (SSDT)
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004.
Intro to dot Net Dr. John Abraham UTPA – Fall 09 CSCI 3327.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Overview SQL Server 2008 Overview Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP, MCTS Microsoft Web Development MCP ITIL.
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
Creating Business Workflow Using SharePoint Designer 2007 Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP Microsoft SQL Server.
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
1 Introduction to.NET Framework. 2.NETFramework Internet COM+ Orchestration Orchestration Windows.NET Enterprise ServersBuildingBlockServices Visual Studio.NET.
Introduction to .Net Framework
SQL Server Integration Services (SSIS) Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server (MVP) Microsoft Certified Technology Specialist.
Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08.
An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low.
© 2008 Dr. Paul Walcott – The University of the West Indies: Cave Hill CampusDr. Paul Walcott COMP6325 Advanced Web Technologies Dr. Paul Walcott The University.
Module 1: Introduction to C# Module 2: Variables and Data Types
Session 1 - Introduction and Data Access Layer
Meir Botner David Ben-David. Project Goal Build a messenger that allows a customer to communicate with a service provider for a fee.
Native Support for Web Services  Native Web services access  Enables cross platform interoperability  Reduces middle-tier dependency (no IIS)  Simplifies.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
.NET Framework & C#.
Introduction to .NET Framework
CS795/895: Introduction. Topics Distributed Systems –Availability –Performance –Web Services Security –Authentication –Authorization –Confidentiality.
Microsoft SharePoint Server 2010 for the Microsoft ASP.NET Developer Yaroslav Pentsarskyy
Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#
SQL Server 2005: The CLR Integration What Developers and DBAs need to know.
Triggers and Stored Procedures in DB 1. Objectives Learn what triggers and stored procedures are Learn the benefits of using them Learn how DB2 implements.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Module 1: Getting Started. Introduction to.NET and the.NET Framework Exploring Visual Studio.NET Creating a Windows Application Project Overview Use Visual.
Module 3: Using Microsoft.NET- Based Languages. Overview Overview of the.NET-Based Languages Comparison of the.NET-Based Languages.
Intro to dot Net Dr. John Abraham UTPA CSCI 3327.
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
PRIOR TO WEB SERVICES THE OTHER TECHNOLOGIES ARE:.
Microsoft .NET A platform that can be used for building and running windows and web applications such that the software is platform and device-independent.
Getting Started with.NET Getting Started with.NET/Lesson 1/Slide 1 of 31 Objectives In this lesson, you will learn to: *Identify the components of the.NET.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
INTRODUCTION CHAPTER #1 Visual Basic.NET. VB.Net General features It is an object oriented language  In the past VB had objects but focus was not placed.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
SQL Triggers, Functions & Stored Procedures Programming Operations.
In this session, you will learn to: Understand managed code Create managed database objects Define the Hypertext Transfer Protocol endpoints Implement.
SQL Server 2005:.NET Framework Programming in the Database Tim Sneath Architectural Engineer, Microsoft
D OTNET ONLINE TRAINING. DOTNET Online Training Course Content : Introduction to.Net Online Training NET FUNDAMENTALS Why Dot Net? The Dot Net initiative.
Introducing the Microsoft® .NET Framework
A Guide to SQL, Seventh Edition
Beyond the BDC\BCS Model
Software Design and Architecture
Module 1: Getting Started
Introduction to C# AKEEL AHMED.
Module 10: Implementing Managed Code in the Database
Chapter 8 Advanced SQL.
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Presentation transcript:

SQL Server 2005 The Common Language Runtime (CLR) Integration Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP Web Development MCP LebDev Vice President © 2006 Tarek Ghazali. All rights reserved.

CLR Integration CLR Introduction Highlights Highlights –Common type system (CTS) Mapping of data types. Programming language  Framework Mapping of data types. Programming language  Framework –Just-in-time (JIT) compilers JIT compiles intermediary language (MSIL) into native code JIT compiles intermediary language (MSIL) into native code Highly optimized for platform or device Highly optimized for platform or device –Garbage collector –Permission and policy-based security –Exceptions –Threading –Diagnostics and profiling

CLR Integration CLR Diagram Common Language Runtime Diagram Common Language Runtime Diagram Class Loader MSIL to Native Compilers (JIT) Code Manager Garbage Collector (GC) Security EngineDebug EngineType CheckerException ManagerThread SupportCOM Marshaler Base Class Library Support

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

CLR Integration Deep Integration with the Database CLR Hosting layer provides coordination CLR Hosting layer provides coordination –Assembly Loading –Memory management –Security Model –Reliability –Threads & Fibers –Deadlock detection –Execution context Windows OS SQL OS Layer SQL Engine HostingLayer CLR

VS.NET Project Assembly: “TaxLib.dll” VB,C#,C++ Build SQL Server SQL Data Definition: create assembly … create function … create procedure … create trigger … create type … SQL Queries: select sum(tax(sal,state)) from Emp where county = ‘King’ Runtime hosted by SQL (in-proc) The Developer Experience

CLR Integration Available Classes Even in supported assemblies, some APIs are not available in SQL Even in supported assemblies, some APIs are not available in SQL –Environment.Exit(), Console, etc. Potentially unreliable constructs disabled Potentially unreliable constructs disabled –No thread creation –No shared state or synchronization –No listening on sockets in server –No finalizers Eliminate functionality N/A to database Eliminate functionality N/A to database –System.Windows.Forms –System.Drawing –System.Web, …

CLR Integration SQL Server Projects in Visual Studio 2005 Project for creating managed database objects Project for creating managed database objects Automatically includes necessary references Automatically includes necessary references –System –System.Data.dll Includes templates for each object type Includes templates for each object type –Stored procedure –Trigger –User-defined function –User-defined type –Aggregate Allows immediate deployment and debugging Allows immediate deployment and debugging

CLR Integration The System.Data.SqlServer Namespace ClassDescription SqlContext Provides access to other objects, like a connection SqlConnection An open connection to a SQL Server database SqlCommand Used to send a command to the database server SqlParameter Supplies a parameter for a SqlCommand object SqlPipe Used to send results or information to the client SqlDataReader Reads the data one row at a time, forward only SqlResultSet For working with flexible server-side cursors SqlTransaction For providing transactional behavior SqlTriggerContext Provides information about the trigger action

CLR Integration Registering an assembly CREATE ASSEMBLY assembly_name CREATE ASSEMBLY assembly_name –[ AUTHORIZATION owner_name ] –FROM { | [,...n] } –[ WITH PERMISSION_SET = { SAFE | EXTERNAL_ACCESS | UNSAFE } ] – :: = – '[\\machine_name\]share_name\[path\]manifest_file_nam e' – :: = – { varbinary_literal | varbinary_expression } CREATE ASSEMBLY assembly_name CREATE ASSEMBLY assembly_name –[ AUTHORIZATION owner_name ] –FROM { | [,...n] } –[ WITH PERMISSION_SET = { SAFE | EXTERNAL_ACCESS | UNSAFE } ] – :: = – '[\\machine_name\]share_name\[path\]manifest_file_nam e' – :: = – { varbinary_literal | varbinary_expression }

CLR Integration Assembly Security -PERMISSION_SET SAFE SAFE –May not access external resources: registry, file system, or network –May access data using the current context but not via SQLClient or any other data provider –No thread processing EXTERNAL_ACCESS EXTERNAL_ACCESS –May access external resources: registry, file system, network, environment variables UNSAFE UNSAFE –May access external resources –Can use SQLClient and other data providers –Can use thread constructs –(No restrictions; similar to extended stored procedures)

CLR Integration Meta Data of Assemblies Create Assembly Details of Assembly: Sys.assemblies Assembly source code: Sys.assembly_files Assembly references: Sys.assembly_references Other meta data information SYS.OBJECTS SYS.OBJECTS SYS.ASSEMBLY_MODULES SYS.ASSEMBLY_MODULES SYS.ASSEMBLY_TYPES SYS.ASSEMBLY_TYPES

CLR Integration User Defined Functions Similar to T-SQL function Similar to T-SQL function Written in CLR language Written in CLR language –Decorated with [SqlFunction] attribute in code –Assembly loaded into the database –Function defined from assembly Limits on functions Limits on functions –must be in public class –cannot be in nested class –method must be public and static

CLR Integration User Defined Functions -Example public class MyFunctions { [SqlFunction] public static SqlString GetLongDate(SqlDateTime DateVal) { // Return the date as a long string return DateVal.Value.ToLongDateString(); }

CLR Integration User Defined Functions Properties have impact on whether or not computed column that use these functions can be indexed. Properties have impact on whether or not computed column that use these functions can be indexed. –IsDeterministic = true (it always produces the same output values given the same input values and the same database state.) –DataAccess DataAccessKind.None: Does not access data. DataAccessKind.None: Does not access data. DataAccessKind.Read: Only reads data. DataAccessKind.Read: Only reads data. –SystemDataAccess SystemDataAccessKind.None: Does not access system data. SystemDataAccessKind.None: Does not access system data. SystemDataAccessKind.Read: Only reads system data. SystemDataAccessKind.Read: Only reads system data. –IsPrecise = { true | false } (that indicates whether the routine involves imprecise computations such as floating point operations. )

CLR Integration.Net Stored Procedures (1) Capable of doing everything a T-SQL proc can do. Capable of doing everything a T-SQL proc can do. Uses a Shared method (static in C#) Uses a Shared method (static in C#) Pass parameters both ways Pass parameters both ways –OUTPUT parameters should be byref (ref in C#) Return multiple result sets Return multiple result sets

CLR Integration.Net Stored Proc Can Return (2) Numeric return code Numeric return code Count of rows affected by the command Count of rows affected by the command Scalar value Scalar value Single row Single row One or more multi row result sets One or more multi row result sets A stream of XML A stream of XML

CLR Integration Stored Procedure public class ContactCode { [SqlProcedure] public static void GetContactNames() { SqlCommand cmd = ……. …… cmd.CommandText = "SELECT FirstName + ' ' + LastName" + – –" AS [Name] FROM Person.Contact"; SqlDataReader rdr = cmd.ExecuteReader(); SqlPipe sp = …………..; sp.Send(rdr); }

CLR Integration Create Sql Server Proc. Syntax : create procedure ProcName as external name.. as external name.. Example : create procedure GetContactsName as external name assemblyname. create procedure GetContactsName as external name assemblyname.ContactCode. GetContactNames

CLR Integration Triggers public class ContactCode { [SqlTrigger(Name="ContactUpdTrg", Target="Person.Contact", Event="FOR UPDATE")] public static void Change () {SqlTriggerContext trg = SqlContext.GetTriggerContext();

DEMO

CLR Integration When to use T-SQL T-SQL better used for data access T-SQL better used for data access –All pre-SQL Server 2005 code is written in T-SQL –SQL Server 2005 adds exception handling to T- SQL T-SQL can be faster for data access T-SQL can be faster for data access –Direct access to SQL Server's internal buffers –Rich, data-centric library of functions –No conversion of types

CLR Integration Feature Comparison with T-SQL T-SQLCLR User Defined Functions XX Stored Procedures XX TriggersXX User Defined Types X AggregatesX

CLR Integration Best uses of SQLCLR Computational functions are always faster Computational functions are always faster Streaming table valued functions Streaming table valued functions User defined aggregates User defined aggregates –Orders magnitude faster than server or client cursor solutions Scalar functions Scalar functions –Function body is compiled to native code Use managed code for: Use managed code for: –Procedures that feature complex logic –Access to the.NET Framework class library –CPU intensive functions

SQLCLR Guidance Mid Tier vs. Data Tier SQLCLR support does not mean move all business logic to server SQLCLR support does not mean move all business logic to server Candidates for moving to server Candidates for moving to server –Centralized data validation –Process large amount of data while needing a small portion of it for application use

Resources & Questions Microsoft Resources: Microsoft Resources: –msdn.microsoft.com/sqlserver/ msdn.microsoft.com/sqlserver/ – Contact me: Contact me: – (will be available soon) Download Presentation : Download Presentation : – Thanks.