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

beginning asp.net 2.0 databases, 2nd edition

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 (18.56 MB, 656 trang )

www.it-ebooks.info
Beginning ASP.NET 2.0
Databases
From Novice to Professional
■■■
Damien Foggon
Foggon_577-7FRONT.fm Page i Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
Beginning ASP.NET 2.0 Databases: From Novice to Professional
Copyright © 2006 by Damien Foggon
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-13 (pbk): 978-1-59059-577-0
ISBN-10 (pbk): 1-59059-577-7
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
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: Ronald Landers, Sahil Malik
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan
Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Matt Wade
Project Manager: Richard Dal Porto
Copy Edit Manager: Nicole LeClerc
Copy Editors: Marilyn Smith, Kim Wimpsett
Assistant Production Director: Kari Brooks-Copony
Production Editor: Kelly Gunther
Compositor: Susan Glinert
Proofreader: Linda Seifert
Indexer: Julie Grady


Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: 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 , or
visit .
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 , or visit .
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 in the Source Code section.
Foggon_577-7FRONT.fm Page ii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
iii
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 Data Sources and the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Introducing Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
■CHAPTER 3 Displaying Data on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
■CHAPTER 4 Database Access in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
■CHAPTER 5 DataReader and DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
■CHAPTER 6 Inline and List Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
■CHAPTER 7 Table Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
■CHAPTER 8 Writing to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
■CHAPTER 9 The GridView Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

■CHAPTER 10 Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
■CHAPTER 11 Modifying the Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
■CHAPTER 12 Useful Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
■CHAPTER 13 Application Design and Implementation . . . . . . . . . . . . . . . . . . . . . . 547
■APPENDIX A Installation Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
■APPENDIX B SQL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
■APPENDIX C SQL Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
■APPENDIX D Sample Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Foggon_577-7FRONT.fm Page iii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
Foggon_577-7FRONT.fm Page iv Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
v
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 Data Sources and the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Are Data-Driven Web Sites a Good Idea? . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How Do Web Sites Use Data Sources? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Database Uses in a Web Environment . . . . . . . . . . . . . . . . . . . . . . . . . 4
How Does the Web Site Get the Data?. . . . . . . . . . . . . . . . . . . . . . . . . 6
Looking for Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Database Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Flat Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Introducing ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Data Access Technology: A Brief History . . . . . . . . . . . . . . . . . . . . . . 11
Data Providers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Developing Your First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Try It Out: Creating a Simple Data-Driven Page . . . . . . . . . . . . . . . . 19
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
■CHAPTER 2 Introducing Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . 27
The Databases and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Try It Out: Creating a Table in SQL Server 2005. . . . . . . . . . . . . . . . 31
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Try It Out: Creating a Table in MySQL 5.0 . . . . . . . . . . . . . . . . . . . . . 36
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Column Properties and Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Foggon_577-7FRONT.fm Page v Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
vi
■CONTENTS
Queries and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
SQL Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Types of Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Try It Out: Adding Indexes in SQL Server 2005. . . . . . . . . . . . . . . . . 48
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Try It Out: Adding Indexes in MySQL 5.0 . . . . . . . . . . . . . . . . . . . . . . 51
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Relationships Between Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Types of Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Foreign Keys and Foreign Key Constraints . . . . . . . . . . . . . . . . . . . . 56
Try It Out: Adding Relationships in SQL Server 2005 . . . . . . . . . . . . 58
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Try It Out: Adding Relationships in MySQL 5.0 . . . . . . . . . . . . . . . . . 61
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Database Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Try It Out: Creating a Database Diagram in SQL Server 2005. . . . . 64
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Try It Out: Using a Database Diagram to Create
a New Relationship in SQL Server 2005
. . . . . . . . . . . . . . . . . . . . 66
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Users, Roles, and Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Try It Out: Creating User Accounts in SQL Server 2005. . . . . . . . . . 69
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Try It Out: Creating User Accounts in MySQL 5.0 . . . . . . . . . . . . . . . 71
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Data for the Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Try It Out: Adding Data to a SQL Server 2005 Database . . . . . . . . . 72
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Try It Out: Adding Data to a MySQL 5.0 Database . . . . . . . . . . . . . . 73
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Database Views and Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Foggon_577-7FRONT.fm Page vi Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
■CONTENTS
vii
■CHAPTER 3 Displaying Data on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Introducing the Data Source Web Controls . . . . . . . . . . . . . . . . . . . . . . . . 78
Introducing SELECT Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Try It Out: Querying a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Try It Out: Ordering the Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Try It Out: Querying Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . 94
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Try It Out: Filtering the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Try It Out: Filtering Results and Showing All Results . . . . . . . . . . . 106
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Introducing the WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Using Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Using Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Using the IN and BETWEEN Operators . . . . . . . . . . . . . . . . . . . . . . . 110
Connecting to MySQL 5.0 and Microsoft Access . . . . . . . . . . . . . . . . . . 111
Connection Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Parameters and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
■CHAPTER 4 Database Access in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
The Connection and Command Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . 116
Connection Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Try It Out: Connecting to SQL Server 2005
Using SqlConnection
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Try It Out: Connecting to MySQL 5.0 Using OdbcConnection . . . . 121
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Try It Out: Connecting to Microsoft Access

Using OleDbConnection
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Connection Object Methods and Properties. . . . . . . . . . . . . . . . . . . 126
Connection Pooling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Foggon_577-7FRONT.fm Page vii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
viii
■CONTENTS
Command Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Creating a Command Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Returning the Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Filtering the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Try It Out: Modifying the Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Try It Out: Using Parameters in Queries. . . . . . . . . . . . . . . . . . . . . . 138
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Parameters and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Command Object Methods and Properties . . . . . . . . . . . . . . . . . . . 142
Scalar Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Try It Out: Using the ExecuteScalar() Method . . . . . . . . . . . . . . . . . 144
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Try It Out: Catching and Handling Errors . . . . . . . . . . . . . . . . . . . . . 147
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
■CHAPTER 5 DataReader and DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The DataReader Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
How to Read Through a DataReader . . . . . . . . . . . . . . . . . . . . . . . . 157

Try It Out: Iterating Through a DataReader . . . . . . . . . . . . . . . . . . . 159
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
DataReader Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . 165
The DataSet Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
How to Fill a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Try It Out: Iterating Through a DataSet. . . . . . . . . . . . . . . . . . . . . . . 173
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Creating a DataSet from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
SqlDataSource—DataSet or DataReader? . . . . . . . . . . . . . . . . . . . . . . . . 185
DataSet vs. DataReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Good Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Foggon_577-7FRONT.fm Page viii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
■CONTENTS
ix
■CHAPTER 6 Inline and List Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Data-Binding Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Data Binding in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Data Binding and the SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . 191
Data-Aware Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Associating Data to the Web Control . . . . . . . . . . . . . . . . . . . . . . . . 192
Inline Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Try It Out: Inline Binding to a DataReader . . . . . . . . . . . . . . . . . . . . 195
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Try It Out: Inline Binding to a DataSet . . . . . . . . . . . . . . . . . . . . . . . 201
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
The Inline Binding Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Try It Out: Showing Data from a DataReader . . . . . . . . . . . . . . . . . 206
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

Try It Out: Showing Data from a DataSet. . . . . . . . . . . . . . . . . . . . . 208
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
List Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Try It Out: Using Single-Value Lookup Lists
with a DataReader
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
List Binding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Try It Out: Using Lookup Lists and Events
with a DataReader
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Try It Out: Using Lookup Lists and Events with a DataSet . . . . . . . 225
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Try It Out: Using Lookup Lists and Events
with a SqlDataSource
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Connecting to Other Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Multiple Selection Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Try It Out: Using Multiple-Value Lookup Lists with a DataReader. . . . 236
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Try It Out: Using Multiple-Value Lookup Lists
with a SqlDataSource
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Foggon_577-7FRONT.fm Page ix Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
x

■CONTENTS
■CHAPTER 7 Table Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
The Table-Binding Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Repeater, DataList, and GridView Differences. . . . . . . . . . . . . . . . . 249
Item Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
The Repeater Web Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The Repeater Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Try It Out: Using the Repeater to Display the Manufacturers. . . . . 252
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
The Repeater Control Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Try It Out: Using the Repeater to Display the Players. . . . . . . . . . . 260
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Inline Binding vs. Event-Based Binding . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Inline Binding Alternative. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Mixing Binding Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
The DataList Web Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Try It Out: Using the DataList to Display the Players . . . . . . . . . . . 270
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
The GridView Web Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Try It Out: Showing Data in a GridView . . . . . . . . . . . . . . . . . . . . . . 274
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
GridView Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Try It Out: Customizing the GridView . . . . . . . . . . . . . . . . . . . . . . . . 281
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Paging and Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Try It Out: Sorting the GridView. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Try It Out: Paging the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
GridView Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

Try It Out: Responding to Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
DataSet vs. DataReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Foggon_577-7FRONT.fm Page x Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
■CONTENTS
xi
■CHAPTER 8 Writing to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Making Changes to a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Inserting Data into the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
The INSERT Query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Working to the Database’s Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Try It Out: Inserting a New Player with INSERT . . . . . . . . . . . . . . . . 300
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Queries in MySQL 5.0 and Microsoft Access. . . . . . . . . . . . . . . . . . 311
Try It Out: Setting the Player’s Supported Formats. . . . . . . . . . . . . 312
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Validating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Try It Out: Validating Entered Data . . . . . . . . . . . . . . . . . . . . . . . . . . 321
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
The DELETE Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Sympathy for the User: GridView ButtonField Columns . . . . . . . . . 329
Try It Out: Deleting Players with DELETE . . . . . . . . . . . . . . . . . . . . . 331
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Updating Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
The UPDATE Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Try It Out: Updating a Player with UPDATE . . . . . . . . . . . . . . . . . . . 336
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

Using a DataSet to Make the Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
The Role of the DataAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
The Role of the DataRow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Try It Out: Inserting Data Using a DataSet . . . . . . . . . . . . . . . . . . . . 348
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Try It Out: Updating Data Using a DataSet. . . . . . . . . . . . . . . . . . . . 356
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Try It Out: Deleting Data Using a DataSet . . . . . . . . . . . . . . . . . . . . 361
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Manually Creating the Commands . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Foggon_577-7FRONT.fm Page xi Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xii
■CONTENTS
■CHAPTER 9 The GridView Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
The Updatable SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
The GridView and DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
The Field Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
The EmptyDataTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
The Eval() and Bind() Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Editing Data in a GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Try It Out: Updating Data in a GridView . . . . . . . . . . . . . . . . . . . . . . 371
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Try It Out: Changing Controls Used for Editing . . . . . . . . . . . . . . . . 382
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Try It Out: Deleting Data in a GridView. . . . . . . . . . . . . . . . . . . . . . . 385
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Using the DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Try It Out: Showing Data in a DetailsView . . . . . . . . . . . . . . . . . . . . 388

How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Try It Out: Editing Data in a DetailsView. . . . . . . . . . . . . . . . . . . . . . 392
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Try It Out: Deleting Data in a DetailsView . . . . . . . . . . . . . . . . . . . . 395
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Try It Out: Adding Data in a DetailsView . . . . . . . . . . . . . . . . . . . . . 397
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Tidying Up the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Try It Out: Manually Adding an Add New Player Link . . . . . . . . . . . 400
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Using the FormView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Using Templates with the FormView . . . . . . . . . . . . . . . . . . . . . . . . 404
Switching Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Validating User Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Try It Out: Adding Validation to the DetailsView . . . . . . . . . . . . . . . 408
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Foggon_577-7FRONT.fm Page xii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
■CONTENTS
xiii
■CHAPTER 10 Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Why Should You Use Stored Procedures? . . . . . . . . . . . . . . . . . . . . . . . . 415
Configuring MySQL 5.0 to Use Stored Procedures . . . . . . . . . . . . . . . . . 416
Creating Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Try It Out: Creating a Stored Procedure in SQL Server 2005. . . . . 418
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Try It Out: Creating a Stored Procedure in MySQL 5.0 . . . . . . . . . . 422
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Granting Permissions for Stored Procedures. . . . . . . . . . . . . . . . . . 424

Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Try It Out: Using a Command Object to Call
a Stored Procedure
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Choosing an Execute Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Try It Out: Calling a Stored Procedure in a SqlDataSource . . . . . . 430
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Altering and Deleting Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . 430
Try It Out: Modifying a Stored Procedure
in SQL Server 2005
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Try It Out: Modifying a Stored Procedure in MySQL 5.0. . . . . . . . . 433
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Creating Stored Procedures with Input Parameters . . . . . . . . . . . . . . . . 434
Try It Out: Creating a Stored Procedure with
Input Parameters in SQL Server 2005
. . . . . . . . . . . . . . . . . . . . . 436
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Try It Out: Creating a Stored Procedure
with Input Parameters in MySQL 5.0
. . . . . . . . . . . . . . . . . . . . . . 439
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Passing Parameters to Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . 441
Try It Out: Using Input Parameters with a Command Object . . . . . 441
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Try It Out: Using Input Parameters with a SqlDataSource . . . . . . . 444
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Using Parameters with MySQL 5.0. . . . . . . . . . . . . . . . . . . . . . . . . . 445

Foggon_577-7FRONT.fm Page xiii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xiv
■CONTENTS
Returning Data Using Output Parameters . . . . . . . . . . . . . . . . . . . . . . . . 445
Try It Out: Creating a Stored Procedure
with Output Parameters in SQL Server 2005
. . . . . . . . . . . . . . . 446
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Try It Out: Creating a Stored Procedure
with Input Parameters in MySQL 5.0
. . . . . . . . . . . . . . . . . . . . . . 448
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Returning Parameters from Stored Procedures . . . . . . . . . . . . . . . . . . . 450
Try It Out: Using Output Parameters
with a SqlCommand Object
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Try It Out: Using Output Parameters with a SqlDataSource. . . . . . 453
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Using Stored Procedures with Other Queries . . . . . . . . . . . . . . . . . . . . . 455
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
■CHAPTER 11 Modifying the Database Structure . . . . . . . . . . . . . . . . . . . . . . 459
Using Command-Line Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Using SQLCMD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Try It Out: Querying a SQL Server Database
via the Command Line
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Using mysql.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

Try It Out: Querying a MySQL Database
via the Command Line
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Creating Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Try It Out: Creating a Database with CREATE DATABASE . . . . . . . 466
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Try It Out: Creating Tables in SQL Server
with CREATE TABLE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Try It Out: Creating Tables in MySQL with CREATE TABLE . . . . . . 473
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Adding, Modifying, and Removing Columns . . . . . . . . . . . . . . . . . . . . . . 475
Try It Out: Changing a Table Definition with ALTER TABLE . . . . . . 477
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Foggon_577-7FRONT.fm Page xiv Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
■CONTENTS
xv
Creating and Deleting Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Creating Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Try It Out: Creating Indexes with CREATE INDEX . . . . . . . . . . . . . . 481
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Deleting Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Try It Out: Deleting an Index with DROP INDEX. . . . . . . . . . . . . . . . 483
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Creating and Deleting Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Creating Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

Try It Out: Creating Relationships with ALTER TABLE . . . . . . . . . . 486
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Deleting Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Try It Out: Deleting Relationships with ALTER TABLE. . . . . . . . . . . 490
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Try It Out: Deleting Database Tables with DROP TABLE . . . . . . . . 491
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Deleting Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Try It Out: Deleting a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
■CHAPTER 12 Useful Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Try It Out: Handling Concurrency Using Command Objects. . . . . . 499
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Try It Out: Handling Concurrency Using the SqlDataSource . . . . . 507
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Specifying the Life Span of a Cached Object. . . . . . . . . . . . . . . . . . 516
Try It Out: Caching the Manufacturers . . . . . . . . . . . . . . . . . . . . . . . 517
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Caching a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Removing Objects from the Cache . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Try It Out: Removing Cached Objects Manually . . . . . . . . . . . . . . . 521
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Foggon_577-7FRONT.fm Page xv Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xvi
■CONTENTS

Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Defining Database Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Using a Transaction Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Try It Out: Using a Transaction Object . . . . . . . . . . . . . . . . . . . . . . . 528
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Implementing Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . 534
Try It Out: Using a Local Transaction . . . . . . . . . . . . . . . . . . . . . . . . 536
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Try It Out: Using a Distributed Transaction . . . . . . . . . . . . . . . . . . . 540
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Multiple Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Try It Out: Executing Two SELECT Queries in a Query Batch. . . . . 543
How It Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
■CHAPTER 13 Application Design and Implementation . . . . . . . . . . . . . . . . 547
The Software Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Client Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
The Right Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Database Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Application Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Code Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Testing and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Measuring Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567

Bug Fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Feature Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Issue Tracking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Foggon_577-7FRONT.fm Page xvi Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
■CONTENTS
xvii
■APPENDIX A Installation Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
.NET Framework 2.0 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Microsoft Jet Engine Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Visual Web Developer 2005 Express Edition Installation . . . . . . . . . . . . 572
SQL Server 2005 Express Edition Installation . . . . . . . . . . . . . . . . . . . . . 573
SQL Server 2005 Management Studio Express Installation . . . . . . . . . . 576
MySQL 5.0 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
MySQL Query Browser 1.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
MySQL Connector/ODBC 3.51 Installation . . . . . . . . . . . . . . . . . . . . . . . . 578
MySQL Connector/NET 1.0 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . 578
■APPENDIX B SQL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Text Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
longtext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
mediumtext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
nchar/national char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
nvarchar/national varchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
ntext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
tinytext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
varchar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582

Autonumbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Integer Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Variable-Size Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . 585
Date and Time Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
datetime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
smalldatetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
timestamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Foggon_577-7FRONT.fm Page xvii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xviii
■CONTENTS
Binary Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
SQL Server Binary Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
MySQL Binary Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Microsoft Access Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Miscellaneous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
enum('value1','value2', ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
set('value1','value2', ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
smallmoney. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
uniqueidentifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
■APPENDIX C SQL Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594

■APPENDIX D Sample Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
The Manufacturer Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
The Player Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
The Format Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
The WhatPlaysWhatFormat Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Using the Database Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Using a Script to Build the SQL Server 2005 Database . . . . . . . . . 605
Using a Script to Build the MySQL 5.0 Database . . . . . . . . . . . . . . 606
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Foggon_577-7FRONT.fm Page xviii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xix
About the Author
■DAMIEN FOGGON is a freelance programmer and technical author based
in Newcastle, England. He is technical director of Thing-E Ltd., a company
specializing in the development of dynamic Web solutions for the
education sector, and founder of Littlepond Ltd. He started out working
for BT in the UK before moving on to progressively smaller companies,
finally founding his own company. Now he can work on all the cool new
technologies, rather than the massive monolithic developments that
still exist out there.
Although this is his first solo outing, Damien has coauthored books for Microsoft Press
and Apress and acted as technical reviewer for both Wrox and Apress. He can be reached at
.
Foggon_577-7FRONT.fm Page xix Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
Foggon_577-7FRONT.fm Page xx Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xxi
About the Technical

Reviewers
■RONALD LANDERS is the President and Senior Technical Consultant
of IT Professionals, Inc. (), a staffing,
recruiting, development, and IT project services company located in
Calabasas, California. He has more than 20 years of experience in the
IT field and specializes in database design and implementation, appli-
cation design and architecture, and Web-based technologies such as
Web services, electronic commerce, and Web portals. In addition to
IT Professionals, Inc., Ronald has been teaching IT courses at UCLA
Extension for the past 13 years. Currently, his courses include beginning
and advanced classes in ASP.NET, SQL Server, Web services, and
object-oriented programming.
■SAHIL MALIK is a consultant, trainer, and mentor in various Microsoft
technologies. He is also the author of the best-selling Pro ADO.NET 2.0.
He has worked for many large clients across the globe, including a
number of Fortune 100 companies and U.S. government organizations.
Currently, he leads the study of emerging technologies at a prominent
government office, where he is in charge of reviewing, assessing, and
recommending various technologies to support the organization. Sahil
frequently speaks on a variety of .NET-related topics at local user group
meetings and industry events. For his community involvement and
contribution, he has been awarded the Microsoft MVP award. He can
be reached at .
Foggon_577-7FRONT.fm Page xxi Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
Foggon_577-7FRONT.fm Page xxii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
xxiii
Acknowledgments
The second was definitely easier than the first, but the third was the killer. Thanks to Beckie,

Richard, Marilyn, Kelly, and Kim for putting up with me and getting something out there that
makes sense.
Foggon_577-7FRONT.fm Page xxiii Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info
Foggon_577-7FRONT.fm Page xxiv Sunday, February 26, 2006 7:27 AM
www.it-ebooks.info

×