Contents at a Glance
About the Author
. . . xixAbout the Technical Reviewers
. . . xxiAcknowledgments
. . . xxiiiIntroduction
. . . 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
. . . 607v
Contents
About the Author
. . . xixAbout the Technical Reviewers
. . . xxiAcknowledgments
. . . xxiiiIntroduction
. . . xxv■ CHAPTER 1 Data Sources and the Web
. . . 1Are Data-Driven Web Sites a Good Idea?
. . . 2How Do Web Sites Use Data Sources?
. . . 4Database Uses in a Web Environment
. . . 4How Does the Web Site Get the Data?
. . . 6Looking for Information
. . . 8Database Servers
. . . 8Flat Files
. . . 9Web Services
. . . 10Objects
. . . 10Services
. . . 10Introducing ADO.NET
. . . 11Data Access Technology: A Brief History
. . . 11Data Providers
. . . 13Data Source Controls
. . . 18Developing Your First Example
. . . 18Try It Out: Creating a Simple Data-Driven Page
. . . 19How It Works
. . . 25Summary
. . . 26■ CHAPTER 2 Introducing Relational Databases
. . . 27The Databases and Tools
. . . 28Tables, Rows, and Columns
. . . 28Try It Out: Creating a Table in SQL Server 2005
. . . 31How It Works
. . . 35Try It Out: Creating a Table in MySQL 5.0
. . . 36How It Works
. . . 39Column Properties and Constraints
. . . 40vi ■C O N T E N T S
Queries and Stored Procedures
. . . 42SQL Queries
. . . 42Stored Procedures
. . . 46Indexes
. . . 46Types of Index
. . . 48Try It Out: Adding Indexes in SQL Server 2005
. . . 48How It Works
. . . 50Try It Out: Adding Indexes in MySQL 5.0
. . . 51How It Works
. . . 52Relationships Between Tables
. . . 52Types of Relationship
. . . 53Foreign Keys and Foreign Key Constraints
. . . 56Try It Out: Adding Relationships in SQL Server 2005
. . . 58How It Works
. . . 61Try It Out: Adding Relationships in MySQL 5.0
. . . 61How It Works
. . . 63Database Diagrams
. . . 64Try It Out: Creating a Database Diagram in SQL Server 2005
. . . 64How It Works
. . . 65Try It Out: Using a Database Diagram to Create a New Relationship in SQL Server 2005
. . . 66How It Works
. . . 67Users, Roles, and Permissions
. . . 68Try It Out: Creating User Accounts in SQL Server 2005
. . . 69How It Works
. . . 70Try It Out: Creating User Accounts in MySQL 5.0
. . . 71How It Works
. . . 71Data for the Sample Database
. . . 72Try It Out: Adding Data to a SQL Server 2005 Database
. . . 72How It Works
. . . 72Try It Out: Adding Data to a MySQL 5.0 Database
. . . 73How It Works
. . . 73Database Views and Triggers
. . . 74Summary
. . . 74■C O N T E N T S vii
■ CHAPTER 3 Displaying Data on a Page
. . . 77Introducing the Data Source Web Controls
. . . 78Introducing SELECT Queries
. . . 79Try It Out: Querying a Single Table
. . . 79How It Works
. . . 85Try It Out: Ordering the Results
. . . 90How It Works
. . . 92Try It Out: Querying Multiple Tables
. . . 94How It Works
. . . 96Try It Out: Filtering the Results
. . . 99How It Works
. . . 103Try It Out: Filtering Results and Showing All Results
. . . 106How It Works
. . . 108Introducing the WHERE Clause
. . . 108Using Comparison Operators
. . . 109Using Logical Operators
. . . 109Using the IN and BETWEEN Operators
. . . 110Connecting to MySQL 5.0 and Microsoft Access
. . . 111Connection Strings
. . . 111Parameters and Queries
. . . 112Summary
. . . 113■ CHAPTER 4 Database Access in Code
. . . 115The Connection and Command Life Cycle
. . . 116Connection Objects
. . . 117Try It Out: Connecting to SQL Server 2005 Using SqlConnection
. . . 117How It Works
. . . 119Try It Out: Connecting to MySQL 5.0 Using OdbcConnection
. . . . 121How It Works
. . . 123Try It Out: Connecting to Microsoft Access Using OleDbConnection
. . . 124How It Works
. . . 126Connection Object Methods and Properties
. . . 126Connection Pooling
. . . 127viii ■C O N T E N T S
Command Objects
. . . 128Creating a Command Object
. . . 129Returning the Results
. . . 130Filtering the Results
. . . 130Try It Out: Modifying the Query
. . . 130How It Works
. . . 134Try It Out: Using Parameters in Queries
. . . 138How It Works
. . . 138Parameters and Queries
. . . 140Command Object Methods and Properties
. . . 142Scalar Commands
. . . 143Scalar Functions
. . . 144Try It Out: Using the ExecuteScalar() Method
. . . 144How It Works
. . . 146Error Handling
. . . 147Try It Out: Catching and Handling Errors
. . . 147How It Works
. . . 150Summary
. . . 152■ CHAPTER 5 DataReader and DataSet
. . . 155The DataReader Object
. . . 156How to Read Through a DataReader
. . . 157Try It Out: Iterating Through a DataReader
. . . 159How It Works
. . . 163DataReader Properties and Methods
. . . 165The DataSet Object
. . . 168How to Fill a DataSet
. . . 169Try It Out: Iterating Through a DataSet
. . . 173How It Works
. . . 175Creating a DataSet from Scratch
. . . 176SqlDataSource—DataSet or DataReader?
. . . 185DataSet vs. DataReader
. . . 185Good Practices
. . . 187Summary
. . . 187■C O N T E N T S ix
■ CHAPTER 6 Inline and List Binding
. . . 189Data-Binding Techniques
. . . 189Data Binding in Code
. . . 190Data Binding and the SqlDataSource
. . . 191Data-Aware Web Controls
. . . 191Associating Data to the Web Control
. . . 192Inline Binding
. . . 194Try It Out: Inline Binding to a DataReader
. . . 195How It Works
. . . 199Try It Out: Inline Binding to a DataSet
. . . 201How It Works
. . . 204The Inline Binding Alternative
. . . 205Try It Out: Showing Data from a DataReader
. . . 206How It Works
. . . 207Try It Out: Showing Data from a DataSet
. . . 208How It Works
. . . 210List Binding
. . . 211Try It Out: Using Single-Value Lookup Lists with a DataReader
. . . 213How It Works
. . . 217List Binding Events
. . . 219Try It Out: Using Lookup Lists and Events with a DataReader
. . . 220How It Works
. . . 223Try It Out: Using Lookup Lists and Events with a DataSet
. . . 225How It Works
. . . 228Try It Out: Using Lookup Lists and Events with a SqlDataSource
. . . 229How It Works
. . . 231Connecting to Other Data Sources
. . . 235Multiple Selection Lists
. . . 236Try It Out: Using Multiple-Value Lookup Lists with a DataReader
. . . . 236How It Works
. . . 240Try It Out: Using Multiple-Value Lookup Lists with a SqlDataSource
. . . 242How It Works
. . . 243Summary
. . . 244x ■C O N T E N T S
■ CHAPTER 7 Table Binding
. . . 247The Table-Binding Web Controls
. . . 247Repeater, DataList, and GridView Differences
. . . 249Item Templates
. . . 250The Repeater Web Control
. . . 251The Repeater Templates
. . . 252Try It Out: Using the Repeater to Display the Manufacturers
. . . 252How It Works
. . . 257The Repeater Control Events
. . . 260Try It Out: Using the Repeater to Display the Players
. . . 260How It Works
. . . 265Inline Binding vs. Event-Based Binding
. . . 269Inline Binding Alternative
. . . 269Mixing Binding Types
. . . 270The DataList Web Control
. . . 270Try It Out: Using the DataList to Display the Players
. . . 270How It Works
. . . 273The GridView Web Control
. . . 274Try It Out: Showing Data in a GridView
. . . 274How It Works
. . . 275GridView Customization
. . . 276Try It Out: Customizing the GridView
. . . 281How It Works
. . . 283Paging and Sorting
. . . 286Try It Out: Sorting the GridView
. . . 286How It Works
. . . 288Try It Out: Paging the GridView
. . . 288How It Works
. . . 290GridView Events
. . . 290Try It Out: Responding to Events
. . . 290How It Works
. . . 293DataSet vs. DataReader
. . . 294Summary
. . . 295■C O N T E N T S xi
■ CHAPTER 8 Writing to the Database
. . . 297Making Changes to a Database
. . . 297Inserting Data into the Database
. . . 298The INSERT Query
. . . 298Working to the Database’s Rules
. . . 300Try It Out: Inserting a New Player with INSERT
. . . 300How It Works
. . . 306Queries in MySQL 5.0 and Microsoft Access
. . . 311Try It Out: Setting the Player’s Supported Formats
. . . 312How It Works
. . . 317Validating Data
. . . 319Try It Out: Validating Entered Data
. . . 321How It Works
. . . 325Deleting Data from the Database
. . . 328The DELETE Query
. . . 329Sympathy for the User: GridView ButtonField Columns
. . . 329Try It Out: Deleting Players with DELETE
. . . 331How It Works
. . . 334Updating Data in the Database
. . . 336The UPDATE Query
. . . 336Try It Out: Updating a Player with UPDATE
. . . 336How It Works
. . . 342Using a DataSet to Make the Changes
. . . 346The Role of the DataAdapter
. . . 347The Role of the DataRow
. . . 348Try It Out: Inserting Data Using a DataSet
. . . 348How It Works
. . . 353Try It Out: Updating Data Using a DataSet
. . . 356How It Works
. . . 359Try It Out: Deleting Data Using a DataSet
. . . 361How It Works
. . . 363Manually Creating the Commands
. . . 364Summary
. . . 365xii ■C O N T E N T S
■ CHAPTER 9 The GridView Family
. . . 367The Updatable SqlDataSource
. . . 368The GridView and DetailsView
. . . 369The Field Controls
. . . 369The EmptyDataTemplate
. . . 369The Eval() and Bind() Methods
. . . 370Editing Data in a GridView
. . . 371Try It Out: Updating Data in a GridView
. . . 371How It Works
. . . 375Try It Out: Changing Controls Used for Editing
. . . 382How It Works
. . . 383Try It Out: Deleting Data in a GridView
. . . 385How It Works
. . . 386Using the DetailsView
. . . 387Try It Out: Showing Data in a DetailsView
. . . 388How It Works
. . . 390Try It Out: Editing Data in a DetailsView
. . . 392How It Works
. . . 394Try It Out: Deleting Data in a DetailsView
. . . 395How It Works
. . . 397Try It Out: Adding Data in a DetailsView
. . . 397How It Works
. . . 398Tidying Up the User Interface
. . . 399Try It Out: Manually Adding an Add New Player Link
. . . 400How It Works
. . . 402Using the FormView
. . . 403Using Templates with the FormView
. . . 404Switching Modes
. . . 406Validating User Responses
. . . 406Try It Out: Adding Validation to the DetailsView
. . . 408How It Works
. . . 411Summary
. . . 412■C O N T E N T S xiii
■ CHAPTER 10 Stored Procedures
. . . 415Why Should You Use Stored Procedures?
. . . 415Configuring MySQL 5.0 to Use Stored Procedures
. . . 416Creating Stored Procedures
. . . 418Try It Out: Creating a Stored Procedure in SQL Server 2005
. . . 418How It Works
. . . 421Try It Out: Creating a Stored Procedure in MySQL 5.0
. . . 422How It Works
. . . 424Granting Permissions for Stored Procedures
. . . 424Calling Stored Procedures
. . . 425Try It Out: Using a Command Object to Call a Stored Procedure
. . . 427How It Works
. . . 429Choosing an Execute Method
. . . 429Try It Out: Calling a Stored Procedure in a SqlDataSource
. . . 430How It Works
. . . 430Altering and Deleting Stored Procedures
. . . 430Try It Out: Modifying a Stored Procedure in SQL Server 2005
. . . 431How It Works
. . . 432Try It Out: Modifying a Stored Procedure in MySQL 5.0
. . . 433How It Works
. . . 434Creating Stored Procedures with Input Parameters
. . . 434Try It Out: Creating a Stored Procedure with Input Parameters in SQL Server 2005
. . . 436How It Works
. . . 438Try It Out: Creating a Stored Procedure with Input Parameters in MySQL 5.0
. . . 439How It Works
. . . 440Passing Parameters to Stored Procedures
. . . 441Try It Out: Using Input Parameters with a Command Object
. . . 441How It Works
. . . 443Try It Out: Using Input Parameters with a SqlDataSource
. . . 444How It Works
. . . 444Using Parameters with MySQL 5.0
. . . 445xiv ■C O N T E N T S
Returning Data Using Output Parameters
. . . 445Try It Out: Creating a Stored Procedure with Output Parameters in SQL Server 2005
. . . 446How It Works
. . . 448Try It Out: Creating a Stored Procedure with Input Parameters in MySQL 5.0
. . . 448How It Works
. . . 449Returning Parameters from Stored Procedures
. . . 450Try It Out: Using Output Parameters with a SqlCommand Object
. . . 451How It Works
. . . 452Try It Out: Using Output Parameters with a SqlDataSource
. . . 453How It Works
. . . 454Using Stored Procedures with Other Queries
. . . 455Summary
. . . 457■ CHAPTER 11 Modifying the Database Structure
. . . 459Using Command-Line Tools
. . . 460Using SQLCMD
. . . 460Try It Out: Querying a SQL Server Database via the Command Line
. . . 461How It Works
. . . 463Using mysql.exe
. . . 463Try It Out: Querying a MySQL Database via the Command Line
. . . 464How It Works
. . . 465Creating Databases
. . . 466Try It Out: Creating a Database with CREATE DATABASE
. . . 466How It Works
. . . 468Creating Tables
. . . 469Try It Out: Creating Tables in SQL Server with CREATE TABLE
. . . 469How It Works
. . . 471Try It Out: Creating Tables in MySQL with CREATE TABLE
. . . 473How It Works
. . . 475Adding, Modifying, and Removing Columns
. . . 475Try It Out: Changing a Table Definition with ALTER TABLE
. . . 477■C O N T E N T S xv
Creating and Deleting Indexes
. . . 480Creating Indexes
. . . 480Try It Out: Creating Indexes with CREATE INDEX
. . . 481How It Works
. . . 482Deleting Indexes
. . . 483Try It Out: Deleting an Index with DROP INDEX
. . . 483How It Works
. . . 484Creating and Deleting Relationships
. . . 485Creating Relationships
. . . 485Try It Out: Creating Relationships with ALTER TABLE
. . . 486How It Works
. . . 488Deleting Relationships
. . . 489Try It Out: Deleting Relationships with ALTER TABLE
. . . 490How It Works
. . . 490Deleting Tables
. . . 490Try It Out: Deleting Database Tables with DROP TABLE
. . . 491How It Works
. . . 493Deleting Databases
. . . 493Try It Out: Deleting a Database
. . . 494How It Works
. . . 496Summary
. . . 496■ CHAPTER 12 Useful Techniques
. . . 497Concurrency
. . . 498Try It Out: Handling Concurrency Using Command Objects
. . . 499How It Works
. . . 505Try It Out: Handling Concurrency Using the SqlDataSource
. . . 507How It Works
. . . 511Caching
. . . 514Specifying the Life Span of a Cached Object
. . . 516Try It Out: Caching the Manufacturers
. . . 517How It Works
. . . 519Caching a DataSet
. . . 520Removing Objects from the Cache
. . . 521Try It Out: Removing Cached Objects Manually
. . . 521How It Works
. . . 522xvi ■C O N T E N T S
Transactions
. . . 523Defining Database Transactions
. . . 526Using a Transaction Object
. . . 527Try It Out: Using a Transaction Object
. . . 528How It Works
. . . 532Implementing Distributed Transactions
. . . 534Try It Out: Using a Local Transaction
. . . 536How It Works
. . . 539Try It Out: Using a Distributed Transaction
. . . 540How It Works
. . . 542Multiple Result Sets
. . . 542Try It Out: Executing Two SELECT Queries in a Query Batch
. . . 543How It Works
. . . 544Summary
. . . 545■ CHAPTER 13 Application Design and Implementation
. . . 547The Software Life Cycle
. . . 548Analysis
. . . 548Client Requirements
. . . 548The Right Tools
. . . 552Design
. . . 553Database Design
. . . 554Application Design
. . . 559Implementation
. . . 563Prototypes
. . . 563Stored Procedures
. . . 564Code Issues
. . . 564Testing and Debugging
. . . 565Unit Testing
. . . 566Measuring Performance
. . . 566Maintenance
. . . 567Bug Fixes
. . . 567Feature Requests
. . . 568Issue Tracking
. . . 568Summary
. . . 568■C O N T E N T S xvii
■ APPENDIX A Installation Instructions
. . . 571.NET Framework 2.0 Installation
. . . 571Microsoft Jet Engine Installation
. . . 571Visual Web Developer 2005 Express Edition Installation
. . . 572SQL Server 2005 Express Edition Installation
. . . 573SQL Server 2005 Management Studio Express Installation
. . . 576MySQL 5.0 Installation
. . . 576MySQL Query Browser 1.1 Installation
. . . 577MySQL Connector/ODBC 3.51 Installation
. . . 578MySQL Connector/NET 1.0 Installation
. . . 578■ APPENDIX B SQL Data Types
. . . 579Text Types
. . . 579char
. . . 580longtext
. . . 580mediumtext
. . . 580nchar/national char
. . . 580nvarchar/national varchar
. . . 581ntext
. . . 581text
. . . 581tinytext
. . . 582varchar
. . . 582Numeric Types
. . . 582Autonumbers
. . . 582Integer Types
. . . 583decimal
. . . 585Variable-Size Floating-Point Numbers
. . . 585Date and Time Types
. . . 586date
. . . 586datetime
. . . 587smalldatetime
. . . 587time
. . . 587timestamp
. . . 587year
. . . 588xviii ■C O N T E N T S
Binary Types
. . . 588SQL Server Binary Types
. . . 588MySQL Binary Types
. . . 589Microsoft Access Binary Data
. . . 589Miscellaneous Types
. . . 589enum('value1','value2',...)
. . . 589money
. . . 590set('value1','value2',...)
. . . 590smallmoney
. . . 590uniqueidentifier
. . . 590■ APPENDIX C SQL Primer
. . . 591SELECT
. . . 591INSERT
. . . 593UPDATE
. . . 594DELETE
. . . 594■ APPENDIX D Sample Database Tables
. . . 597The Manufacturer Table
. . . 598The Player Table
. . . 600The Format Table
. . . 602The WhatPlaysWhatFormat Table
. . . 603Using the Database Scripts
. . . 605Using a Script to Build the SQL Server 2005 Database
. . . 605Using a Script to Build the MySQL 5.0 Database
. . . 606■ INDEX
. . . 607