SQL Server 2005 CLR Integration

22
SQL Server 2005 SQL Server 2005 CLR Integration CLR Integration Matthew Roche Matthew Roche

Transcript of SQL Server 2005 CLR Integration

Page 1: SQL Server 2005 CLR Integration

SQL Server 2005SQL Server 2005CLR IntegrationCLR Integration

Matthew RocheMatthew Roche

Page 2: SQL Server 2005 CLR Integration

Session OverviewSession Overview

Programmability Options in SQL ServerProgrammability Options in SQL Server SQLCLR ArchitectureSQLCLR Architecture Creating Managed ObjectsCreating Managed Objects Stored Procedures, Functions and Stored Procedures, Functions and

TriggersTriggers Data AccessData Access User Defined TypesUser Defined Types User Defined AggregatesUser Defined Aggregates Best PracticesBest Practices

Page 3: SQL Server 2005 CLR Integration

Programmability Options in SQL Programmability Options in SQL ServerServer

Transact-SQLTransact-SQL Set-basedSet-based CursorsCursors

Extended Stored ProceduresExtended Stored Procedures OLE Automation (sp_oe* procedures)OLE Automation (sp_oe* procedures) SQLCLRSQLCLR

Page 4: SQL Server 2005 CLR Integration

SQLCLR ArchitectureSQLCLR Architecture

Design GoalsDesign Goals SecuritySecurity ReliabilityReliability PerformancePerformance

SQL Server as a CLR HostSQL Server as a CLR Host CorBindToRuntimeEx()CorBindToRuntimeEx() ICLRRuntimeHostICLRRuntimeHost IHostControlIHostControl

Page 5: SQL Server 2005 CLR Integration

Creating Managed ObjectsCreating Managed Objects

Cataloging assembliesCataloging assemblies Using Transact-SQLUsing Transact-SQL Using Visual Studio 2005Using Visual Studio 2005 Interface and data type restrictionsInterface and data type restrictions Reference restrictions and the Reference restrictions and the

HostProtectionAttributeHostProtectionAttribute Assembly permission sets (SAFE, Assembly permission sets (SAFE,

EXTERNAL_ACCESS, UNSAFE)EXTERNAL_ACCESS, UNSAFE)

Page 6: SQL Server 2005 CLR Integration

Creating AssembliesCreating Assemblies

DEMODEMO

Page 7: SQL Server 2005 CLR Integration

Data TypesData Types

Many .NET scalar types are valid as Many .NET scalar types are valid as parameters and return values, but cannot parameters and return values, but cannot handle NULLshandle NULLs

System.Data.SqlTypesSystem.Data.SqlTypes are preferred are preferred Isomorphic with native SQL Server data typesIsomorphic with native SQL Server data types Some differences (DateTime, Decimal)Some differences (DateTime, Decimal)

Use any .NET types internally, within Use any .NET types internally, within restrictions of permission setrestrictions of permission set

Page 8: SQL Server 2005 CLR Integration

Stored ProceduresStored Procedures

Static public int or void methodStatic public int or void method Microsoft.SqlServer.Server.SqlProcedure Microsoft.SqlServer.Server.SqlProcedure

attributeattribute[SqlProcedure]public static void StoredProcedure(){ // Put your code here}

Page 9: SQL Server 2005 CLR Integration

Creating Stored ProceduresCreating Stored Procedures

DEMODEMO

Page 10: SQL Server 2005 CLR Integration

Scalar FunctionsScalar Functions

Static public methodStatic public method Scalar return typeScalar return type Microsoft.SqlServer.Server.SqlFunction Microsoft.SqlServer.Server.SqlFunction

attributeattribute

[SqlFunction]public static SqlString ScalarFunction(){ return new SqlString("Hello");}

Page 11: SQL Server 2005 CLR Integration

Table-Valued FunctionsTable-Valued Functions

Static public methodStatic public method IEnumerable return typeIEnumerable return type Microsoft.SqlServer.Server.SqlFunction Microsoft.SqlServer.Server.SqlFunction

attribute – FillRowMethodName parameterattribute – FillRowMethodName parameter[SqlFunction (FillRowMethodName="FillRow")]public static IEnumerable TableValuedFunction(){ // Put your code here return new string[] {"Syracuse", "Rochester"...};}public static void FillRow(object o, out string name){ name = (string)o;}

Page 12: SQL Server 2005 CLR Integration

Creating FunctionsCreating Functions

DEMODEMO

Page 13: SQL Server 2005 CLR Integration

TriggersTriggers

Public static void methodPublic static void method Microsoft.SqlServer.Server.SqlTrigger Microsoft.SqlServer.Server.SqlTrigger

attributeattribute TriggerContext object available through TriggerContext object available through

SqlContext objectSqlContext object[SqlTrigger (Name="Emp_UPD", Target="emp", Event="FOR UPDATE")]public static void Trigger(){ if (SqlContext.TriggerContext.IsUpdatedColumn(0)) { //... }}

Page 14: SQL Server 2005 CLR Integration

User Defined TypesUser Defined Types

Public class or structPublic class or struct Must implement INullableMust implement INullable Well-defined “interface”Well-defined “interface”public override string ToString(){return "";}public bool IsNull{ get{ return m_Null; }}public static UserDefinedType Null{ get{ return new UserDefinedType(); }}public static UserDefinedType Parse(SqlString s){ UserDefinedType u = new UserDefinedType(); return u;}

Page 15: SQL Server 2005 CLR Integration

User Defined AggregatesUser Defined Aggregates

Public class or struct; well-defined “interface”Public class or struct; well-defined “interface” System.Serializable attributeSystem.Serializable attribute Microsoft.SqlServer.Server. Microsoft.SqlServer.Server.

SqlUserDefinedAggregate attributeSqlUserDefinedAggregate attribute Serialization optionsSerialization options[Serializable][SqlUserDefinedAggregate(Format.Native)]public struct Aggregate{ public void Init(){} public void Accumulate(SqlString Value){} public void Merge(Aggregate Group){} public SqlString Terminate(){ return new SqlString("");}}

Page 16: SQL Server 2005 CLR Integration

Data AccessData Access

Server-side programming model very Server-side programming model very similar to client-side programming modelsimilar to client-side programming model Good or bad?Good or bad?

Using the managed data provider and the Using the managed data provider and the context connectioncontext connection

Sending data to the client using SqlPipeSending data to the client using SqlPipe Describing data using SqlMetaDataDescribing data using SqlMetaData

Page 17: SQL Server 2005 CLR Integration

Using the Managed ProviderUsing the Managed Provider

DEMODEMO

Page 18: SQL Server 2005 CLR Integration

Best PracticesBest Practices

Use Transact-SQL for all data accessUse Transact-SQL for all data access Call T-SQL stored procedures from managed codeCall T-SQL stored procedures from managed code Use set-based operationsUse set-based operations

Use SQLCLR procedures for problems that Use SQLCLR procedures for problems that cannot be solved using T-SQLcannot be solved using T-SQL Complex mathComplex math Complex string manipulation (RegEx)Complex string manipulation (RegEx) External resource accessExternal resource access Replace XPs, Replace XPs, notnot T-SQL T-SQL

Use SqlTypes for all visible parameters and Use SqlTypes for all visible parameters and return valuesreturn values

Page 19: SQL Server 2005 CLR Integration

Performance TestingPerformance Testing

DEMODEMO

Page 20: SQL Server 2005 CLR Integration

Additional ResourcesAdditional Resources

CLR Integration Team Blog:CLR Integration Team Blog: http://blogs.msdn.com/sqlclr/default.aspxhttp://blogs.msdn.com/sqlclr/default.aspx

Introduction to SQL Server CLR Integration at MSDN:Introduction to SQL Server CLR Integration at MSDN: http://msdn2.microsoft.com/en-us/library/ms254498.aspxhttp://msdn2.microsoft.com/en-us/library/ms254498.aspx

““A First Look at SQL Server 2005 for Developers” by A First Look at SQL Server 2005 for Developers” by Bob Beauchemin, Niels Berglund and Dan SullivanBob Beauchemin, Niels Berglund and Dan Sullivan

““Customizing the Microsoft .NET Framework Common Customizing the Microsoft .NET Framework Common Language Runtime” by Steven PratschnerLanguage Runtime” by Steven Pratschner

Niels Berglund’s blog: Niels Berglund’s blog: http://http://staff.develop.com/nielsbstaff.develop.com/nielsb//

Questions?Questions?

Page 21: SQL Server 2005 CLR Integration

Additional SQL Server 2005 Topic Additional SQL Server 2005 Topic IdeasIdeas

Transact-SQL EnhancementsTransact-SQL Enhancements Service BrokerService Broker XML Data Type and XQueryXML Data Type and XQuery Native XML Web ServicesNative XML Web Services Other?Other?

Page 22: SQL Server 2005 CLR Integration

.NET 2.0 and SQL 2005 Beta MCP .NET 2.0 and SQL 2005 Beta MCP Pro Exam Promo CodesPro Exam Promo Codes

Exam 71-442 (“Design & Optimize Data Access Exam 71-442 (“Design & Optimize Data Access by Using MS SQL Server 2005 ”): 442SQLby Using MS SQL Server 2005 ”): 442SQL

Exam 71-547 (“Design & Develop Web-Based Exam 71-547 (“Design & Develop Web-Based Applications by Using MS .NET Framework Applications by Using MS .NET Framework 2.0”): PRO5472.0”): PRO547

Exam 71-548 (“Design & Develop Windows-Exam 71-548 (“Design & Develop Windows-Based Applications by Using MS .NET Based Applications by Using MS .NET Framework 2.0”): BTA548Framework 2.0”): BTA548

Exam 71-549 (“Design & Develop Enterprise Exam 71-549 (“Design & Develop Enterprise Applications by Using MS .NET Framework Applications by Using MS .NET Framework 2.0”): 549BTA2.0”): 549BTA