You are previewing Microsoft® ADO.NET 2.0 Step by Step, 2nd Edition.
O'Reilly logo
Microsoft® ADO.NET 2.0 Step by Step, 2nd Edition

Book Description

The popular, learn-by-doing tutorial-rewritten for the latest version of Microsoft ADO.NET. Teach yourself core capabilities and essential techniques, and build working components and applications for Microsoft Windows®.

Table of Contents

  1. Microsoft® ADO.NET 2.0 Step by Step
  2. A Note Regarding Supplemental Files
  3. Introduction
    1. Who Is This Book For?
    2. The Organization of This Book
    3. Finding Your Best Starting Point in This Book
    4. Conventions and Features in This Book
    5. System Requirements
    6. Code Samples
      1. Installing the Code Samples
      2. Attaching the AdoStepByStep Database
      3. Using the Code Samples
      4. Uninstalling the Code Samples—Read!
    7. Prerelease Software
    8. Online Companion Content
    9. Support for This Book
      1. Questions and Comments
  4. I. Getting Started with ADO.NET
    1. 1. Getting Started with ADO.NET
      1. On the Fundamental Interconnectedness of All Things
        1. A margin note like this points you to the discussion of a property or method that hasn't yet been introduced.
          1. A margin note like this points you to the discussion of a property or method that hasn't yet been introduced.
            1. A margin note like this points you to the discussion of a property or method that hasn't yet been introduced.
      2. The ADO.NET Object Model
        1. Data Providers
          1. Connections are discussed in detail in Chapter 2.
            1. Connections are discussed in detail in Chapter 2.
            2. We'll discuss Command objects in detail in Chapter 3.
            3. DataReaders are the subject of Chapter 4.
            4. We'll discuss adapters in Chapters Chapters 5 and 8.
        2. DataSets
          1. DataSets and their component collections are the subject of Chapters 7 and 8
            1. DataSets and their component collections are the subject of Chapters 7 and 8.
          2. The DataTableCollection Object
            1. We'll discuss DataColumns in Chapter 7.
            2. We'll discuss DataRows in Chapter 8.
            3. We'll discuss Constraints in Chapter 7.
          3. The DataRelationCollection Object
            1. We'll discuss DataRelations in Chapter 7.
      3. Creating a Simple Data-Bound Form
        1. Configuring a Data Source
          1. Using the Data Sources Window
            1. We'll discuss the BindingNavigator in Chapter 13.
      4. Summary
  5. II. Data Provider Objects
    1. 2. Using Connections
      1. Understanding Connections
      2. Creating Connections
        1. Creating Connections at Design Time
        2. Creating Connections at Run Time
      3. Configuring Connections
        1. The ConnectionString Property
        2. Storing the ConnectionString in the Application Configuration File
        3. Using Other Connection Properties
        4. Binding Connection Properties to Form Controls
      4. Connection Methods
        1. We'll examine transaction processing in Chapter 6.
          1. We'll examine transaction processing in Chapter 6.
            1. We'll examine transaction processing in Chapter 6.
            2. We'll examine Data Commands in Chapter 3.
          2. Opening and Closing Connections
            1. We'll examine the DataAdapter in Chapter 4 and the TableAdapter in Chapter 8.
        2. Controlling Connection Pooling
        3. Connection Events
          1. StateChange Events
          2. InfoMessage Events
      5. Summary
    2. 3. Using Data Commands and DataReaders
      1. Understanding Data Commands
        1. We'll discuss DataAdapters in Chapter 4 and TableAdapters in Chapter 8.
          1. We'll discuss DataAdapters in Chapter 4 and TableAdapters in Chapter 8.
            1. We'll discuss DataAdapters in Chapter 4 and TableAdapters in Chapter 8.
      2. Creating Data Commands
        1. Creating Data Commands at Design Time
        2. Creating Connections at Run Time
          1. We'll discuss the version of the Data Command constructor that supports transactions in Chapter 7.
            1. We'll discuss the version of the Data Command constructor that supports transactions in Chapter 7.
      3. Configuring Data Commands
        1. We'll discuss the Transaction property in Chapter 7.
          1. We'll discuss the Transaction property in Chapter 7.
            1. We'll discuss the Transaction property in Chapter 7.
            2. See Chapter 7 for more information.
            3. We'll examine the DataAdapter in Chapter 5 and the DataRow in Chapter 7.
        2. Setting Command Properties at Design Time
        3. Setting Command Properties at Run Time
        4. Using the Parameters Collection
        5. Configuring the Parameters Collection at Design Time
        6. Adding and Configuring Parameters at Run Time
        7. Setting Parameter Values
      4. Command Methods
        1. We'll discuss DataReaders in Chapter 4 and XMLReaders in Chapter 18.
          1. We'll discuss DataReaders in Chapter 4 and XMLReaders in Chapter 18.
            1. We'll discuss DataReaders in Chapter 4 and XMLReaders in Chapter 18.
        2. Executing Commands
      5. Understanding DataReaders
        1. We'll discuss DataSets in Chapter 6.
          1. We'll discuss DataSets in Chapter 6.
            1. We'll discuss DataSets in Chapter 6.
      6. Creating DataReaders
        1. Creating DataReaders at Run Time
        2. DataReader Properties
          1. Retrieving DataReader Properties
      7. DataReader Methods
        1. The Read Method
      8. Summary
    3. 4. Using DataAdapters
      1. Understanding DataAdapters
        1. We'll discuss DataSets in detail in Chapter 6.
          1. We'll discuss DataSets in detail in Chapter 6.
            1. We'll discuss DataSets in detail in Chapter 6.
            2. We'll discuss TableAdapters in Chapter 8.
      2. Creating DataAdapters
        1. Creating DataAdapters at Design Time
        2. Creating DataAdapters at Run Time
        3. Previewing Results
      3. Configuring DataAdapters
        1. We'll examine the AcceptChanges method in Chapter 10.
          1. We'll examine the AcceptChanges method in Chapter 10.
            1. We'll examine the AcceptChanges method in Chapter 10.
            2. We'll examine DataRows in Chapter 7 and the RowError property in Chapter 10.
            3. We'll examine DataRows in Chapter 7 and the FillLoadOption in Chapter 10.
        2. DataAdapter Commands
        3. The DataTableMappingCollection
      4. Using DataAdapter Methods
        1. The Fill Method
        2. The Update Method
      5. Responding to DataAdapter Events
        1. The RowUpdating Event
        2. The RowUpdated Event
      6. Summary
    4. 5. Transaction Processing in ADO.NET
      1. Understanding Transactions
      2. Using Transactions
      3. Creating Transactions
        1. Creating Transactions at Run Time
      4. Creating Nested Transactions
      5. Committing and Rolling Back Transactions
      6. Summary
  6. III. Data Provider Objects
    1. 6. Modeling a Database by Using DataSets and DataRelations
      1. Understanding DataSets
        1. We'll discuss DataTables, DataRows and DataColumns in detail in Chapter 7.
          1. We'll discuss DataTables, DataRows and DataColumns in detail in Chapter 7.
            1. We'll discuss DataTables, DataRows and DataColumns in detail in Chapter 7.
      2. Creating DataSets
        1. We'll discuss TableAdapters in detail in Chapter 8.
          1. We'll discuss TableAdapters in detail in Chapter 8.
            1. We'll discuss TableAdapters in detail in Chapter 8.
        2. Creating Typed DataSets
        3. Creating Untyped DataSets
      3. Configuring DataSets
        1. We'll examine the DataSet's XML-related methods in Chapter 19.
          1. We'll examine the DataSet's XML-related methods in Chapter 19.
            1. We'll examine the DataSet's XML-related methods in Chapter 19.
        2. The DataSet Tables Collection
      4. Using DataSet Methods
        1. We'll examine the methods used to up-date data in Chapter 10.
          1. We'll examine the methods used to up-date data in Chapter 10.
            1. We'll examine the methods used to up-date data in Chapter 10.
        2. Cloning a DataSet
        3. Creating DataTableReaders
      5. Understanding DataRelations
      6. Creating DataRelations
      7. Configuring DataRelations
        1. We'll discuss XML in Chapters 18 and 19.
          1. We'll discuss XML in Chapters 18 and 19.
            1. We'll discuss XML in Chapters 18 and 19.
      8. Summary
    2. 7. Using DataTables
      1. Understanding DataTables
      2. Creating DataTables
        1. Creating DataTables by Using the DataSet Designer
          1. We'll examine TableAdapters in Chapter 8.
            1. We'll examine TableAdapters in Chapter 8.
        2. Creating Independent DataTables at Run Time
        3. Creating DataSet DataTables at Run Time
        4. Creating DataTables by Using a DataAdapter
      3. Configuring DataTables
        1. We'll discuss DataViews in Chapter 9.
          1. We'll discuss DataViews in Chapter 9.
            1. We'll discuss DataViews in Chapter 9.
        2. Primary Key Constraints
      4. Creating DataColumns
        1. Creating DataColumns by Using the DataSet Designer
        2. Creating DataColumns at Run Time
      5. Configuring DataColumns
        1. Creating Calculated Columns
      6. Adding Constraints
        1. ForeignKeyConstraints
        2. UniqueConstraints
      7. Creating DataRows
      8. Configuring DataRows
        1. We'll discuss the RowState property in detail in Chapter 10.
          1. We'll discuss the RowState property in detail in Chapter 10.
            1. We'll discuss the RowState property in detail in Chapter 10.
      9. Using DataTable Methods
        1. The Select Method
      10. Using DataRow Methods
      11. Responding to DataTable Events
      12. Summary
    3. 8. Using TableAdapters
      1. Understanding TableAdapters
      2. Creating TableAdapters
        1. Creating TableAdapter Classes
        2. Creating TableAdapter Objects
      3. Configuring TableAdapters
        1. Configuring TableAdapter Classes
        2. Configuring TableAdapter Objects
      4. Using TableAdapter Methods
        1. Executing Query Methods
      5. Summary
    4. 9. Using DataViews
      1. Understanding DataViews
        1. We'll examine Data Binding in Chapters 12 and 14.
          1. We'll examine Data Binding in Chapters 12 and 14.
            1. We'll examine Data Binding in Chapters 12 and 14.
      2. Creating DataViews
        1. Creating DataViews at Design Time
        2. Creating DataViews at Run Time
      3. Configuring DataViews
        1. DataColumn Expressions
        2. Sort Expressions
        3. The RowState Filter
      4. Using DataView Methods
        1. The Find Method
      5. Summary
  7. IV. Manipulating Data
    1. 10. Editing and Updating Data
      1. Understanding the Editing and Updating Process
        1. We'll examine data binding later in Part IV.
          1. We'll examine data binding later in Part IV.
            1. We'll examine data binding later in Part IV.
      2. Understanding DataRowStates and Versions
        1. The RowState Property
        2. Row Versions
      3. Editing Data in a DataSet Under Program Control
        1. We'll discuss editing using data-bound controls later in Part IV.
          1. We'll discuss editing using data-bound controls later in Part IV.
            1. We'll discuss editing using data-bound controls later in Part IV.
        2. Adding a DataRow
        3. Deleting a DataRow
        4. Changing DataRow Values
        5. Deferring Changes to DataRow Values
      4. Updating Data Sources
        1. Updating the Data Source by Using an Adapter
        2. Updating the Data Source by Using a Data Command
        3. Accepting and Rejecting DataSet Changes
        4. The AcceptChanges Method
        5. The RejectChanges Method
      5. Summary
    2. 11. Manual Data Binding in Windows Forms
      1. Understanding Data Binding in Windows Forms
      2. Binding Controls to an ADO.NET Data Source
        1. Simple-Binding Control Properties in Visual Studio
        2. Simple-Binding Control Properties at Run Time
          1. We'll examine the code that implements these buttons later in this chapter.
            1. We'll examine the code that implements these buttons later in this chapter.
            2. We'll examine the code that implements these buttons later in this chapter.
        3. Complex-Binding Control Properties
          1. We'll examine the use of the ValueMember property to create look-up tables in Chapter 13.
            1. We'll examine the use of the ValueMember property to create look-up tables in Chapter 13.
            2. We'll examine the code that implements these buttons later in this chapter.
      3. Understanding the BindingContext Object
      4. Understanding the CurrencyManager Object
        1. The Position Property
        2. CurrencyManager Methods
          1. We'll examine the SuspendBinding and ResumeBinding methods in Chapter 13.
            1. We'll examine the SuspendBinding and ResumeBinding methods in Chapter 13.
      5. Responding to CurrencyManager Events
        1. The ItemChanged Event
      6. Understanding the Binding Object
      7. Configuring the Binding Object
        1. The BindingMemberInfo Property
      8. Using Binding Object Methods
      9. Responding to Binding Object Events
        1. We'll examine the Format and Parse events in Chapter 13.
          1. We'll examine the Format and Parse events in Chapter 13.
            1. We'll examine the Format and Parse events in Chapter 13.
      10. Summary
    3. 12. Data Binding in Windows Forms by Using the BindingSource Component
      1. Understanding the BindingSource
      2. Creating BindingSource Components
        1. Creating BindingSource Components at Design Time
        2. Creating BindingSource Components at Run Time
      3. Configuring BindingSource Components
        1. DataSource and DataMember Properties
        2. Sort and Filter Properties
      4. Using BindingSource Methods
        1. Position Methods
        2. Add and AddNew Methods
      5. Responding to BindingSource Events
        1. The PositionChanged Event
      6. Summary
    4. 13. Using ADO.NET in Windows Forms
      1. Using the BindingNavigator to Navigate Through Data
      2. Displaying Data in Windows Controls
        1. Simplifying Data Entry
        2. Displaying User-Comprehensible Values
        3. Working with DataRelations
      3. Formatting Data
        1. Custom Format Strings
        2. The Format and Parse Events
          1. Using the Format Event
          2. Using the Parse Event
      4. Finding Data
        1. Finding Data by Using the Add Query Dialog Box
        2. Finding Data by Using the BindingSource
        3. Finding a Row in a DataView
        4. Finding a Row in a DataTable
      5. Validating Data in Windows Forms
        1. Data Change Events
        2. Column Change Events
        3. Control Validation Events
      6. Using the ErrorProvider Component
        1. Binding an ErrorProvider to a Form Control
        2. Binding an ErrorProvider to a Data Source
      7. Summary
    5. 14. Basic Data Binding in Web Forms
      1. Understanding Data Binding in Web Forms
        1. We'll examine Data Source controls and the Web server controls designed to work with them in Chapter 15.
          1. We'll examine Data Source controls and the Web server controls designed to work with them in Chapter 15.
            1. We'll examine Data Source controls and the Web server controls designed to work with them in Chapter 15.
      2. Simple-Binding Web Controls to an ADO.NET Data Source
        1. We'll discuss data-bound controls in detail in Chapter 15.
          1. We'll discuss data-bound controls in detail in Chapter 15.
            1. We'll discuss data-bound controls in detail in Chapter 15.
        2. Setting Control Properties Explicitly
        3. Setting Control Properties by Using DataBinder.Eval
        4. Using Data-Binding Expressions
      3. Complex-Binding Web Controls
      4. Maintaining ADO.NET Object State
        1. Maintaining ADO.NET Objects on the Server
        2. Maintaining ADO.NET Objects on the Page
      5. Implementing Navigation
      6. Updating an Underlying Data Source
        1. Using a Command Object
        2. Using an Adapter
      7. Summary
    6. 15. Data Binding in Web Forms by Using the Data Source Component
      1. Understanding the Data Source Component
      2. Creating Data Source Components
        1. Creating Data Source Components at Design Time
        2. Creating Data Source Components at Run Time
      3. Configuring Data Source Controls
        1. Connection Properties
        2. Command Properties
        3. Command Parameters
        4. Setting Connection and Command Properties at Design Time
        5. Configuration Properties
        6. Caching Properties
      4. Using Data Source Methods
      5. Responding to Data Source Events
      6. Summary
    7. 16. Data-Bound and Validation Controls
      1. Understanding Data-Bound Controls
        1. Data-Binding Properties
      2. Understanding List Controls
        1. Creating List Controls
        2. Configuring List Controls
        3. Using List Control Methods
        4. Responding to List Control Events
      3. Understanding Template-Based Controls
        1. Template Types
        2. Adding Templates to Template-Based Controls
        3. Binding Controls Within a Template
      4. Understanding DataControlFields
        1. Configuring DataControlFields
        2. The CommandField DataControlField
        3. The TemplateField DataControlField
      5. Understanding Validation Controls
      6. Summary
  8. V. ADO.NET and Other Types of Data
    1. 17. Reading and Writing XML
      1. Understanding XML Schemas
      2. Understanding ADO.NET and XML
        1. We'll examine the XmlDataDocument in Chapter 18.
          1. We'll examine the XmlDataDocument in Chapter 18.
            1. We'll examine the XmlDataDocument in Chapter 18.
      3. Using the DataSet XML Methods
        1. The GetXml and GetXmlSchema Methods
        2. The ReadXmlSchema Method
        3. The InferXmlSchema Method
        4. The ReadXml Method
        5. The WriteXmlSchema Method
        6. The WriteXml Method
      4. Controlling How XML Is Written
        1. The Nested Property of the DataRelation
        2. Controlling Column Mapping
      5. Summary
    2. 18. The XmlDataDocument
      1. Understanding the XmlDataDocument
      2. Creating XmlDataDocuments
      3. Navigating an XML Document
        1. Navigating a DataSet by Using the DOM
        2. Navigating a DataSet by Using XPath
      4. Using XmlDataDocument Methods
        1. Manipulating DataRows by Using XML
        2. Coordinating DataRows and XmlDataDocument Elements
      5. Summary
    3. 19. Using ADO and ADOX in the .NET Framework
      1. Understanding COM Interoperability
      2. Understanding ADO in the .NET Framework
        1. Establishing a Reference to ADO
      3. Creating ADO Objects
        1. The ADO Connection Object
      4. Using ADO Recordsets
        1. Filling ADO Recordsets
        2. Updating ADO Recordsets
      5. Understanding ADOX in the .NET Framework
        1. Creating Database Objects by Using ADOX
      6. Summary
  9. Index
  10. About the Author
  11. Copyright