Tải bản đầy đủ (.pdf) (10 trang)

Microsoft SQL Server 2005 Developer’s Guide- P1 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (408 KB, 10 trang )

Microsoft
®
SQL Server

2005
Developer’s Guide
Michael Otey
Denielle Otey
McGraw-Hill/Osborne
New York Chicago San Francisco
Lisbon London Madrid Mexico City Milan
New Delhi San Juan Seoul Singapore Sydney Toronto
Copyright © 2006 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted
under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or
stored in a database or retrieval system, without the prior written permission of the publisher.
0-07-148348-9
The material in this eBook also appears in the print version of this title: 0-07-226099-8.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name,
we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where
such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training pro-
grams. For more information, please contact George Hoare, Special Sales, at or (212) 904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use
of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, dis-
seminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own non-
commercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to com-
ply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE


ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DIS-
CLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MER-
CHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the func-
tions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages result-
ing therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall
McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the
use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall
apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
DOI: 10.1036/0072260998
We hope you enjoy this
McGraw-Hill eBook! If
you’d like more information about this book,
its author, or related books and websites,
please click here.
Professional
Want to learn more?
To Mom and Dad, Ray and Dortha Marty,
For many years of dedication and encouragement,
and great bowling advice.
About the Authors
Michael Otey is Senior Technical Editor of SQL Server Magazine and co-author
of SQL Server 2000 Developer’s Guide, SQL Server 7 Developer’s Guide, and
ADO.NET: The Complete Reference. He is the president of TECA, Inc., a software
development and consulting firm.
Denielle Otey is vice president of TECA, Inc. She has extensive experience
developing commercial software products, and is the co-author of ADO.NET: The
Complete Reference.
Copyright © 2006 by The McGraw-Hill Companies. Click here for terms of use.

v
Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Chapter 1 The Development Environment . . . . . . . . . . . . . . . . . . . . . . . . 1
SQL Server Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The SQL Server Management Studio User Interface . . . . . . . . . . . . . . . . 3
SQL Server Management Studio User Interface Windows . . . . . . . . . . . . . . 4
SQL Server 2005 Administrative Tools . . . . . . . . . . . . . . . . . . . . . . . 14
BI Development Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Business Intelligence Development Studio User Interface . . . . . . . . . . . . 16
BI Development Studio User Interface Windows . . . . . . . . . . . . . . . . . . 16
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Chapter 2 Developing with T-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
T-SQL Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
SQL Server Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Creating Database Objects Using T-SQL DDL . . . . . . . . . . . . . . . . . . . . . . . . 34
Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Storage for Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
For more information about this title, click here
vi Microsoft SQL Server 2005 Developer’s Guide
Querying and Updating with T-SQL DML . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Select and Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Modifying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Chapter 3 Developing CLR Database Objects . . . . . . . . . . . . . . . . . . . . . . 77
Understanding CLR and SQL Server 2005 Database Engine . . . . . . . . . . . . . . . . 78
CLR Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Enabling CLR Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
CLR Database Object Components . . . . . . . . . . . . . . . . . . . . . . . . . 80
Creating CLR Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
CLR Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Debugging CLR Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
.NET Database Object Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Managing CLR Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 4 SQL Server Service Broker . . . . . . . . . . . . . . . . . . . . . . . . . . 117
SQL Server Service Broker Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Developing SQL Service Broker Applications . . . . . . . . . . . . . . . . . . . . . . . . 122
SQL Server Service Broker DDL and DML . . . . . . . . . . . . . . . . . . . . . 122
T-SQL DDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
T-SQL DML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

Enabling SQL Server Broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Using Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Sample SQL Server Service Broker Application . . . . . . . . . . . . . . . . . . . 125
Contents vii
SQL Server Service Broker Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Dialog Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
System Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Chapter 5 Developing with Notification Services . . . . . . . . . . . . . . . . . . . . 135
Notification Services Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Subscriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Developing Notification Services Applications . . . . . . . . . . . . . . . . . . . . . . . 139
Defining the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Compiling the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Building the Notification Subscription Management Application . . . . . . . . . . . 140
Adding Custom Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Notification Services Application Sample . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Creating the ICF File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Defining the ADF File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Building the Notification Services Application . . . . . . . . . . . . . . . . . . . 152
Updating Notification Services Applications . . . . . . . . . . . . . . . . . . . . . . . . 157
Building a .NET Subscription/Event Application . . . . . . . . . . . . . . . . . . . . . . 158
Listing Subscriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Adding Subscriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Deleting Subscriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Firing the Data Event Using .NET . . . . . . . . . . . . . . . . . . . . . . . . . 163
Firing the Data Event Using T-SQL . . . . . . . . . . . . . . . . . . . . . . . . . 166
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

Chapter 6 Developing Database Applications with ADO.NET . . . . . . . . . . . . . 169
The ADO.NET Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
ADO.NET Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
.NET Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Namespaces for the .NET Data Providers . . . . . . . . . . . . . . . . . . . . . . 173
Core Classes for the .NET Data Providers . . . . . . . . . . . . . . . . . . . . . . 175
Core Classes in the ADO.NET System.Data Namespace . . . . . . . . . . . . . . . . . . . 177
DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
DataTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
viii Microsoft SQL Server 2005 Developer’s Guide
DataColumn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
DataRow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
DataView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
DataViewManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
DataRelation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
ForeignKeyConstraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
UniqueConstraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
DataException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Using the .NET Framework Data Provider for SQL Server . . . . . . . . . . . . . . . . . . 182
Adding the System.Data.SqlClient Namespace . . . . . . . . . . . . . . . . . . . 182
Using the SqlConnection Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
The .NET Framework Data Provider for SQL Server
Connection String Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Opening a Trusted Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using Connection Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Using the SqlCommand Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Executing Dynamic SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . 191
Executing Parameterized SQL Statements . . . . . . . . . . . . . . . . . . . . . 193
Executing Stored Procedures with Return Values . . . . . . . . . . . . . . . . . . 196

Executing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Using the SqlDependency Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Using the SqlDataReader Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Retrieving a Fast Forward–Only Result Set . . . . . . . . . . . . . . . . . . . . . 205
Reading Schema-Only Information . . . . . . . . . . . . . . . . . . . . . . . . 208
Asynchronous Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Multiple Active Result Sets (MARS) . . . . . . . . . . . . . . . . . . . . . . . . . 210
Retrieving BLOB Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Using the SqlDataAdapter Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Populating the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Using the CommandBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . 216
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Chapter 7 Developing with XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
The XML Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Data Validation Using an XSD Schema . . . . . . . . . . . . . . . . . . . . . . . 223
XQuery Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Querying Element Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

×