Professional SQL Server™ 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types docx

432 3.2K 2
Professional SQL Server™ 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.free-ebooks-download.org Professional SQL Server ™ 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types Derek Comingore Douglas Hinson 01_054034 ffirs.qxp 11/2/06 11:52 AM Page i www.free-ebooks-download.org Professional SQL Server ™ 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN-13: 978-0-470-05403-1 ISBN-10: 0-470-05403-4 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1MA/RX/RR/QW/IN No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTA- TIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTYMAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDA- TIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002. Library of Congress Cataloging-in-Publication Data Comingore, Derek, 1978- Professional SQL server 2005 CLR programming : with stored procedures, functions, triggers, aggregates, and types / Derek Comingore and Douglas Hinson. p. cm. Includes indexes. ISBN-13: 978-0-470-05403-1 (paper/website) ISBN-10: 0-470-05403-4 (paper/website) 1. SQL (Computer program language) 2. Database management Computer programs. 3. Microsoft .NET Framework. I. Hinson, Douglas, 1968- II. Title. QA76.73.S67C655 2007 005.75’85 dc22 2006031028 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. SQL Server is a trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 01_054034 ffirs.qxp 11/2/06 11:52 AM Page ii To my son, Derek Steven Comingore, and the next generation of programmers. — Derek Comingore To Misty, Mariah, and Kyle, for your love, support, and patience. —Douglas Hinson 01_054034 ffirs.qxp 11/2/06 11:52 AM Page iii About the Authors Derek Comingore is an independent consultant, trainer, and speaker specializing in SQL Server and .NET technologies. He has recently started two ventures, an online Microsoft community for colleagues to learn from one another, located at www.RedmondSociety.com, and a second for SQL Server consulting at www.SQLServerDBAs.com. Derek holds the MCAD/MCDBA Microsoft certifications, the Carnegie Mellon Personal Software Process (PSP) credential, and an AAS/BS in Computer Information Systems from University of North Dakota. Derek’s blog is located at http://RedmondSociety.com/blogs/Derek. Douglas Hinson has worked as an independent software and database consultant in the logistics and financial industries, with an extensive SQL Server background. He has coauthored several Wrox books, including Professional SQL Server 2005 Integration Services. 01_054034 ffirs.qxp 11/2/06 11:52 AM Page iv Credits Executive Editor Bob Elliott Development Editor Brian MacDonald Technical Editor Dan Meyers Production Editor Felicia Robinson Copy Editor Foxxe Editorial Services Editorial Manager Mary Beth Wakefield Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Project Coordinators Patrick Redmond Ryan Steffen Graphics and Production Specialists Carrie A. Foster Peter Gaunt Denny Hager Alicia B. South Ronald Terry Quality Control Technician Laura Albert John Greenough Proofreading and Indexing Techbooks 01_054034 ffirs.qxp 11/2/06 11:52 AM Page v 01_054034 ffirs.qxp 11/2/06 11:52 AM Page vi Contents Acknowledgments xv Introduction xvii Chapter 1: Introducing SQL CLR 1 What is SQL CLR? 1 The Evolution of SQL CLR 2 Pre-SQL Server 2005 Extensibility Options 2 Why Does SQL CLR Exist? 2 Supported SQL CLR Objects 3 The .NET Architecture 4 CTS 4 CLS 5 CIL 5 VES 5 JIT 5 Managed Code and Managed Languages 6 Hosting the CLR 7 SQL CLR Architecture 7 Application Domains 8 The CLR Security Model 8 SQL CLR CAS Permission Sets 9 RBS/RBI 9 Key SQL CLR Decisions 9 Using SQL CLR or T-SQL 9 Using SQL CLR or Extended Stored Procedures 10 Using SQL CLR or OLE Automation Procedures 10 Using the Data Tier or Application Tier for Business Logic 11 SQL CLR Barriers of Entry 11 Security Considerations 11 The DBA Perspective on SQL CLR 12 Implementation Considerations 12 Performance Considerations 13 Maintenance Considerations 13 SQL Server 2005 SQL CLR support 14 02_054034 ftoc.qxp 11/2/06 11:53 AM Page vii viii Contents Visual Studio 2005 SQL CLR support 14 Required Namespaces for SQL CLR Objects 15 Summary 15 Chapter 2: Your First CLR Stored Procedure 17 Setting Up SQL Server Express and the AdventureWorks Database 18 The Mechanics of a Creating and Deploying a CLR Routine 18 Enabling CLR in SQL Server 2005 20 Creating Your First SQL CLR Routine without VS2005 22 Coding the Class 22 Compiling the .NET DLL 24 Creating the Assembly 25 Choosing between User-Defined Functions and Stored Procedures 26 Creating the T-SQL Stored Procedure 27 How SQL Server Manages Assemblies 28 Assembly Metadata 29 Extracting Assembly DLL and Source Code 31 Creating Your First CLR User-Defined Function 32 Using Visual Studio to Create a CLR Routine 33 Creating the Class 34 Deploying the Class 36 What the Auto-Deployment Process Does 37 Suggested Conventions 38 Assemblies 38 Classes 38 Structures 38 Class and Structure Functions or Methods 38 Removing SQL CLR Objects 39 Reviewing Assembly Dependencies 39 Dropping an Assembly 40 Summary 40 Chapter 3: SQL CLR Structure and Common Tasks 43 SQL CLR Code Structure 43 SQL CLR Common Tasks 46 Using SQL CLR Data Type Classes 46 Detecting SQL CLR Availability 49 Performing Data Access 50 02_054034 ftoc.qxp 11/2/06 11:53 AM Page viii ix Contents Returning Resultsets and Messages 53 Programming Transactions 58 Summary 67 Chapter 4: Creating SQL CLR Objects 69 Managed Stored Procedures 69 Managed Code 69 The SqlProcedure Attribute 70 Parameters 72 Return Values 74 Creating, Altering, and Dropping Managed Stored Procedures 75 Managed Functions 77 Scalar-Valued Functions 77 Creating, Altering, and Dropping, Managed Scalar-valued Functions 80 Table-Valued Functions 81 Managed Triggers 83 SqlTriggerContext Class 86 Accessing the Virtual Inserted and Deleted Temporary Tables 87 Enabling and Disabling Triggers 88 Managed DML Triggers 89 Managed DDL Triggers 92 Managed Aggregates 93 Managed Code 93 SqlUserDefinedAggregate Attribute 96 Creating, Altering, and Dropping UDAs 96 Managed Types 97 Managed Code 97 SqlUserDefinedType Attribute 101 Creating, Altering, and Dropping UDTs 101 Summary 101 Chapter 5: Comparing T-SQL with Managed Code 103 Syntactic Comparisons 104 Variable Declaration and Scope 104 Automatic Variables and System Functions 106 Flow of Control 107 Syntax Checking 111 Custom Attributes and Optimizers 111 02_054034 ftoc.qxp 11/2/06 11:53 AM Page ix [...]... SQL Server Connections and SQL CLR Stored Procedures Connection Conclusions Console Apps and SQL CLR Extended Procs WinForms and SQL CLR UDTs Creating the Point SQL CLR UDT Creating a Windows Form to Display a SQL CLR UDT Accepting WinForm Input to Save a SQL CLR UDT Web Forms and SQL CLR Aggregates Integration Services and SQL CLR TVFs Building the SQL CLR TVF Using the SQL CLR TVF in an SSIS Package... Package Reporting Services and SQL CLR TVFs Summary Chapter 9: Handling Errors in CLR Stored Procedures Exception-Handling Mechanisms Used in SQL CLR How How How How T -SQL @@Error Exception-Handling Works T -SQL Try Catch Error-handling Works NET Structured Error-Handling Works SQL CLR Error Handling Works SQL CLR Error-Handling Strategies SQL CLR Objects Throw Errors Exception Handling at Design Time... Exceptions SQL CLR Handling T -SQL Object Errors T -SQL Objects Handling SQL CLR Exceptions Summary Chapter 10: CLR Administration and Security NET for the DBA and System Admin Where the NET Framework Is Located on Your Server Only One Version of CLR Can Be Hosted in SQL Server How SQL Server Manages Memory Usage in SQL CLR How to Look inside a Compiled Assembly Security and SQL Server–Hosted CLR Security... to interact with them Chapter 9 demonstrates some error-handling techniques in SQL CLR and compares the differences between T -SQL and NET error handling Chapter 10 describes the security risks and details the process and procedures that administrators will need to know to safely manage SQL CLR deployments Chapter 11 details a case study of solving a business problem using T -SQL and SQL CLR to demonstrate... your T -SQL user-defined functions, procedures, and extended stored procedures and you’ll start getting the picture of SQL CLR Arrays, datasets, even object-oriented programming techniques that are available when you develop in NET are just not possible in current T -SQL development But this is only a start SQL CLR opens up the ability to create user-defined types, triggers, user-defined table values functions,. .. you’ll be creating a basic SQL CLR stored procedure From there you’ll come up for air to get a few NET concepts in place and then you’ll immediately put those new skills to work After you’ve seen what you can do with SQL CLR, we’ll step back and look at a comparison between SQL CLR and T -SQL to put some perspective on the hype Then we’ll dive into some more complex SQL CLR objects and explore the topic... Example Performance and Other Comparisons Methodology of Comparison A Final Word on Performance Maintainability Portability Summary Chapter 6: Replacing Extended Stored Procedures, Functions, and Other T -SQL Objects Historical Perspective on Extended Stored Procedures Why Replace XPs with SQL CLR? Faster and Easier to Code Plays Nice with SQL Server Not Restricted to the Master Database SQL CLR Replacements... hosts the CLR, thus the birth name of the technology SQL CLR, ” and its successor CLR Integration.” Pre -SQL Server 2005 Extensibility Options Before SQL Server 2005, there was a handful of options a database developer could implement to extend beyond the boundaries of T -SQL As we will discuss in this chapter, SQL CLR is almost always a better environment for these routines The pre -SQL Server 2005 extensible... Introducing SQL CLR So how does the CLR security model apply to SQL CLR? There are two methods of applying security in SQL CLR, CAS permission sets and Role-Based Security (RBS) also known as Role-Based Impersonation (RBI), both of which can be used to ensure that your SQL CLR objects run with the required permissions and nothing more SQL CLR CAS Permission Sets When you load your assemblies into SQL Server,... Why Does SQL CLR Exist? Dr E F “Ted” Codd is the “father” of relational databases and thus Structured Query Language (SQL) as well SQL is both an American National Standards Institute (ANSI) and International Organization for Standardization (ISO) standard SQL (and its derivatives, including T -SQL) are set-based languages designed to create, retrieve, update, and delete (CRUD) data that is stored in . Derek, 1978- Professional SQL server 2005 CLR programming : with stored procedures, functions, triggers, aggregates, and types / Derek Comingore and Douglas. i www.free-ebooks-download.org Professional SQL Server ™ 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types Published by Wiley

Ngày đăng: 05/03/2014, 20:20

Từ khóa liên quan

Mục lục

  • Professional SQL Server 2005 CLR Programming

    • Contents

    • Acknowledgments

    • Introduction

      • Who This Book Is For

      • How This Book Is Structured

      • What You Need to Use This Book

      • Conventions

      • Source Code

      • Errata

      • p2p.wrox.com

      • Introducing SQL CLR

        • What is SQL CLR?

        • The Evolution of SQL CLR

          • Pre-SQL Server 2005 Extensibility Options

          • Why Does SQL CLR Exist?

          • Supported SQL CLR Objects

          • The .NET Architecture

            • CTS

            • CLS

            • CIL

            • VES

            • JIT

            • Managed Code and Managed Languages

            • Hosting the CLR

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan