Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Server 2005:.NET Framework Programming in the Database Tim Sneath Architectural Engineer, Microsoft

Similar presentations


Presentation on theme: "SQL Server 2005:.NET Framework Programming in the Database Tim Sneath Architectural Engineer, Microsoft"— Presentation transcript:

1 SQL Server 2005:.NET Framework Programming in the Database Tim Sneath Architectural Engineer, Microsoft tims@microsoft.com http://blogs.msdn.com/tims

2 .NET Framework Integration Benefits  Enhanced programming model Full access to the BCL from SQL Server  Safety and security Integration of SQL “native” and CLR security  User-defined types and aggregates  Common development environment Shared tools from client-tier to data-tier Authoring, debugging, deployment, and profiling  Performance and scalability For processor-centric tasks

3 T-SQL Versus C# For Logic Converting Binary To Hex if @bin is null return null declare @len int, @b tinyint, @lowbyte tinyint, @hibyte tinyint, @index int, @str nchar(2), @result nvarchar(4000) set @len = datalength(@bin) set @index = 1 set @result = '0x' while @index <= @len begin set @b = substring(@bin, @index, 1) set @index = @index + 1 set @lowbyte = @b & 0xF set @hibyte = @b & 0xF0 if @hibyte > 0 set @hibyte = @hibyte / 0xF set @result = @result + ((case when @hibyte < 10 then convert(varchar(1), @hibyte) when @hibyte = 10 then 'A' when @hibyte = 11 then 'B' when @hibyte = 12 then 'C' when @hibyte = 13 then 'D' when @hibyte = 14 then 'E' when @hibyte = 15 then 'F' else 'Z' end)+(case when @lowbyte < 10 then convert(varchar(1), @lowbyte) when @lowbyte = 10 then 'A' when @lowbyte = 11 then 'B' when @lowbyte = 12 then 'C' when @lowbyte = 13 then 'D' when @lowbyte = 14 then 'E' when @lowbyte = 15 then 'F' else 'Z' end))end if (value == null) return null; StringBuilder sb = new StringBuilder(); foreach (byte b in value) sb.Append(b.ToString("X2")); return sb.ToString();

4 Design Guidelines Choosing Between Managed Code and T-SQL T-SQL is best suited for data access Familiar, simple programming model Static compilation model Optimised for data access Managed code is ideal for procedural programming and computation IL compiled to x86/ia64 (native) code at runtime, easily outperforms interpreted T-SQL Computationally-intensive business logic encapsulated as functions

5 Runtime hosted in-proc by SQL Server 2005 SQL Queries: SELECT Payment (Total, Interest) FROM Credit WHERE CustomerID = '048349' The Developer Experience SQL Server VS.NET Project VB / C# / C++ Assembly: “CredLib.dll” SQL Data Definition: CREATE ASSEMBLY then: CREATE FUNCTION CREATE PROCEDURE CREATE TRIGGER CREATE TYPE

6 demo Stored Procedures

7 Some Questions Where does a catalogued assembly go? How do you view what assemblies are catalogued? How do you amend or delete an assembly? How do dependent assemblies get managed? How do you share assemblies across databases? When does the CLR get loaded in process? How do you debug a SQL Server assembly?

8 Development Environment New SQL Server Project template in VS 2005 for SQL Server 2005 managed code Server debug integration Full debugger visibility Set breakpoints anywhere Single step support Single step support Between languages Between deployment tiers Auto-deploymentAttributes

9 demo Scalar-Valued Functions

10 Interacting with the Engine

11 demo Triggers Table-Valued Functions

12 CLR Security: Permission Sets SAFE Execute & data access permission No access to resources outside SQL Server No unmanaged calls Must be verifiable EXTERNAL_ACCESS Adds ability to access external resources via managed APIs SQL Server impersonates the caller for external access Must be verifiable Requires new EXTERNAL ACCESS permission to create UNSAFE Can call unmanaged code Need not be unverifiable Only system administrator can create

13 CLR Security: Restrictions Protection Mechanisms Fusion Loader Hook [HostProtection] attribute Most of System.* available, including: mscorlib.dllsystem.data.dllsystem.dllsystem.xml.dllsystem.security.dllsystem.web.services.dllmicrosoft.visualbasic.dll Non-applicable functionality eliminated: System.Windows.FormsSystem.Drawing System.Web, …

14 User Defined Types / Aggregations Types Complex structure and associated behaviour Implemented as managed classes with a specific contract Deep engine support Query processing, Distributed Queries, Replication, BCP, etc. Aggregates Extensibility hook to aggregate values over a group during query processing Contract details Initialise a group Accumulate a value Merge groups Finish computation and return result

15 demo User-Defined Types (UDTs)

16 Tips for Developers and DBAs Use SqlPipe.Execute instead of cmd.ExecuteReader() Use SqlDataReader instead of T-SQL cursors Use SqlExecutionContext instead of SqlCommand Catalogue debugger symbols and source code Use db_option 'clr enabled' 1/0 to control CLR loading Performance views: sys.dm_exec_resultssys.dm_clr_loaded_assembliessys.dm_exec_query_stats Use DBCC STACKDUMP to generate a stack dump

17 Resources Articles (based on Beta 1) http://blogs.msdn.com/tims/articles/167243.aspxhttp://msdn.com/msdnmag/issues/04/02/yukonbasics/default.aspxhttp://msdn.com/library/en-us/dnsql90/html/sql_ovyukonnetprogfeatures.asp http://download.microsoft.com/download/3/8/1/38154d73-bc47-4e9f-a7f5- ca9beb118fde/SQL_Server_Yukon_New_Features_CLR.pdf Blogshttp://blogs.msdn.com/timshttp://www.sqljunkies.com/weblogDocumentation Books Online – Architectural Topics MSDN Library – Class Syntax Traininghttp://www.solidqualitylearning.com/Classes/VBNETAndCLRForDBAs.htmhttp://www.develop.com/uk/training/course.aspx?id=180

18 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.


Download ppt "SQL Server 2005:.NET Framework Programming in the Database Tim Sneath Architectural Engineer, Microsoft"

Similar presentations


Ads by Google