Pro ADO net 2 0

585 416 0
Pro ADO net 2 0

Đ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

5122chFM.qxd 8/23/05 3:44 PM Page i Pro ADO.NET 2.0 Sahil Malik 5122chFM.qxd 8/23/05 3:44 PM Page ii Pro ADO.NET 2.0 Copyright © 2005 by Sahil Malik All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher ISBN: 1-59059-512-2 Printed and bound in the United States of America Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Lead Editor: Jonathan Hassell Technical Reviewers: Frans Bouma and Erick Sgarbi Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Associate Publisher: Grace Wong Project Manager: Emily K Wolman Copy Edit Manager: Nicole LeClerc Copy Editor: Linda Marousek Assistant Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositor: Kinetic Publishing Services, LLC Proofreader: April Eddy Indexer: Carol Burbo Artist: Kinetic Publishing Services, LLC Interior Designer: Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com The information in this book is distributed on an “as is” basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work The source code for this book is available to readers at http://www.apress.com in the Source Code section 5122chFM.qxd 8/23/05 3:44 PM Page iii I would like to dedicate this book to my parents Mom, for being extremely strict with me when I was a kid and always very loving in all my years Pop, for saving me from Mom ;-), and being the neverending source of inspiration and strength I love you both very much 5122chFM.qxd 8/23/05 3:44 PM Page iv 5122chFM.qxd 8/23/05 3:44 PM Page v Contents at a Glance About the Author xv About the Technical Reviewers xvii Acknowledgments xix Introduction xxi ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER 10 11 12 13 14 An Introduction to ADO.NET The ADO.NET Object Model 15 ADO.NET Hello World! 31 Connecting to a Data Source 53 Retrieving Data in a Connected Fashion 77 DataSets 109 Fetching Data: The DataAdapter 177 Sorting, Searching, and Filtering 213 Updating Data 247 Updating Data: Advanced Scenarios 321 Transactions 355 XML and ADO.NET 413 The CLR in SQL Server 461 ADO.NET Best Practices 513 ■INDEX 529 v 5122chFM.qxd 8/23/05 3:44 PM Page vi 5122chFM.qxd 8/23/05 3:44 PM Page vii Contents About the Author xv About the Technical Reviewers xvii Acknowledgments xix Introduction xxi ■CHAPTER An Introduction to ADO.NET What Is ADO.NET? What Is Wrong with ADO? Meeting the Players: Important Objects in ADO.NET The Connected Objects The Disconnected Objects NET Data Providers Using the ProviderBase Model Third-Party NET Data Providers 12 The System.Data.Design Namespace 14 Summary 14 ■CHAPTER The ADO.NET Object Model 15 This Is a Reference Chapter 15 10,000-Ft View of ADO.NET 16 Establishing a Connection: DbConnection 17 Executing a Command: DbCommand and DbTransaction 19 Creating Parameterized Commands: DbParameter 20 Holding Disconnected Data: DataSet 22 Fetching Data: Data Reader and Data Adapter 24 The Connected Way to Fetch Data: DbDataReader 24 The Bridge Between Connected and Disconnected: DbDataAdapter 25 Exceptions in ADO.NET 26 Summary 30 vii 5122chFM.qxd viii 8/23/05 3:44 PM Page viii ■CONTENTS ■CHAPTER ADO.NET Hello World! 31 Setting Up the Hello World Data Source 31 Creating a Data-Driven Application: The Drag-and-Drop Approach 32 Drag and Drop in ASP.NET 2.0 33 Drag and Drop in a Windows Forms Application 39 Hybrid Approach: Write Some Code, Do Some Drag and Drop 45 Data-Driven Application: The “Write Code Yourself” Approach 48 Summary 51 ■CHAPTER Connecting to a Data Source 53 The Ability to Connect 53 Creating Connection Objects 54 Generating Provider-Specific Connection Strings 57 The Easy Way to Any Connection String 60 Securing Connection Strings 62 The Common Behavior: IDbConnection 66 The Common Logic: DbConnection 68 High-Demand Applications 68 Connection Pooling 69 So How Does It All Work? 71 Deciding on the Right Pool Size 72 Corrupt Connection Pools 73 Closing Connections: Good Application Design 74 Summary 75 ■CHAPTER Retrieving Data in a Connected Fashion 77 Communicating with the Data Source 77 Retrieving a Scalar Value 78 Which Database to Execute Against 79 What to Execute 81 Executing a Command to Retrieve Results 82 Retrieving a Result Set 83 Querying a Result Set for Storage 88 Querying Large Result Sets Asynchronously 91 Querying the Database for Multiple Result Sets 96 Object-Oriented vs Relational Representation 99 Storing Objects in the Database 100 Querying for UDT Data Using SQL 105 5122chFM.qxd 8/23/05 3:44 PM Page ix ■CONTENTS Retrieving UDT Data in a Connected Fashion 105 Pragmatic Use of UDTs 106 Summary 107 ■CHAPTER DataSets 109 The Case for the Disconnected Model 109 The DataSet Object Model 111 DataTable 112 DataColumn 113 DataRow 114 Constraints 115 Setting a Primary Key: PrimaryKey Property 116 Dynamically Constructing a DataTable 116 DataTable Events 120 Practical Usage of DataTable Events 121 Relational Data 125 The Relations Collection 125 Putting It All Together 128 DataSets As Data Transfer Objects 134 Strongly Typed DataSets: An Introduction 140 Overview of XSD 141 DataSet Schemas 151 Building Strongly Typed DataSets 159 Typed DataSet Performance 168 Annotating Typed DataSets 168 Summary 174 ■CHAPTER Fetching Data: The DataAdapter 177 What Is a DataAdapter? 177 Structure of a DataAdapter 178 Putting DataAdapters to Use 179 Setting Up the Data Source 179 Querying One Table: Point and Click 180 Querying One Table: Writing Code 185 Filling DataSets: More Than One Table 190 Querying Database Schema 196 ix 5122chIDX.qxd 548 8/23/05 4:41 PM Page 548 ■INDEX GetChildRows() method, invocation of through the GetBookReviewsRows() method, 166 GetConcatenatedNames stored procedure creating, 496–497 modifying the connection string in, 508 GetName method, implemented by the IDataRecord interface, 84 GetNames stored procedure, creating, 497–499 GetNames.cs/GetNames.vb stored procedure, creating and naming, 496 GetOrdinal method, implemented by the IDataRecord interface, 84 GetProviderSpecificValue method, for returning a string rather than an XmlReader, 446 GetRandomNumber UDF creating, 466 output of SQLCLR handwritten, 467 testing, 472 GetString method, using to retrieve textual representation of the UDT, 106 GetUpdateCommand method, using, 264 GetXml using to show DataSet contents as XML, 131 GiveMeFakeData() code for in C#, 135–136 code for in VB.NET, 136–137 greater-than (>) character, entity encoding representation for, 426 GridView control adding to the aspx page, formatting, and setting properties on, 36 defined on the Default.aspx page, 38–39 element, in XSD element group, 149–150 GUIDs, using as primary keys, 294 ■H HandWrittenUDF class library project, creating, 465–467 HasErrors property, for DataSet and DataTable for identifying errors at a global level, 333–334 Hello World application See also ADO.NET Hello World! compiling and running in ASP.NET, 36–37 setting up the data source, 31–32 hide directive, example using, 428 hierarchical data deleting, 347 inserting into Exercise 10.1 application, 338–346 order for saving, 348 a sample database, 334 updating, 347 working with, 334–352 high-demand applications, writing ADO.NET code for, 68–69 history of changes, importance of for working with disconnected model, 110 hybrid approach for creating code for ADO.NET Hello World application, 45–48 results of compiling and running the application, 46 steps involved in producing code, 46–48 ■I IDataReader interface, implementation of IDataRecord and IDisposable interfaces by, 83 IDataRecord interface a few important methods implemented by, 83–84 reasons to use SqlDataReader object instead of, 85–86 IDbConnection interface common behavior enforced by, 66–67 methods it requires the NET data provider writer to implement, 66–67 properties it requires you to implement, 67 IdbTransaction, implemented by the DbTransaction class, 19 IDisposable interface, importance of implementation of by connection object, 66 IDisposable.Dispose method, used for cleanup jobs for any object, 74–75 idoc parameter, to OPENXML, 438 Ignore setting for MissingMappingAction property of the data adapter, 211 for MissingSchemaAction property of the data adapter, 211 implicit casting, a basic DataSet back from a strongly typed DataSet, 224 inbuilt scalar types, defined, 99 InferXmlSchema method, on DataSet object, 240 InitAdapter code for in C#, 258–260 code for in VB.NET, 260–261 injection attacks, parameterized queries resistant to, 20 INSERT command, using to manipulate database data, 78 InsertCommand, setting up and saving in Exercise 9.4, 288–290 Inserting, hierarchical data, 338–346 InsertName stored procedure, creating, 500–501 5122chIDX.qxd 8/23/05 4:41 PM Page 549 ■INDEX insertPetBelongingCommand, setting up with insertPetCommand, 340–343 insertPetCommand, setting up with insertPetBelongingCommand, 340–343 Int32 indexer, using in a loop instead of the string indexer, 85 interleaved command execution, with MARS, 381 interleaving rules, in a MARS-enabled world, 382–383 interop, used to access COM objects, IsDbNull method, implemented by the IDataRecord interface, 84 IsNull, Null methods, function of in SQL queries, 104 Isolated transactions, defined, 357 isolation levels changing, 373–378 defined, 370 examining the effect of, 370–378 reading current value of, 372 setting up two transactions with ReadCommitted and ReadUncommitted, 373–374 understanding related terms used when discussing, 370–371 working with two commands on the same table in two transactions on different, 374–376 IsolationLevel, specification of by the BeginTransaction method, 66 IsolationLevel enumeration, values accessible via, 371–372 ItemArray property, modifying a DataRow by loading an object array into, 280 ■K key constraints creating, 154 vs unique constraints, 154–155 KeyInfo command behavior, for ExecuteReader method, 86 key/keyref syntax, for defining a parent-child relationship, 171 elements, within an XML schema, 155–159 ■L last-in wins, in optimistic concurrency scheme, 325–326 lblSumPrice label, that holds results of aggregate calculations, 224–225 length constraining facet, function of, 146 less-than (

Ngày đăng: 10/04/2017, 10:50

Mục lục

  • Pro ADO.NET 2.0

    • Table of Content

    • Chapter 1 An Introduction to ADO.NET

    • Chapter 2 The ADO.NET Object Model.

    • Chapter 3 ADO.NET Hello World!

    • Chapter 4 Connecting to a Data Source

    • Chapter 5 Retrieving Data in a Connected Fashion

    • Chapter 6 DataSets

    • Chapter 7 Fetching Data: The DataAdapter

    • Chapter 8 Sorting, Searching, and Filtering

    • Chapter 9 Updating Data

    • Chapter 10 Updating Data: Advanced Scenarios

    • Chapter 11 Transactions

    • Chapter 12 XML and ADO.NET

    • Chapter 13 The CLR in SQL Server

    • Chapter 14 ADO.NET Best Practices

    • Index

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

Tài liệu liên quan