Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08.

Similar presentations


Presentation on theme: "Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08."— Presentation transcript:

1 Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08

2 Introductions The End of T-SQL How does it work? –Hosting Layer, SQL OS How do I use it? –Stored procedures, Functions, Triggers, Custom aggregations, User Defined Type Deployment Monitoring Questions Programming the CLR in SQL Server 2005

3 Mark Blomsma Professional developer since 1992 Microsoft Certified Professional Microsoft Most Valuable Professional (MVP) for three years running Current employment –Software Architect : Develop-One –Consultant : Omnext.NET –Sales Manager North America : MRA Group

4 The end of T-SQL? No! Managed code will not solve all your problems. –Select/Insert/Update/Delete are here to stay! –CLR is an alternative to the procedural portion of T-SQL

5 So why put C# in the database? Complex string manipulation Encryption XML manipulation No more need for external procedures Tooling support for managed languages Leverage.NET FCL & third party libraries For everything that you cannot do with T-SQL

6 Show me the magic Stability is everything for a database CLR 2.0 offers more control to host environment through “HostProtection” attribute –No thread creation –No listening on sockets –No finalizers –Allow/deny allocation of memory –Deny killing the host process –CLR is sandboxed through CAS Checked during assembly creation process CLR 2.0 AppDomain is hosted in SQL Server –Loading and unloading of assemblies is controlled via SQL Server

7 Limited FCL support Not available –Environment.Exit() –System.Console –System.Windows.Forms –System.Drawing –System.Web –…

8 SQL OS CLR uses SQL OS for: –Memory –Threads/fibers –Synchronization SQL Engine Windows SQL OS CLR HostingLayer

9 Hosting layer Hosting layer provides coordination of: –Assembly Loading –Memory management –Security Model –Reliability –Threads & Fibers –Deadlock detection –Execution context SQL Engine Windows SQL OS CLR HostingLayer

10 How do I use it? Stored Procedures Functions Triggers Custom Aggregations SqlContext –SqlExecutionContext –SqlTriggerContext

11 Stored procedures Public class Pubic method Method must be static Use [SqlProcedure] attribute Use in, out inout and return parameters

12 Stored procedures

13 Functions Public class Public method Method must be static Use [SqlFunction] attribute Must return a value

14 Functions

15 Triggers Public class Public method Method must be static Use [SqlTrigger] attribute –[SqlTrigger(Event=“FOR INSERT”)] Use SqlTriggerContext for getting at the data related to the trigger.

16 Triggers

17 Custom Aggregations Public struct Use [SqlUserDefinedAggregate] attribute Implement: –Init –Accumulate –Merge –Terminate

18 Custom Aggregations

19 User Defined Type Public struct Use [SqlUserDefinedType] attribute 8KB size limit. Do not use for business objects.

20 Deployment Use sp_configure ‘clr enabled’, ‘1’ to enable sqlclr –Off by default Use create assembly SqlClr uses 3 CAS permission buckets –Safe –External access –Unsafe

21 Deployment

22 Monitor assemblies Profiler trace events: –CLR:load assembly monitors assembly load requests (successes and failures) –SQL:BatchStarting, BatchCompleted –SP:Starting, Completed, StmtStarting, StmtCompleted monitor execution of Transact-SQL and CLR routines Performance counters: –SQL Server: Total CLR time –.NET CLR Memory –Processor DMVs and catalog views: –sys.assembly* shows basic information about the assemblies stored –sys.dm_os_memory_clerks –sys.dm_clr* –sys.dm_exec_query_stats –sys.dm_exec_requests –sys.dm_exec_cached_plans

23 Monitoring

24 Tips Database server CPU is valuable SQLCLR is about making DB programming easier Managed code has a performance advantage over T-SQL with respect to most procedural computation, but for data- access T-SQL generally fares better.

25 Questions?

26 References SQL Server 2005 distilled by Eric Brown, ISBN: 0321349792 http://msdn.microsoft.com/sql/learning/pro g/clr/default.aspx?pull=/library/en- us/dnsql90/html/sqlclrguidance.asphttp://msdn.microsoft.com/sql/learning/pro g/clr/default.aspx?pull=/library/en- us/dnsql90/html/sqlclrguidance.asp http://blog.develop-one.com This presentation on: –www.sdn.nlwww.sdn.nl –www.develop-one.comwww.develop-one.com

27 Addendum New SQL Server 2005 features Presentation by Dinesh Chandrasekhar www.microsoft.com/sql/prodinfo/overview/ whats-new-in-sqlserver2005.mspxwww.microsoft.com/sql/prodinfo/overview/ whats-new-in-sqlserver2005.mspx http://msdn2.microsoft.com/en- us/library/ms170363(SQL.90).aspxhttp://msdn2.microsoft.com/en- us/library/ms170363(SQL.90).aspx

28 Evaluatieformulier Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Session Code: DB.08


Download ppt "Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08."

Similar presentations


Ads by Google