Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQL Server 2005 CLR Integration

866 views

Published on

  • Be the first to comment

  • Be the first to like this

SQL Server 2005 CLR Integration

  1. 1. SQL Server 2005 CLR Integration Matthew Roche
  2. 2. Session Overview <ul><li>Programmability Options in SQL Server </li></ul><ul><li>SQLCLR Architecture </li></ul><ul><li>Creating Managed Objects </li></ul><ul><li>Stored Procedures, Functions and Triggers </li></ul><ul><li>Data Access </li></ul><ul><li>User Defined Types </li></ul><ul><li>User Defined Aggregates </li></ul><ul><li>Best Practices </li></ul>
  3. 3. Programmability Options in SQL Server <ul><li>Transact-SQL </li></ul><ul><ul><li>Set-based </li></ul></ul><ul><ul><li>Cursors </li></ul></ul><ul><li>Extended Stored Procedures </li></ul><ul><li>OLE Automation (sp_oe* procedures) </li></ul><ul><li>SQLCLR </li></ul>
  4. 4. SQLCLR Architecture <ul><li>Design Goals </li></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>Reliability </li></ul></ul><ul><ul><li>Performance </li></ul></ul><ul><li>SQL Server as a CLR Host </li></ul><ul><ul><li>CorBindToRuntimeEx() </li></ul></ul><ul><ul><li>ICLRRuntimeHost </li></ul></ul><ul><ul><li>IHostControl </li></ul></ul>
  5. 5. Creating Managed Objects <ul><li>Cataloging assemblies </li></ul><ul><li>Using Transact-SQL </li></ul><ul><li>Using Visual Studio 2005 </li></ul><ul><li>Interface and data type restrictions </li></ul><ul><li>Reference restrictions and the HostProtectionAttribute </li></ul><ul><li>Assembly permission sets (SAFE, EXTERNAL_ACCESS, UNSAFE) </li></ul>
  6. 6. Creating Assemblies <ul><li>DEMO </li></ul>
  7. 7. Data Types <ul><li>Many .NET scalar types are valid as parameters and return values, but cannot handle NULLs </li></ul><ul><li>System.Data.SqlTypes are preferred </li></ul><ul><ul><li>Isomorphic with native SQL Server data types </li></ul></ul><ul><ul><li>Some differences (DateTime, Decimal) </li></ul></ul><ul><li>Use any .NET types internally, within restrictions of permission set </li></ul>
  8. 8. Stored Procedures <ul><li>Static public int or void method </li></ul><ul><li>Microsoft.SqlServer.Server.SqlProcedure attribute </li></ul>[SqlProcedure] public static void StoredProcedure() { // Put your code here }
  9. 9. Creating Stored Procedures <ul><li>DEMO </li></ul>
  10. 10. Scalar Functions <ul><li>Static public method </li></ul><ul><li>Scalar return type </li></ul><ul><li>Microsoft.SqlServer.Server.SqlFunction attribute </li></ul>[SqlFunction] public static SqlString ScalarFunction() { return new SqlString(&quot;Hello&quot;); }
  11. 11. Table-Valued Functions <ul><li>Static public method </li></ul><ul><li>IEnumerable return type </li></ul><ul><li>Microsoft.SqlServer.Server.SqlFunction attribute – FillRowMethodName parameter </li></ul>[SqlFunction (FillRowMethodName=&quot;FillRow&quot;)] public static IEnumerable TableValuedFunction() { // Put your code here return new string[] {&quot;Syracuse&quot;, &quot;Rochester&quot; ... }; } public static void FillRow(object o, out string name) { name = (string)o; }
  12. 12. Creating Functions <ul><li>DEMO </li></ul>
  13. 13. Triggers <ul><li>Public static void method </li></ul><ul><li>Microsoft.SqlServer.Server.SqlTrigger attribute </li></ul><ul><li>TriggerContext object available through SqlContext object </li></ul>[SqlTrigger (Name=&quot;Emp_UPD&quot;, Target=&quot;emp&quot;, Event=&quot;FOR UPDATE&quot;)] public static void Trigger() { if (SqlContext.TriggerContext.IsUpdatedColumn(0)) { //... } }
  14. 14. User Defined Types <ul><li>Public class or struct </li></ul><ul><li>Must implement INullable </li></ul><ul><li>Well-defined “interface” </li></ul>public override string ToString(){return &quot;&quot;;} 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;}
  15. 15. User Defined Aggregates <ul><li>Public class or struct; well-defined “interface” </li></ul><ul><li>System.Serializable attribute </li></ul><ul><li>Microsoft.SqlServer.Server. SqlUserDefinedAggregate attribute </li></ul><ul><li>Serialization options </li></ul>[S erializable] [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(&quot;&quot;);} }
  16. 16. Data Access <ul><li>Server-side programming model very similar to client-side programming model </li></ul><ul><ul><li>Good or bad? </li></ul></ul><ul><li>Using the managed data provider and the context connection </li></ul><ul><li>Sending data to the client using SqlPipe </li></ul><ul><li>Describing data using SqlMetaData </li></ul>
  17. 17. Using the Managed Provider <ul><li>DEMO </li></ul>
  18. 18. Best Practices <ul><li>Use Transact-SQL for all data access </li></ul><ul><ul><li>Call T-SQL stored procedures from managed code </li></ul></ul><ul><ul><li>Use set-based operations </li></ul></ul><ul><li>Use SQLCLR procedures for problems that cannot be solved using T-SQL </li></ul><ul><ul><li>Complex math </li></ul></ul><ul><ul><li>Complex string manipulation (RegEx) </li></ul></ul><ul><ul><li>External resource access </li></ul></ul><ul><ul><li>Replace XPs, not T-SQL </li></ul></ul><ul><li>Use SqlTypes for all visible parameters and return values </li></ul>
  19. 19. Performance Testing <ul><li>DEMO </li></ul>
  20. 20. Additional Resources <ul><li>CLR Integration Team Blog: </li></ul><ul><ul><li>http://blogs.msdn.com/sqlclr/default.aspx </li></ul></ul><ul><li>Introduction to SQL Server CLR Integration at MSDN: </li></ul><ul><ul><li>http://msdn2.microsoft.com/en-us/library/ms254498.aspx </li></ul></ul><ul><li>“ A First Look at SQL Server 2005 for Developers” by Bob Beauchemin, Niels Berglund and Dan Sullivan </li></ul><ul><li>“ Customizing the Microsoft .NET Framework Common Language Runtime” by Steven Pratschner </li></ul><ul><li>Niels Berglund’s blog: </li></ul><ul><ul><li>http:// staff.develop.com/nielsb / </li></ul></ul><ul><li>Questions? </li></ul>
  21. 21. Additional SQL Server 2005 Topic Ideas <ul><li>Transact-SQL Enhancements </li></ul><ul><li>Service Broker </li></ul><ul><li>XML Data Type and XQuery </li></ul><ul><li>Native XML Web Services </li></ul><ul><li>Other? </li></ul>
  22. 22. .NET 2.0 and SQL 2005 Beta MCP Pro Exam Promo Codes <ul><li>Exam 71-442 (“Design & Optimize Data Access by Using MS SQL Server 2005 ”): 442SQL </li></ul><ul><li>Exam 71-547 (“Design & Develop Web-Based Applications by Using MS .NET Framework 2.0”): PRO547 </li></ul><ul><li>Exam 71-548 (“Design & Develop Windows-Based Applications by Using MS .NET Framework 2.0”): BTA548 </li></ul><ul><li>Exam 71-549 (“Design & Develop Enterprise Applications by Using MS .NET Framework 2.0”): 549BTA </li></ul>

×