Você está na página 1de 802

Applied ADO.

NET: Building Data-Driven Solutions


by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Back Cover
by Mahesh Chand and David Talbot ISBN:1590590732
Applied ADO.NET: Building Data-Driven Solutions provides extensive coverage of ADO.NET technology including
Apress 2003 (928 pages)
ADO.NET internals, namespaces, classes, and interfaces. Where most books cover only SQL and OLE DB data
This text
providers, Mahesh Chand andprovides extensive
David Talbot covercoverage
SQL, OLEofDB,
ADO.NET technology
ODBC data providers and the latest additions to
including
ADO.NET: Oracle, MySQL, andADO.NET
XML .NETinternals, namespaces,
data providers. Chandclasses, and also cover internals of data binding and
and Talbot
interfaces,
they provide detailed coverage and takesWindows
on both a comprehensive
Forms and look
WebatForms
XML data binding and data-bound controls.
namespaces
Since XML plays a major and classes,
role in .NET and how
development, theto integrate
authors alsoboth with a comprehensive look at XML
provide
ADO.NET.
namespaces and classes, and how to integrate both with ADO.NET.

Applied ADO.NET: Building Data-Driven Solutions begins with the basics of ASP.NET and data-bound controls. It
then delves
Table into internals of data binding and shows how to use DataGrid, DataList, and other data-bound controls
of Contents
in real-world
Applied applications. Chand
ADO.NETBuilding and Talbot
Data-Driven also discuss the ASP.NET guest book, database XML Web services, and
Solutions
even an online bookstore site development and design process. Advanced developers will learn from the coverage
Introduction
of ADO.NET architecture, related design issues, and how ADO.NET data providers are designed. The authors also
show
Chapter
how1 to -create
ADO.NET
a Custom
BasicsData Provider. Event handling, server side programming, data relations and
constraints, Active Directory,
Chapter 2 - Data Components messaging,
in Visual exception
Studio .NETand error handling, and object relational model are other topics
covered in detail.
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments About the Authors
Chapter 5 - Handling ADO.NET Events
Mahesh
ChapterChand is presentlyXML
6 - Integrating engaged as a software developer with Kruse. Inc. He has been working with Microsoft
with ADO.NET
database technologies including ODBC, DAO, ADO, and OLE-DB for over 5 years. He has a M.S. degree in Computer
Chapter a7 B.S.
Science, - Data Binding and and
in Mathematics, Windows Forms
is also Data-Bound
a Microsoft Controls
Certified Professional in VC++.In addition to his day job,
Mahesh
Chapterwrites
8 - Constraints
and programsandfor
Data
C#Relations
Corner on C#, VB .NET, ASP .NET and other .NET technologies and help site
visitors.
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
David Talbot's background is diverse, ranging from developing license plate recognition technology to television set
Chapter
boxes 11 - Stored
to scalable WebProcedures and
applications. Views
Currently, David works in the New Business Technologies group at FreeMarkets
applying
Chapter cutting edge .NET
12 - Oracle, SQLXML,technologies
and Otherto.NET
solveData
business problems. He has been working with .NET since the early
Providers
betas, developing a number of solutions for several start-ups and established companies while working on several
Chapter 13 - Developing a Custom Data Provider
.NET books and articles. Prior to the development of .NET, David worked primarily on Java, SCO UNIX, Linux, DB2,
Chapter
SQL 14 -and
Server, Developing
PERL. Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET-Building Data-Driven
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Solutions
Apress
MAHESH CHAND AND 2003 (928 pages)
DAVID TALBOT This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Copyright 2003 by Mahesh Chand and David Talbot


Table of Contents
Applied ADO.NETBuilding
All rights Data-Driven
reserved. No part Solutions
of this work may be reproduced or transmitted in any form or by any means,
Introduction
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
Chapter
system,1 without
- ADO.NET Basics
the prior written permission of the copyright owner and the publisher.
Chapter 2 - Data
ISBN (pbk): Components in Visual Studio .NET
1-59059-073-2
Chapter 3 - ADO.NET in Disconnected Environments
Printed4and
Chapter bound in in
- ADO.NET theConnected
United States of America 12345678910
Environments
Chapter 5 - Handling ADO.NET Events
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
Chapter 6 - Integrating XML with ADO.NET
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
Chapter
owner, with- Data
7 Binding of
no intention and Windows Forms
infringement Data-Bound
of the trademark.Controls
Chapter 8 - Constraints and Data Relations
Technical
Chapter 9 -Reviewer: Philip Pursglove
ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Editorial Directors: Dan Appleman, Gary Cornell, Jason Gilmore, Simon Hayes, Karen Watterson, John
Chapter 11 - Stored Procedures and Views
Zukowski
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 -Editor:
Managing Developing a Custom
Grace Wong Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Project15Manager:
Chapter Tracy Brown
- Using ADO.NET in XmlCollins
Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Development Editor: Philip Pursglove
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational
Copy Editor: Kim Wimpsett Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Compositor:
Chapter Diana
20 - COM Van Winkle,and
Interoperability Van Winkle Design Group
ADO.NET
Chapter 21 - Messaging
Artist and Cover Designer: Kurt Krames
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix
Indexer:A Ron
- Relational
StraussDatabases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Production
Appendix Manager:
C - ADO.NET Kari Brooks
Frequently Asked Questions
Index
Manufacturing Manager: Tom Debolski
List of Figures
Distributed
List of Tables to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue,
New
List of York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr.
Listings
17,of69112
List Heidelberg, Germany.
Sidebars

In the United States, phone 1-800-SPRINGER, email <orders@springer-ny.com>, or visit


http://www.springer-ny.com.

Outside the United States, fax +49 6221 345229, email <orders@springer.de>, or visit
http://www.springer.de.

For information on translations, please contact Apress directly at 2560 9th Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax: 510-549-5939, email <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 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
Applied ADO.NET: Building Data-Driven Solutions
the information contained in this work.
by Mahesh Chand and David Talbot ISBN:1590590732
The source codeApress
for thisbook is available
2003 (928 pages) to readers at http://www.apress.com in the Downloads
section. This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
To uncles Banshiinterfaces, and takes
Lal and Rajbir Singha comprehensive look at XML
Malik for their support and guidance.
namespaces and classes, and how to integrate both with
-Mahesh Chand ADO.NET.

For Nadia whose patience has carried me through this book.


Table of Contents
-DavidADO.NETBuilding
Applied Talbot Data-Driven Solutions
Introduction
About the-Authors
Chapter 1 ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Mahesh
Chapter 18Chand is a Microsoft
- Object-Relational .NET consultant,
Mapping in .NET an author, and the founder of C# Corner (www.c-
sharpcorner.com),
Chapter one ofPerformance
19 - Mapped Objects: the leading Considerations
online community
and for
Data.NET (C#, VB .NET, and ASP.NET)
Binding
developers.
Chapter He has
20 - COM a master's degree
Interoperability in computer science and a bachelor's degree in mathematics, and
and ADO.NET
he is a 21
Chapter Microsoft Certified Professional in VC++. Mahesh is also the author of A Programmer's Guide to
- Messaging
ADO.NET
Chapter 22 -inSQL
C# Server
(Apress, 2002).
and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

David Talbot has worked in a variety of technologies including television set top boxes, license plate
recognition, and scalable Web applications. He has written a number of articles concerning .NET
technologies and has worked in various capacities on three books. He is currently working as a
development manager in the New Business Technologies group at FreeMarkets in Pittsburgh,
Pennsylvania. Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress
About the Technical Reviewer
2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Philip Pursglove
Chapter 9 - ADO.NETis aException
developer with the Willis Group, based in Ipswich, United Kingdom. He has worked
Handling
with every
Chapter 10 -version
Workingofwith
Visual
theBasic
ODBCfrom
.NET3.0 onward.
Data Philip was one of the first developers in the UK to gain
Provider
the Microsoft
Chapter Certified
11 - Stored Application
Procedures Developer certification; he also holds the Microsoft Certified Solution
and Views
Developer certification in addition to a degree in business information systems. Philip writes for Pinnacle
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Publishing'sHardcore Visual Basic and SQL Server Professional magazines, and he speaks at user
Chapter 13 - Developing a Custom Data Provider
group events in the UK. He maintains a Web site at www.philippursglove.com and can be reached
Chapter 14 - Developing Database Web Applications using ASP.NET
at <phil@philippursglove.com>.
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Acknowledgments
Chapter 17 - Building Real-World Web Applications
First, I would
Chapter like to thank Karen
18 - Object-Relational Watterson
Mapping in .NETat Apress. I couldn't even think about finishing this book without
her timely
Chapter 19 -expert
Mappedcomments and motivation.
Objects: Performance Second, asand
Considerations always,
Data project
Bindingmanager Tracy Brown Collins did
a great job of managing the entire project so we could finish it within the time constraints. Without a
Chapter 20 - COM Interoperability and ADO.NET
technical reviewer, a book could never be published. Philip Pursglove did a good job of improving the
Chapter 21 - Messaging
technical aspect of the book. Thanks also to Kim Wimpsett, the copy editor, and Kari Brooks, the
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
production manager, for their hard work and for giving the book a professional look.
Appendix A - Relational Databases: Some Basic Concepts
Appendix
-MaheshBChand
- Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
I would like to thank Andy Harris for convincing me to write my first book.
Index
List of Figures
-David Talbot
List of Tables
List of Listings
List of Sidebars
Introduction
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003developers
This book is for experienced (928 pages) who want to write desktop or Web-based database applications in
This text provides
.NET. This book assumes you have extensive coverage
some prior of ADO.NET
programming technology
knowledge of an object-oriented
including ADO.NET internals, namespaces, classes, and
programming language such as C++, Java, or Visual Basic; some basic concepts of database
interfaces, and takes a comprehensive look at XML
programming; and some experience
namespaces with and
and classes, previous versions
how to of both
integrate Visual Studio (VS). This book also assumes
with
you know the basic concepts of .NET and how to install it.
ADO.NET.

The following sections briefly introduce this book's chapters.


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Chapter 1: "ADO.NET Basics"
Introduction
Chapter 1 - ADO.NET
This chapter providesBasics
you with a high-level overview of ADO.NET. This chapter covers the basics of
Chapter 2 - describes
ADO.NET, Data Components in Visualover
its advantages Studio .NET data access technologies, and briefly introduces
current
ADO.NET
Chapter 3 -classes
ADO.NETand
in namespaces. It also shows you how to use ADO.NET classes and namespaces to
Disconnected Environments
write simple
Chapter databaseinapplications
4 - ADO.NET using VS .NET (VS .NET). Finally, Microsoft VS .NET provides
Connected Environments
tremendous
Chapter support ADO.NET
5 - Handling for writingEvents
database applications in no time using its wizards and utilities. This chapter
also briefly
Chapter 6 - introduces
Integrating ADO.NET components and how they fit in the model and work together.
XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 2: by
"Data
MaheshComponents in Visual Studio
Chand and David Talbot .NET"
ISBN:1590590732
Apress 2003 (928 pages)
The Visual StudioThis
.NETtext(VS .NET)extensive
provides Integratedcoverage
Development Environment
of ADO.NET (IDE) provides design-time
technology
support to work with data components.
including In thisnamespaces,
ADO.NET internals, chapter, you'll learn and
classes, how to use these data components in
the VS .NET IDEinterfaces, and takes
at design-time a comprehensive
to create look at XMLThis chapter starts with the Server
database applications.
namespaces and classes, and how to integrate both with
Explorer, a useful tool for database applications. You'll focus on developing database applications quickly
ADO.NET.
using data components in VS .NET without writing a lot of code. You'll work through a step-by-step tutorial
to help you develop and run a project. The chapter also discusses the Connection,DataAdapter,
Command,
Table DataSet, and DataView components in more detail. After finishing this chapter, you'll have a
of Contents
good understanding
Applied of data
ADO.NETBuilding components
Data-Driven and how to work with them in VS .NET.
Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 3: by
"ADO.NET
Mahesh Chand in
andDisconnected
David Talbot Environments"
ISBN:1590590732
Apress 2003 (928 pages)
In this chapter, you'll
This get
textaprovides
broad view of the coverage
extensive ADO.NET ofarchitecture and the basic building blocks of
ADO.NET technology
ADO.NET in a disconnected environment.
including ADO.NET This
internals, chapter plays
namespaces, a major
classes, and role for later chapters as well.
Actually, ADO.NET interfaces,
not onlyand takes a
provides a comprehensive
way to work withlook at XML
databases, it also allows you to access various
namespaces and classes, and how to integrate both with
kinds of data sources and even in-memory representations of data. You'll learn how to write database
ADO.NET.
applications without using a database. You'll also explore the System.Data and System.Data.Common
namespace classes. Some of the common data objects covered in this chapter are DataTable,
DataColumn,
Table of ContentsDataRow, and DataSet.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 4: by
"ADO.NET
Mahesh Chand in
andConnected
David Talbot Environments"
ISBN:1590590732
Apress 2003 (928 pages)
This chapter examines ADO.NET
This text provides functionality in a connected
extensive coverage of ADO.NETenvironment,
technology which means you won't be
storing data in memory. You'll
including be reading
ADO.NET andnamespaces,
internals, storing dataclasses,
in data sources.
and You access data from a data
source and save interfaces,
data back and takes
to the dataa source
comprehensive
with thelook
helpatofXML
a bridge between the application and the
namespaces and classes, and how to integrate both with
data source; in ADO.NET this bridge is a data provider. ADO.NET provides many data providers for
ADO.NET.
working with different data sources to make data access fast, reliable, and easy to use. Each data provider
has data components (classes) that let you connect to a data source and read, write, add, delete, and
update
Table data. This chapter examines these components and shows how to use Windows data-bound
of Contents
controls
Applied to bind data with Data-Driven
ADO.NETBuilding ADO.NET components.
Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 5: by
"Handling
Mahesh ChandADO.NET Events"
and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Events are usefulThisfor notifying you extensive
text provides when some action happens
coverage to technology
of ADO.NET an object. An event can have a method
associated with it.including
Not too ADO.NET
many books cover namespaces,
internals, the ADO.NET eventsand
classes, in details. This chapter shows you how
interfaces,
you can handle events and takes objects.
for ADO.NET a comprehensive
It coverslook at XML
almost every ADO.NET data component event and
how to use them namespaces and classes, and how to integrate both with
in your applications.
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 6: by
"Integrating XML
Mahesh Chand and David with
Talbot ADO.NET"
ISBN:1590590732
Apress 2003 (928 pages)
This chapter begins
Thiswith
textthe introduction
provides of the
extensive classes
coverage of provided
ADO.NET by the .NET Framework Library to work
technology
with Extensible Markup Language
including ADO.NET(XML) documents.
internals, Thisclasses,
namespaces, chapteranddiscusses how to read from and write to
XML documents interfaces, and takes
using the .NET a comprehensive
Framework look at After
Library classes. XML that it discusses how to navigate
namespaces and classes, and how to integrate both with
through XML documents. The chapter also discusses XML transformations. In addition, this chapter covers
ADO.NET.
the relationship between ADO.NET and XML and shows how to mix them up and use rich ADO.NET
database components to display and manipulate XML data. Finally, this chapter covers the
XPathNavigator
Table of Contents class, which navigates XML documents.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 7: by
"Data
MaheshBinding
Chand and DavidandTalbot
Windows Forms Data-Bound
ISBN:1590590732
Apress 2003 (928 pages)
Controls" This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Data-bound controls are not only ease to use, but they also provide many built-in features that
interfaces, and takes a comprehensive look at XML
programmers willnamespaces
find useful. and
Thisclasses,
chapteranddiscusses the basics
how to integrate of data
both with binding and data-bound controls,
as well as how toADO.NET.
develop interactive Graphical User Interface (GUI) database applications using these
controls in minimal time and with minimal effort. This chapter covers how to use Windows controls such as
theTextBox,Button,Label,CheckBox,ComboBox,ListBox, and DataGrid controls in the data-
Table of Contents
binding process and how to write your custom data-bound applications to navigate through the records of
Applied
a data ADO.NETBuilding Data-Driven
source. In this chapter, Solutions
you'll learn some data-binding techniques that are hard to find in other
Introduction
ADO.NET books.
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 8: by
"Constraints
Mahesh Chand andand
David Data
Talbot Relations"
ISBN:1590590732
Apress 2003 (928 pages)
This chapter discusses constraints
This text and data coverage
provides extensive relations of
in ADO.NET
detail. First, this chapter starts with a discussion of
technology
constraints and how ADO.NET
including constraint
ADO.NET objects
internals, maintainclasses,
namespaces, data integrity.
and The second part of this chapter
interfaces, and takes a comprehensive look at XML
discusses data relations.
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 9: by
"ADO.NET
Mahesh Chand Exception
and David Talbot Handling"
ISBN:1590590732
Apress 2003 (928 pages)
Exception and errorThishandling is oneextensive
text provides of the most important
coverage parts oftechnology
of ADO.NET writing reliable and error-prone
applications. If you come from
including a C++
ADO.NET background,
internals, you're probably
namespaces, familiar with different exceptional
classes, and
handling methodsinterfaces, and takes
such as C++ a comprehensive
exceptional look at XMLexceptional handling, and Microsoft
handling, structured
namespaces and classes, and how to integrate both with
Foundation Classes (MFC) exceptions. If you come from a Visual Basic background, you're probably
ADO.NET.
familiar with the On Error statement. Besides the exception handling in previous versions of VC++ and
Visual Basic, .NET implements exceptions in a different way, and all .NET-supported languages (C# and
VB .NET)
Table share the same mechanism. Now Visual Basic developers can enjoy the same rich exception
of Contents
handling
Applied C++ developersData-Driven
ADO.NETBuilding have enjoyed. Discussing .NET exception handling is beyond the scope of this
Solutions
book; this chapter concentrates on ADO.NET exception handling classes.
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 10:by"Working with
Mahesh Chand and the
David ODBC .NET
Talbot Data Provider"
ISBN:1590590732
Apress 2003 (928 pages)
This chapter discusses theprovides
This text ODBC extensive
.NET datacoverage
providerofand how totechnology
ADO.NET work with various data sources such as
Access, MySQL, including
Excel, and text using
ADO.NET the ODBC
internals, data provider.
namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 11:by"Stored Procedures
Mahesh Chand and David Talbot and Views"
ISBN:1590590732
Apress 2003 (928 pages)
Stored procedures and
This views
text are another
provides extensiveimportant
coverageaspect of database
of ADO.NET programming. This chapter
technology
discusses how you can take
including advantage
ADO.NET of VSnamespaces,
internals, .NET tools to work with
classes, and these objects.
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 12:by"Oracle, SQLXML,
Mahesh Chand and OtherISBN:1590590732
and David Talbot .NET Data Providers"
Apress 2003 (928 pages)
In this chapter, you'll
This see
text some more
provides data providersspecifically,
extensive Oracle and SQLXML. In previous
coverage of ADO.NET technology
chapters, you learned howADO.NET
including to work with other namespaces,
internals, data providers, so working
classes, and with the rest of them is just a
piece of cake. Allinterfaces, and takes
data providers a comprehensive
implement look at
a similar class XML
hierarchy model with only a few changes. This
namespaces and classes, and how to integrate both with
chapter concentrates how to work with Oracle databases and how to access SQL Server using SQLXML.
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 13:by"Developing a Custom
Mahesh Chand and David Talbot Data ISBN:1590590732
Provider"
Apress 2003 (928 pages)
ADO.NET is an extensible architecture
This text provides that allows
extensive anyone,
coverage not just
of ADO.NET Microsoft, to create data providers for
technology
virtually any type including
of data source.
ADO.NET This chapternamespaces,
internals, covers how classes,
to createand
a data provider for pipe-delimited data
interfaces,
files, which is a common fileand takesespecially
format, a comprehensive
for Unixlook at XMLThis data provider plugs in to the
systems.
namespaces and classes, and how to integrate both with
ADO.NET architecture and provides all of the things you would expect from a data provider, including the
ADO.NET.
ability to fill a DataSet and bind it to any GUI object.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 14:by"Developing Database
Mahesh Chand and David Talbot Applications Using ASP.NET"
ISBN:1590590732
Apress 2003 (928 pages)
As the programming This world moves extensive
text provides toward thecoverage
Internet of
these days,technology
ADO.NET it's becoming important for developers
to be able to create Web applications
including and Web
ADO.NET internals, services. The
namespaces, Microsoft
classes, and .NET Framework enables you to
develop, maintain, interfaces,
and deploy andreliable
takes a and
comprehensive look at XML
high-performance Web applications and Web services. This
namespaces and classes, and how to integrate both with
chapter starts with an overview of ASP.NET and how to install it, and it then shows how to develop a
ADO.NET.
simple Web application using VS .NET. Finally, it discusses Web Forms controls and how to use them.
The Web Form's DataGrid control is a useful control to develop Web-based database applications. You
can of
Table bind a DataSet to the DataGrid control as you did for the Windows Form's DataGrid control. This
Contents
chapter discusses how toData-Driven
Applied ADO.NETBuilding fill data to aSolutions
DataGrid and how to develop powerful Web-based database
applications
Introduction using the DataGrid control. It also discusses how to enable paging in a DataGrid control.
Finally,1it shows
Chapter you how
- ADO.NET to develop an ASP.NET guest book for your Web site using ASP.NET.
Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 15:by"Using ADO.NET
Mahesh Chand in XML Web
and David Talbot Services"
ISBN:1590590732
Apress 2003 (928 pages)
Web services provide a way
This text to run extensive
provides a service coverage
on the Web and access
of ADO.NET its methods using standard protocols,
technology
including Simple including
Object Access
ADO.NET Protocol (SOAP),
internals, XML, Web
namespaces, Service
classes, and Description Language (WSDL), and
Hypertext Transfer interfaces,
Protocoland takes Technically,
(HTTP). a comprehensive look
a Web at XMLis nothing more than an application that
service
namespaces and classes, and how to integrate both with
exposes its interface over the Web to a client who wants to access the service's abilities. The uses of a
ADO.NET.
Web service include validating credit cards, searching for data in a database, inserting an order into a
shopping cart, and updating a guest list. The sky is the limit on what you can have your Web service do. In
the past,
Table JavaBeans, Component Object Model (COM) components, and other, nonstandard service
of Contents
components
Applied handled these
ADO.NETBuilding services, Solutions
Data-Driven which required specialized formats or extra, specialized components
to exchange data with the client. But Web services under .NET run by invoking methods in the service
Introduction
directly1through
Chapter HTTPBasics
- ADO.NET or SOAP, so someone wanting to run your Web service from their computer can
simply send an HTTP call
Chapter 2 - Data Components to your service,
in Visual passing
Studio .NET the parameters in a standard Uniform Resource
Locator (URL). This chapter shows you
Chapter 3 - ADO.NET in Disconnected Environments
how to use ADO.NET in XML Web services.
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 16:by"ASP.NET Server
Mahesh Chand and Controls and
David Talbot Data Binding"
ISBN:1590590732
Apress 2003 (928 pages)
Chapter 14 discussed some
This text of theextensive
provides basics of coverage
data binding in Web Forms
of ADO.NET and ASP.NET data-bound
technology
controls. You alsoincluding
saw how to use data-bound
ADO.NET controls in classes,
internals, namespaces, Web applications.
and Chapter 15 discussed how to
interfaces,
develop data-driven and takes
Web services anda how
comprehensive lookfunctionality
to access the at XML implemented in a Web service from
namespaces and classes, and how to integrate both with
Web Forms applications. This chapter extends the information in Chapter 14 and concentrates on the
ADO.NET.
more practical aspect of data binding and Web Forms data-bound controls.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 17:by"Building
Mahesh Chand Real-World
and David Talbot Web Applications"
ISBN:1590590732
Apress 2003 (928 pages)
Chapter 14 discussed the provides
This text basics ofextensive
ASP.NET and how
coverage ofto write data-driven
ADO.NET technologyWeb applications using
ASP.NET and ADO.NET. Chapter 16
including ADO.NET discussed
internals, data binding
namespaces, in ASP.NET
classes, and and ASP.NET server controls
interfaces,
and how to use these andtotakes
controls a comprehensive
develop look at
data-bound Web XML
applications. This chapter uses the functionality
namespaces and classes, and how to integrate both with
discussed in those chapters. You'll see how you can write a real-world online job board where candidates
ADO.NET.
can post their resumes, recruiters can post their jobs, recruiters can browse the available resumes, and
job hunters can browse and apply for the available jobs online.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 18:by"Object-Relational Mapping ISBN:1590590732
Mahesh Chand and David Talbot in .NET"
Apress 2003 (928 pages)
Object-relational This
mapping is a commonly
text provides desired
extensive software
coverage architecture
of ADO.NET element on any platform because
technology
of the encapsulation and modeling
including ADO.NET of data behavior
internals, from aclasses,
namespaces, relational
anddatabase into an object. Although
you can use XMLinterfaces, and takes a(XSD)
Schema Document comprehensive look at
files to create XML from data, these objects are not easily
objects
namespaces and classes, and how to integrate both with
extensible in regard to modeling data behavior. The technique discussed in this chapter extends the
ADO.NET.
concept provided by Duwamish7 (a demo application from Microsoft) to create custom-built mapped
objects.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 19:by"Mapped
Mahesh ChandObjects: Performance
and David Talbot Considerations and
ISBN:1590590732
Apress 2003 (928 pages)
Data Binding"
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Building on the object-relational concepts discussed in Chapter 18, this chapter deals with the common
interfaces, and takes a comprehensive look at XML
problems encountered in a mapped
namespaces system,
and classes, andincluding performance
how to integrate considerations and how to use data
both with
binding. Data binding is the preferred way to work with data in .NET, but you'll need to alter your code
ADO.NET.
slightly when using mapped objects. In addition, performance is always a consideration in creating a
scalable system, but often many of the things that improve performance can lead to an unmaintainable
Table of Contents
system design. In the performance part of the chapter, it discusses common performance problems such
Applied ADO.NETBuilding
as managing Data-Driven
large tree structures Solutions
and balancing the ease of use that lazy loading can provide vs.
Introduction
reducing round trips to the database.
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 20:by"COM Interoperability
Mahesh Chand and David Talbot with ADO.NET"
ISBN:1590590732
Apress 2003 (928 pages)
COM has been around forprovides
This text many years and iscoverage
extensive used byofmany Windows
ADO.NET applications. Eventually .NET
technology
platform will replace COM,ADO.NET
including but until internals,
then you namespaces,
may need to classes,
developand
COM components from .NET and
access them frominterfaces,
unmanaged and code,
takes aorcomprehensive
you may wantlook at XML
to use COM components in .NET applications.
namespaces and classes, and how to integrate both with
COM interoperability (also referred to as COM Interop) enables you to use existing COM components in
ADO.NET.
.NET applications or use .NET components in unmanaged code. This chapter shows you how to use ADO
and other COM-based data access technologies through COM Interop in your .NET applications.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 21:by"Messaging"
Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
A messaging service
This is one
text of the most
provides effective
extensive and popular
coverage mediums
of ADO.NET to communicate between two
technology
machines. It doesn't matterADO.NET
including if you'reinternals,
a high-tech developer,classes,
namespaces, a stockbroker,
and a high-school student, or a
businessperson. interfaces, and takes
In some means, youaprobably
comprehensive look at XML
use messaging services. Some of the common messaging
namespaces and classes, and how to integrate both with
services are electronic mail (email), instant messenger services, text messaging via phones, or messaging
ADO.NET.
used in complex distributed applications. This chapter covers messaging and its related functionality
defined in the .NET Framework Class Library, as well as how to use it in your applications.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Chapter 22:by"SQL Server
Mahesh Chand andTalbot
and David ADO.NET: ISBN:1590590732
Notes on Performance"
Apress 2003 (928 pages)
Database performance has
This text alwaysextensive
provides been something
coverageofofaADO.NET
black arttechnology
consisting largely of obscure tricks.
Although there's no single ADO.NET
including trick that internals,
will makenamespaces,
your application scale,
classes, andthere are several tricks that can help
interfaces,
you identify and address and takes aproblems.
performance comprehensive look at XML
This chapter covers those tricks. It also discusses some
namespaces and classes, and how to integrate both with
best practices to make sure you get most out of your code.
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Appendix A:
by "Relational
Mahesh Chand andDatabases:Some
David Talbot Basic Concepts"
ISBN:1590590732
Apress 2003 (928 pages)
Appendix A covers some
This text of the basics
provides of databases,
extensive coverage such as locking,
of ADO.NET cursors, and normalization.
technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Appendix B: "Commonly
by Mahesh Used
Chand and David SQL Statements"
Talbot ISBN:1590590732
Apress 2003 (928 pages)
SQL is a vital partThis
of database programming.
text provides Appendix
extensive coverage of B is a quick
ADO.NET reference to SQL statements and
technology
shows you how toincluding
write various SQLinternals,
ADO.NET statements.
namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
Appendix C: "ADO.NET
by Mahesh Frequently
Chand and David Talbot Asked Questions"
ISBN:1590590732
Apress 2003 (928 pages)
This appendix answers some
This text of theextensive
provides commonly asked of
coverage questions
ADO.NETrelated to ADO.NET. Most of these
technology
questions were originally
includingasked on the
ADO.NET discussion
internals, forums at
namespaces, C# Corner
classes, and (www.c-sharpcorner.com) or
interfaces, and takes a comprehensive look at XML
other newsgroups.
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Chapter Applied
1: ADO.NET Basics
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Highlights This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Activex data objects for the and
interfaces, .NETtakes
Framework (ADO.NET)
a comprehensive lookisatthe latest database access technology from
XML
Microsoft. ADO.NET namespaces
addressesandissues
classes, and
with how to integrate
previous databaseboth withtechnologies and provides the
access
ADO.NET.
foundation for future scalability. Although ADO.NET stands for Active Data Objects .NET, it's perhaps
misnamed because unlike ActiveX Data Objects (ADO), ADO.NET isn't an ActiveX/Component Object
Model
Table of (COM) technology. As the business world is moving onto the Internet, one of the main goals of
Contents
ADO.NET is allow developers
Applied ADO.NETBuilding to writeSolutions
Data-Driven high-performance, reliable, and scalable database applications over
the Internet.
Introduction
ADO.NET uses .NET Common Language Runtime (CLR) services to manage the library, and
it utilizes Extensible Markup Language (XML) to cache the data and exchange data among applications
Chapter 1 - ADO.NET Basics
over the Internet as well as intranets.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 in- this
Our goal ADO.NET in Disconnected
chapter Environments
is simply to provide you with a high-level overview of ADO.NET. In it, we provide
Chapter 4 -ofADO.NET
the basics ADO.NET,in Connected Environments
describe its advantages over current data access technologies, and briefly
Chapter 5 ADO.NET
introduce - Handling ADO.NET Events
classes and namespaces as well as show how to use them to write simple database
applications
Chapter using Microsoft
6 - Integrating Visual
XML with Studio .NET (VS .NET). VS .NET provides tremendous support for
ADO.NET
writing 7database
Chapter applications
- Data Binding in no time,
and Windows using
Forms its wizardsControls
Data-Bound and utilities. (Chapter 2 covers VS .NET and
ADO.NET
Chapter 8 -support.) In this
Constraints and chapter, we also briefly describe the ADO.NET components and how they fit in
Data Relations
the model
Chapter 9 -and work together.
ADO.NET ExceptionChapters
Handling 3 and 4 discuss these components in more detail. Specifically, in
Chapter
Chapter 103,- we coverwith
Working the ADO.NET object
the ODBC .NET model
Data in disconnected environments, and in Chapter 4, we
Provider
cover ADO.NET
Chapter in Procedures
11 - Stored connected and
environments.
Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Note The .NET Base Class Library is also called the .NET Runtime Library or the .NET Framework
Chapter 13 Class
- Developing
Library.a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Overview ofApplied
Microsoft Data
ADO.NET: Access
Building Technologies
Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
To understand the importance
Apress of ADO.NET,
2003 (928 pages) it's useful to take a quick look at existing Microsoft database
access technologies before ADO.NET.
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Since the first release of Open
interfaces, andDatabase Connectivity (ODBC)
takes a comprehensive to access databases for Windows
look at XML
namespaces
applications, Microsoft and introducing
has been classes, andand
howimproving
to integrate
waysboth
towith
make database programmers' lives
ADO.NET.
easier. In this series of database technologies, it introduced many new technologies and improved existing
technologies. Each of these technologies has its own pros and cons. Some of the technologies are
specific to a particular kind of data source, and some of them are common to all kinds of data sources.
Table of Contents
Together these technologies are called Microsoft Data Access Technologies or Microsoft Data Access
Applied ADO.NETBuilding Data-Driven Solutions
Components (MDAC). These technologies include the following:
Introduction
Chapter 1 - ADO.NET Basics
ODBC
Chapter 2 - Data Components in Visual Studio .NET
Data
Chapter 3 Access Objects
- ADO.NET (DAO)
in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Microsoft Foundation Classes (MFC) ODBC and DAO classes
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating
Remote XML(RDO)
Data Objects with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Object
Chapter 8 - Linking and and
Constraints Embedding Database (OLE DB)
Data Relations
Chapter 9 - ADO.NET Exception Handling
ADO
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
ODBC
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
ODBC was the first formal database access technology introduced by Microsoft. ODBC provides a low-
Chapter 14 - Developing Database Web Applications using ASP.NET
level C/C++ Application Programming Interface (API) to retrieve data from relational databases such as
Chapter 15 - Using
SQL Server, ADO.NET
Access, in Xml
or Oracle byWeb
usingServices
ODBC drivers and ODBC Administration. With the help of ODBC
Chapter 16 - ASP.NET Server Controls and Datayou
drivers and the ODBC Administration utility, Binding
can connect to any ODBC-compliant data source using
Chapter 17 - Building Real-World Web Applications
the ODBC API. However, ODBC is relational in nature, so it's difficult to use ODBC to communicate with
Chapter 18 - Object-Relational
nonrelational Mapping
data sources such in .NET
as object databases, network directory services, email stores, and so on.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Figure 20
Chapter 1-1-shows ODBC connectivity
COM Interoperability to a database through an ODBC driver and ODBC Administration,
and ADO.NET
also called
Chapter 21 - the ODBC driver manager.
Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 1-1: ODBC architecture

Using ODBC functions isn't a fun way of programming for high-level programmers. ODBC is a hierarchical
model, which means you have to create environment and database objects before creating a recordset
object with which you can start accessing the data. Other problems with ODBC are multiple connections
and multiple data sources. An ODBC connection doesn't allow you to connect to two data sources using
the same connection. You need to use two separate connection objects to do so. Being non-object-
oriented in nature and having poor memory management are other drawbacks of ODBC.

For the History Buffs: How ODBC Came to Be [1]


In early 1988,Applied
Microsoft's Applications
ADO.NET: Group
Building realized it Solutions
Data-Driven had a problem. Microsoft had a whole slew of
applications that needed to share data. Enter
by Mahesh Chand and David Talbot Kyle Geiger. Geiger, fresh from eight years of
ISBN:1590590732
experience atApress
WangLaboratories-primarily
2003 (928 pages) in PC databases-recalls that he was given this sort of ill-
defined problemThis text provides extensive coverage of ADO.NET technologythought about it for awhile and
and told to go "think about it for awhile." Well, Geiger
restated the problem
including as one of internals,
ADO.NET deliveringnamespaces,
data "from anywhere"
classes, andinto Microsoft applications, producing
interfaces,
a first specification and takes
by spring 1988.a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
By spring 1989, the emerging ODBC specification had gone through several drafts. Geiger and
members of Microsoft's Applications and Systems groups wrestled with how much reliance they
should put on the just-inherited Sybase DB-Library API. Concurrently, Geiger heard about what was
Table of Contents
eventually to become the SQL Access Group (SAG). Then came three grueling years of consensus
Applied ADO.NETBuilding Data-Driven Solutions
building as a member of the SAG. The culmination of the first phase was the March 1992 ODBC
Introduction
Developers Conference where the ODBC 1.0 Software Development Kit (SDK) was demonstrated
Chapter 1 - ADO.NET Basics
and distributed.
Chapter 2 - Data Components in Visual Studio .NET
The
Chapter 3 design goalsinofDisconnected
- ADO.NET ODBC wereEnvironments
to provide location, a Database Management System (DBMS), and
particular
Chapter ODBC in
4 - ADO.NET driver transparency;
Connected to provide SQL as a language for universal data access, even to
Environments
non-SQL
Chapter data sources;
5 - Handling ADO.NETto Events
leverage existing and evolving standards; and to provide tools for easy
ODBC
Chapter 6 - setup and administration.
Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Although ODBC was designed as a Windows-specific API, because it was built on top of the SAG's
Chapter 8 - Constraints and Data Relations
Call Level Interface specification, the potential for cross-platform, ODBC-compliant APIs was always
Chapter 9 - ADO.NET Exception Handling
present. In fact, in 1994, ODBC links to both the Unix and Macintosh worlds (via Apple Data Access
Chapter 10 - Working
Language, withbegan
or DAL) the ODBC .NET Data Provider
to appear.
Chapter 11 - Stored Procedures and Views
Meanwhile,
Chapter Microsoft
12 - Oracle, revised
SQLXML, the ODBC
and Other specification
.NET Data Providers and introduced it as the ODBC 2.0 SDK. The
main
Chapter 13 difference between
- Developing ODBC
a Custom Data1.0 and ODBC 2.0 was new 32-bit support and enhanced functionality
Provider
for 14
Chapter the- so-called "navigational"
Developing Database Web model. The latter
Applications usingessentially
ASP.NET maps SQL functionality to non-SQL
databases
Chapter and
15 - Using data sources
ADO.NET in Xml including xBASE and Paradox.
Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
DAO
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
DAO is a set of COM interfaces for accessing databases; it uses the Microsoft Jet database engine to
Chapter 21 - Messaging
connect. DAO is best suited for accessing desktop databases including Access, FoxPro, and SQL Server
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
because it can directly connect to these databases through the Jet database engine. Figure 1-2 shows the
Appendix A - Relational Databases: Some Basic Concepts
connectivity between a client application, an Access database, and ODBC data sources using DAO.
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 1-2: DAO model

DAO is easier to use than the ODBC API, but it doesn't provide the degree of low-level control and
flexibility afforded by the ODBC API. Therefore, you could classify DAO as a high-level database interface.
To access non-Access databases, DAO uses ODBC internally, which adds one more layer of API to
access ODBC data sources. This degrades the performance of DAO while accessing ODBC data
sources.
MFC ODBCApplied
and DAO Classes
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
MFC is a set of C++ classes that provides DAO and ODBC classes to access databases that use DAO
Apress 2003 (928 pages)
and ODBC. These classes provide a high-level programming model for Visual C++ developers working
This text provides extensive coverage of ADO.NET technology
with DAO and ODBC. These classes are a nice wrapper around DAO and ODBC APIs. A drawback of
including ADO.NET internals, namespaces, classes, and
these classes is that they aren't
interfaces, flexible
and takes enough. Also, they
a comprehensive look are slow in performance because of too many
at XML
layers. namespaces and classes, and how to integrate both with
ADO.NET.

RDO
Table of Contents
RDO isADO.NETBuilding
Applied an object interface for ODBC Solutions
Data-Driven that provides an OLE layer on the top of the ODBC API. The main
advantage of RDO is that it gives developers an object interface for ODBC developers, but the
Introduction
disadvantage is that it's slow in performance because of too many layers. It also suffers the ODBC
Chapter 1 - ADO.NET Basics
limitation of not being well suited for nonrelational data sources.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
OLE DB
Chapter 4
- ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
OLE DB was designed to access all kinds of data including relational, nonrelational, text data, graphical
Chapter 6 - Integrating XML with ADO.NET
data, and directory services. OLE DB components consist of data providers, which expose their data; data
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
consumers, which use data; and service components, which process and transport data (for example,
Chapter 8 - Constraints and Data Relations
query processors and cursor engines). OLE DB is a low-level programming model and provides the most
Chapter 9 and
flexibility - ADO.NET Exception
control over data.Handling
Figure 1-3 shows the OLE DB model.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - 1-3:
Figure COM OLE
Interoperability
DB model and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
As you can see from Figure 1-3, a client application accesses a database through an OLE DB provider
Appendix A - Relational Databases: Some Basic Concepts
and consumers. You must have an OLE DB provider installed to use OLE DB in your application; database
Appendix B - Commonly Used SQL Statements
venders provide this.
Appendix C - ADO.NET Frequently Asked Questions
Index
ADO
List of Figures
List of Tables
ActiveX Data Objects (ADO) is a set of ActiveX components that provide object-based programmatic
List of Listings
access to OLE DB. ADO is a COM wrapper around OLE DB libraries. ADO is much simpler to use than
List of Sidebars
OLE DB; you can classify it as a high-level database interface.

In comparison to other data access technologies, ADO provides many benefits. ADO is easy to program
and, with a few exceptions, is faster than ODBC and DAO. ADO also provides more flexibility and control
over data. You don't have to go through a hierarchy of objects to access data as you do in DAO or ODBC.

ADO (OLE DB) is a set of COM interfaces. This means ADO provides a binary standard to access any kind
of data source. You can use ADO with any language that supports COM, such as Visual Basic (VB) or
C++. ADO also supports XML data access with the help of Microsoft XML (MSXML).

ADO.NET
ADO.NET is an evolution of ADO. The ADO.NET and XML .NET APIs construct a uniform model to work
Applied ADO.NET: Building Data-Driven Solutions
with XML data from any type of data store. The ADO.NET API is managed code, which means you get all
the advantages ofby.NET,
Mahesh Chand and
including David Talbot
automatic memory management ISBN:1590590732
and garbage collection. Unlike DAO
Apress 2003 (928 pages)
or ODBC, though, you don't have to install new libraries to make an ADO.NET program work if you are
This text
already running .NET provides extensive
applications coverage
on a machine. We of ADO.NET
discuss technology
ADO.NET in more detail in the following
including ADO.NET internals, namespaces, classes, and
section.
interfaces, and takes a comprehensive look at XML
[1]Karen Watterson, "ODBC," and
namespaces Microsoft SQL
classes, andServer
how toConnection, July
integrate both 1992.
with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
What Is ADO.NET?
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
ADO.NET is a new database
Apress 2003 access technology created by Microsoft; its .NET Framework can access any
(928 pages)
kind of data source. It's a collection of object-oriented
This text provides extensive classes
coverage of ADO.NETthattechnology
provides a rich set of data components
to create high-performance, reliable, and scalable database applications
including ADO.NET internals, namespaces, classes, and for client-server applications as
well as distributedinterfaces, and takes
environments over athecomprehensive look at XML
Internet and intranets.
namespaces and classes, and how to integrate both with
ADO.NET.
In the ADO.NET modelunlike ADO (in connected state) and previous data access
technologiesapplications are connected to the data sources only when they are reading or updating the
data. After that, the connection closes. This is important because in client-server or distributed
Table of Contents
applications, having connection resources open all the time is one of the most resource-consuming parts.
Applied ADO.NETBuilding Data-Driven Solutions
You don't have to connect to a data source all the time; the only time you need to connect to a data source
Introduction
is when you are reading and writing final changes to a data source.
Chapter 1 - ADO.NET Basics
Chapter
The old2 approach
- Data Components in Visual Studio
of having connections open.NET
all the time is problematic when considering the number of
Chapter
clients 3that- access
ADO.NETtheinsame
Disconnected Environments
data source simultaneously. In larger systems, Microsoft Transaction Server
(MTS)/COM+
Chapter was often
4 - ADO.NET used to pool
in Connected ADO connections to address some of this resource consumption;
Environments
however,
Chapter 5 ADO.NET
- Handling provides
ADO.NET aEvents
more optimal paradigm for large systems without needing to utilize
MTS/COM+
Chapter enterprise XML
6 - Integrating servicesunless,
with ADO.NET of course, you need to take advantage of such advanced
techniques
Chapter 7 - as a three-phase
Data commit. Forms Data-Bound Controls
Binding and Windows
Chapter 8 - Constraints and Data Relations
ADO.NET uses Structured Query Language (SQL) queries and stored procedures to read, write, update,
Chapter 9
and delete- data
ADO.NET Exception Handling
from a data source. You use SQL queries through the ADO.NET Command object, which
Chapter
returns dataWorking
10 - withof
in the form the ODBC .NET Data
DataReader Provider objects. After that connection is closed with the data
or DataSet
Chapter 11 - Stored Procedures and Views
source, you use DataSet objects to work with the data and connect to the data source again when you
Chapter
need to12 - Oracle,
update the SQLXML, and Other .NET Data Providers
data source.
Chapter 13 - Developing a Custom Data Provider
Note
Chapter 14 SQL queries Database
- Developing are also called SQL statements.
Web Applications See Appendix B for some SQL query examples.
using ASP.NET
Chapter 15 See Chapter
- Using 11 for
ADO.NET in more details
Xml Web on stored procedures.
Services
Chapter 16 - ASP.NET Server Controls and Data Binding
ADataSet is a collection of DataTable objects and the relationships among them. It works as a
Chapter 17 - Building Real-World Web Applications
container that stores returned data from a database in cached form. You can fill a DataSet with the data
Chapter 18 - Object-Relational Mapping in .NET
retrieved from multiple tables of a database. Once you have a DataSet (which is disconnected data, stored
Chapter
on your19 - Mapped
local Objects:
machine), Performance
you treat Considerations
the DataSet as your and
dataData Binding
source and work with it. You call the Update
Chapter
method20to-make
COM Interoperability
DataSet changes and ADO.NET
final to the actual database. You can even read and update a
Chapter
DataSet 21 -from
Messaging
different data sources.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
You access
Appendix a data source
A - Relational and fillSome
Databases: a DataSet via data providers. The .NET Framework provides three
Basic Concepts
differentBtypes
Appendix of data providers
- Commonly Used SQL that ship with the version 1.0 of Visual Studio .NET: Sql, OleDb, and Odbc.
Statements
MicrosoftC and
Appendix third parties
- ADO.NET have Asked
Frequently also developed
Questions data providers for the Oracle database. Some companies
also have developed data providers for MySql and other databases. You use a DataAdapter object of a
Index
data provider and call its Fill method to fill a DataSet.
List of Figures
List
XML of plays
Tablesa major role in ADO.NET. The ADO.NET model utilizes XML to store the data in cache and
List of Listings
transfer the data among applications. DataSets use XML schemas to store and transfer data among
List of SidebarsYou can even use this XML file from other applications without interacting with the actual
applications.
DataSet. Because XML is an industry standard, data can be used among all kinds of applications and
components; because of its text-based nature, data can be transferred via many protocols, such as
Hypertext Transfer Protocol (HTTP). We discuss XML and ADO.NET integration and how you can take
advantage of XML support in ADO.NET applications in Chapter 6.
Why ADO.NET Was
Applied Designed
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
As mentioned, each previous
Apress 2003 data access technology has its own pros and cons. Some of them were
(928 pages)
designed specifically for a particular type of data
This text provides extensive source,
coverage some of technology
of ADO.NET them aren't easy to program for
developers, and some
including ADO.NET internals, namespaces, classes, and For example, the ODBC API is a set
of them don't have good enough performance.
interfaces,
of C functions. It has and takes a comprehensive
no object-oriented look at XMLapplications using ODBC is a time-
support. So, developing
consuming process namespaces and classes,
for developers. andproblem
Another how to integrate
with ODBC bothiswith
its relational nature. The ODBC API is
ADO.NET.
limited to relational databases, and it's difficult to use ODBC with nonrelational data sources such as
network directories, email stores, ISAM data, object data, and so on.
Table of Contents
DAO is an object model but was specifically designed to work with the Microsoft Jet database engine. The
Applied ADO.NETBuilding Data-Driven Solutions
MFC ODBC and DAO classes are one more wrapper for ODBC and DAO APIs. Because of this extra
Introduction
wrapper, these classes aren't up to the mark performance-wise and still have some of the same limitations
Chapter 1 - ADO.NET Basics
as ODBC and DAO.
Chapter 2 - Data Components in Visual Studio .NET
Chapter
OLE-DB 3 is- aADO.NET
solution in Disconnected
that works with Environments
any kind of data source using OLE-DB providers and consumers;
however,
Chapter 4 writing OLE-DB
- ADO.NET applications
in Connected is time consuming, and it's hard for developers to program them.
Environments
Moreover,
Chapter 5 -OLE-DB
HandlingisADO.NET
a flat model.
EventsThat means you could directly connect to a table without going through
hierarchical
Chapter objects of connections
6 - Integrating and databases. Because of the lower-level programming model of the
XML with ADO.NET
OLE API,
Chapter 7 the OLE-DB
- Data API
Binding isn't
and easy toForms
Windows program for non-OLE
Data-Bound programmers.
Controls
Chapter 8 - Constraints and Data Relations
To overcome this problem, Microsoft introduced ADO. ADO is a high-level programming model, and it was
Chapter 9 - ADO.NET Exception Handling
the best data access technology at that point. It's a nice COM wrapper for the OLE-DB API, and it suits
Chapter 10 - Working with the ODBC .NET Data Provider
most developers. The first major drawback of ADO is that it's a COM-based model. Writing COM-based
Chapter 11 - Stored
applications Procedures
and using them isand Views one of the most difficult models of programming and a pain for
probably
Chapter 12 - Oracle, SQLXML, and Other
non-COM developers. The second drawback .NET Data Providers
is the limited support for XML. You can read XML documents
Chapter
through13ADO,
- Developing a Custom
but documents Data
have to Provider
have a certain format, which negates many of the benefits of XML.
Chapter 14 - Developing Database Web Applications using ASP.NET
Keeping
Chapter 15all these ADO.NET
- Using problemsininXml
mind,
WebMicrosoft
Serviceswrote a set of new object-oriented classes: ADO.NET.
ADO.NET
Chapter 16 -inherits
ASP.NET itsServer
programming
Controls model from
and Data its predecessor ADO, so ADO programmers will see
Binding
something
Chapter 17 -common between these
Building Real-World two APIs. As such, it will also take less time to jump from ADO to
Web Applications
ADO.NET. ADO.NET classes are based on the ADO model and have some objects in common, such as
Chapter 18 - Object-Relational Mapping in .NET
Connection and Command. However, DataSets replace recordsets in ADO.NET. These classes are
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
easy to use and have solutions for all the problems mentioned.
Chapter 20 - COM Interoperability and ADO.NET
Chapter
In brief,21 - Messaging
ADO.NET was designed to meet today's programming needs, including disconnected data, tight
Chapter
integration-with
22 SQL XML,
Server anda ADO.NET:
and Notes on
common model to Performance
work with all kinds of data sources. Welcome to the
Appendix
ADO.NET A -world.
Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
AdvantagesApplied
of ADO.NET
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
ADO.NET offers Apress
severaladvantages over previous Microsoft data access technologies, including ADO.
2003 (928 pages)
The following sections outline these advantages.
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Single Object-Oriented API
namespaces and classes, and how to integrate both with
ADO.NET.
As discussed earlier, with the many available data access methods, sometimes developers are confused
about which technology to use. For example, to work with Microsoft Access databases, you can use
Table of Contents
ODBC, DAO, OLE-DB, ADO, DAO, or ODBC MFC classes.
Applied ADO.NETBuilding Data-Driven Solutions
Which one would you choose? It all depends on your requirements. Another problem with these
Introduction
technologies
Chapter is installing
1 - ADO.NET the right version. For example, ODBC and ADO have different versions of DLLs,
Basics
and some -ofData
Chapter 2
these versions aren't backward or forward compatible. So, if a user is running some
Components in Visual Studio .NET
programs that require ODBC 3.0 and your application uses ODBC 3.5, then the user will have to install
Chapter 3 - ADO.NET in Disconnected Environments
ODBC 3.5 to run your application. However, the users' programs that are based on ODBC 3.0 may not
Chapter 4 - ADO.NET in Connected Environments
work properly after installing ODBC 3.5. Having multiple DLL versions is a well-known problem, termed
Chapter 5 - Handling ADO.NET Events
DLL hell.
Chapter 6 - Integrating XML with ADO.NET
Chapter
ADO.NET7 -provides
Data Binding and object-oriented
a single Windows Forms set Data-Bound Controls
of classes. Different data providers work with different data
sources,
Chapter 8 but the programming
- Constraints and Datamodel for all these data providers work in the same way. So, if you know
Relations
how to 9work
Chapter with one Exception
- ADO.NET data provider, you can easily work with others. It's just a matter of changing class
Handling
names10
Chapter and- Working
connection
withstrings.
the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Note You can use more than one data provider to access a single data source. For example, you can
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
use Odbc or OleDb data providers to access Microsoft Access databases.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing
The ADO.NET classesDatabase
are easy Web Applications
to use and easy using ASP.NET because of their object-oriented nature.
to understand
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Managed Code
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational
The ADO.NET Mapping in
classes are managed .NET They have all the advantages of the .NET CLR, such as
classes.
Chapter 19 independency
language - Mapped Objects:
andPerformance Considerations
automatic resource and DataAll
management. Binding
.NET languages access the same API.
So, if you
Chapter 20 know
- COMhow to use theseand
Interoperability classes in C#, you will have no problem using them in VB .NET. Another
ADO.NET
big advantage
Chapter is you don't have to worry about memory allocation and freeing it. The CLR takes care of it
21 - Messaging
for you.22 - SQL Server and ADO.NET: Notes on Performance
Chapter
Appendix A - Relational Databases: Some Basic Concepts
XML Support
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Today, XML is an industry standard and the most widely used method of sharing data among applications
Index
over
List of the Internet. As mentioned earlier, in ADO.NET data is cached and transferred in XML format. All
Figures
components
List of Tables and applications can share this data, and data can be transferred via different protocols such
as of
List HTTP. We explain this topic in more detail in Chapters 6 and 7.
Listings
List of Sidebars
Visual Data Components
VS .NET offers ADO.NET components and data-bound controls to work in visual form. That means you
can use these components as you use any Windows controls. You drag and drop these components on
Windows and Web Forms, set their properties, and write events. This helps programmers to write less
code and develop applications in no time. VS .NET also offers the Data Form Wizard, which helps you
create full-fledged database applications without writing a single line of code. Using these components,
you can directly bind these components with data-bound controls by setting these control's properties at
design-time.Chapter 4 explains this in detail.

Performance and Scalability


Performance andApplied
scalability are two major factors when developing Web-based applications and services.
ADO.NET: Building Data-Driven Solutions
Transferring data from one source to another is a costly affair over the Internet because of connection
by Mahesh Chand and David Talbot ISBN:1590590732
bandwidth limitations and rapidly increasing traffic. Using disconnected cached data in XML takes care of
Apress 2003 (928 pages)
both of these problems.
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
ComparingApplied
ADO.NET and
ADO.NET: ADO
Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Prior to ADO.NET, ADOwas
Apress 2003 the
(928most
pages)popular and powerful Microsoft data access technology. So, how
does ADO.NET improve on ADO? To understand
This text provides extensive the of
coverage power of ADO.NET,
ADO.NET technologyit's not a bad idea to compare
ADO.NET with ADO.
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces
The ADO.NET model and classes,
is inherited from its and how to integrate
predecessor both
ADO, but withare some key differences between
there
ADO.NET.
ADO and ADO.NET. The following sections describe these differences.

Connections and Disconnected Data


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
With ADO.NET, you use as few connections as possible and have more disconnected data. In fact, it's
Introduction
recommended
Chapter to useBasics
1 - ADO.NET disconnected data in ADO.NET. In ADO.NET, disconnected data is stored in the
form of in-memory DataTable or XML DataSet objects. Having disconnected data not only reduces the
Chapter 2 - Data Components in Visual Studio .NET
load and resource consumption on the server and network, but it also provides better performance.
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET
The ADO.NET in Connected
DataSet not only Environments
supports disconnected data, but it provides rich features in comparison
Chapter 5 - recordset.
to an ADO Handling ADO.NET Events
A DataSet, which is a collection of DataTable objects, can store data, but it can
Chapter 6 -database
also store Integrating XML
table with ADO.NET
schemas and the relationship between multiple tables.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8
- Constraints and Data Relations
Recordsets vs. DataSets
Chapter 9 - ADO.NET Exception Handling
Chapter
In ADO,10the
- Working
in-memorywithrepresentation
the ODBC .NETofData
dataProvider
is the recordset. A recordset is a set of records that can be
Chapter 11database
an entire - Stored Procedures and Views
table, a section of a table, or a join of more than one table. You use SQL statements to
return the
Chapter 12 -set of data.
Oracle, You use
SQLXML, anda Other
recordset's MoveNext,
.NET Data MovePrevious,MoveFirst, and MoveLast
Providers
methods
Chapter 13to navigate through
- Developing the Data
a Custom data.Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Note Even though there's no support of move methods in a DataSet, you can implement them using
Chapter 15 - Using ADO.NET in Xml Web Services
data binding. We discuss data binding in more detail in Chapter 7.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building
In ADO.NET, Real-World
the recordset Web Applications
is gone. The new objects introduced in ADO.NET that serve the purpose of the
Chapter 18 are
recordset - Object-Relational
the DataSet and Mapping in .NET
DataReader. A DataSet object represents a DataSet, and a
Chapter objectObjects:
19 - Mapped
DataReader represents a DataReader
Performance in ADO.NET.
Considerations A DataSet
and Data Binding is a collection of one or more
than one
Chapter 20 table.
- COMAInteroperability
DataSet can and alsoADO.NET
contain metadata of database tables, such as their relationships,
constraints,
Chapter and primary and foreign keys. A DataTable object represents a table of a DataSet. With
21 - Messaging
the help
Chapter 22of- the Server and object,
SQLDataView ADO.NET:youNotes
can have multiple views of the same DataSet.
on Performance
Appendix A - Relational Databases: Some Basic Concepts
ADataSet doesn't have any move methods to navigate through DataSet records like a recordset does.
Appendix B - Commonly Used SQL Statements
In ADO.NET, a DataTable is a collection of DataRows. You use a DataRow object to navigate through
Appendix C - ADO.NET Frequently Asked Questions
the records. ADO.NET also provides DataRelation objects to navigate through master and detail
Index
records using primary and foreign keys. The DataReader object provides fast-cached, forward-only data
List of Figures
to read data and navigate through its records.
List of Tables
List of Listings
XML Support
List of Sidebars

The ADO.NET model uses XML to store and transfer data among applications, which is not only an
industry standard but also provides fast access of data for desktop and distributed applications. ADO uses
COM marshaling to transfer data among applications, which is not only hard to program but also not as
efficient or reliable as XML because of its limitations, its data types and their conversions, and the nature of
COM. Using XML, it's easy to program XML files, and you can also transfer data through firewalls using
HTTP, which isn't possible using COM marshaling.
Overview ofApplied
ADO.NET Namespaces
ADO.NET: and
Building Data-Driven Classes
Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
TheSystem.Data namespace
Apress and
2003 (928 its five supporting namespaces define the ADO.NET functionality. In
pages)
this section, we briefly describe these namespaces.
This text provides extensive coverageLater chaptertechnology
of ADO.NET describe these namespaces and
classes in more detail. These namespaces reside in the System.Data.dll
including ADO.NET internals, namespaces, classes, and assembly.
interfaces, and takes a comprehensive look at XML
namespaces
Note In the .NET and classes,
Framework, and how
each class to integrate
belongs both with A namespace is a logical group of
to a namespace.
ADO.NET.
related types and their definitions. To access a type defined in a namespace, you must reference
the namespace by using the Imports keyword or by referencing it directly. For example, the
System namespace defines all the system-related functionality. For example, the Console
Table of Contents
class of the System
Applied ADO.NETBuilding namespace
Data-Driven represents the system console. If you want to access the
Solutions
Console
Introduction class, you need to call System.Console or Imports System in your application.
Chapter 1 - ADO.NET Basics
The six general ADO.NET namespaces are System.Data,System.Data.Common,
Chapter 2 - Data Components
System.Data.OleDb, in Visual Studio .NET
System.Data.Odbc, System.Data.SqlClient, and
Chapter 3 - ADO.NET in Disconnected
System.Data.SqlTypes. BesidesEnvironments
these namespaces, each new data provider may have its own
Chapter 4 - ADO.NET
namespace. in Connected
For example, Environments
if you install the Oracle .NET data provider, the installation adds a new
Chapter 5 - Handling
namespace, ADO.NET Events
the Microsoft.Data.OracleClient namespace, which also belongs to ADO.NET.
Chapter 6 - Integrating XML with ADO.NET
TheSystem.Data
Chapter namespace
7 - Data Binding is the Forms
and Windows core namespace
Data-Bound of ADO.NET. It consists of the base classes for
Controls
the ADO.NET
Chapter architecture.
8 - Constraints All data
and Data providers use these classes. It defines classes that represent tables,
Relations
columns, rows,
Chapter 9
and DataSets. Some common classes from this namespace are DataView,
- ADO.NET Exception Handling
DataViewManager,DataSet,DataTable,DataRow,DataColumn, and DataRelation. To use
Chapter 10 - Working with the ODBC .NET Data Provider
these classes in your applications, you need to add a reference to the System.Data namespace.
Chapter 11 - Stored Procedures and Views
Chapter 12 All
Note - Oracle,
classesSQLXML, and Other
in the .NET Base .NET Data
Class Providers
Library are derived from the Object class and represent a
Chapter 13 type.
- Developing a Custom
Some authors Data
and Provider
documentation also use the word type for a .NET Framework class.
Chapter 14 - Developing Database Web Applications using ASP.NET
TheSystem.Data.Common
Chapter namespace
15 - Using ADO.NET in Xml defines common classes. These classes are base classes for
Web Services
concrete
Chapter 16data provider
- ASP.NET classes.
Server All data
Controls providers
and Data share these classes. DBConnection,DataAdapter,
Binding
DbDataAdaper,
Chapter DataColumnMapping,
17 - Building and DataTableMapping are some of the classes defined in
Real-World Web Applications
this namespace.
Chapter To use these
18 - Object-Relational classes
Mapping in in your application, you need to add a reference to the
.NET
System.Data.Common namespace in your application. Sql, OleDb, and Odbc.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability
TheSystem.Data.OleDb and ADO.NET
namespace defines classes to work with OLE-DB data sources using .NET
Chapter 21 - Messaging
OleDb data providers. To work with an OLE-DB data source, you must have an OleDb provider for that
Chapter 22 - SQL
data source. EachServer
dataand ADO.NET:
provider Notes on
component Performance
has a class corresponding to it. These classes start with
Appendix A - Relational Databases: Some Basic Concepts
OleDb, followed by the component. For example, the OleDbConnection class represents a
Appendix object. Some
B - Commonly
Connection of the
Used SQL common classes of this namespace are OleDbDataAdapter,
Statements
OleDbDataReader,
Appendix OleDbCommand,
C - ADO.NET Frequently OleDbCommandBuilder,OleDbError,OleDbParameter,
Asked Questions
OleDbPermission, and OleDbTransaction. To use these classes in your application, you need to
Index
addofaFigures
List reference to the System.Data.OleDb namespace in your application.
List of Tables
Similar to the System.Data.OleDb namespace, the System.Data.Odbc namespaces define Odbc
List of Listings
.NET data provider classes to work with the ODBC data sources. To work with ODBC data sources, you
List of Sidebars
need to install an ODBC driver for a database. The System.Data.Odbc namespace classes start with
Odbc, followed by the component. For example, the OdbcConnection class represents a Connection
object. Some of the common classes of this namespace are OdbcDataAdapter,OdbcDataReader,
OdbcCommand,OdbcCommandBuilder,OdbcError,OdbcParameter,OdbcPermission, and
OdbcTransaction. To use these classes in your application, you need to add a reference to the
System.Data.Odbc namespace in your application.

TheSystem.Data.SqlClient namespaces define Sql .NET data provider classes to work with SQL
Server 7.0 or later databases. Similar to Odbc and OleDb classes, the classes in this namespace start with
Sql, followed by the component. For example, common classes are SqlConnection,
SqlDataAdapter,SqlCommand,SqlDataReader, and SqlTransaction.
The last namespace, System.Data.SqlTypes, provides a group of classes representing the specific
Applied
types found in SQL ADO.NET:
Server. Some of Building Data-Driven
these classes Solutions
are SqlBinary, SqlMoney,SqlString,SqlInt32,
by Mahesh Chand and David
SqlDouble,SqlDateTime, and SqlNumeric. Talbot ISBN:1590590732
Apress 2003 (928 pages)
We discuss theseThis
namespaces, classes,
text provides andcoverage
extensive their members in more
of ADO.NET detail in Chapters 3 and 4.
technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Understanding
AppliedADO.NET Components
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
You can categorize ADO.NET
Apress components
2003 (928 pages) into three categories: disconnected, common or shared, and
the .NET data providers. The disconnected components build thetechnology
This text provides extensive coverage of ADO.NET basic ADO.NET architecture. You can
use these components (or classes) with or without data providers.
including ADO.NET internals, namespaces, classes, and For example, you can use a
DataTable object interfaces, and takes
with or without a comprehensive
data look or
providers. Shared at common
XML components are the base classes
for data providersnamespaces and classes,
and are shared and providers.
by all data how to integrate
The databothprovider
with components are specifically
ADO.NET.
designed to work with different kinds of data sources. For example, Odbc data providers work with ODBC
data sources, and OleDb data providers work with OLE-DB data sources.
Table of Contents
Figure 1-4 represents the ADO.NET components model and shows how the components work together.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - 1-4:
Figure Constraints and Data components
The ADO.NET Relations model
Chapter 9 - ADO.NET Exception Handling
Adata provider
Chapter is a set
10 - Working ofthe
with components,
ODBC .NET such
Data as Connection,Command,DataAdapter, and
Provider
DataReader.
Chapter TheProcedures
11 - Stored Connection is the first component that talks to a data source. The Connection
and Views
object establishes a connection to a data source and is used in Command and DataAdapter objects as a
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
connection reference. A Command object executes a SQL query and stored procedures to read, add,
Chapter 13 - Developing a Custom Data Provider
update, and delete data in a data source via a DataAdapter. The DataAdapter is a bridge between a
Chapter 14 - Developing Database Web Applications using ASP.NET
DataSet and the connection. It uses Command objects to execute SQL queries and stored procedures.
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter
All data16providers
- ASP.NET Server
share theControls
ADO.NET andcommon
Data Binding
components. These components represent the data.
Chapter 17 - Building Real-World Web Applications
Some of the common components are DataSet,DataView, and DataViewManager. The DataSet
uses XML
Chapter 18 -toObject-Relational
store and transfer data between
Mapping in .NET the applications and the data provider. A DataSet is a set of
DataTable
Chapter objects.
19 - Mapped A DataTable
Objects: represents
Performance a database
Considerations table.Binding
and Data The DataView and
DataViewManager
Chapter objects provide
20 - COM Interoperability single or multiple views of a DataSet. You can attach a DataView
and ADO.NET
or a DataViewManager
Chapter 21 - Messaging directly to data-bound controls such as a DataGrid or a DataList. Other
common
Chapter 22 components
- SQL Server are
andDataTable, DataRow,
ADO.NET: Notes DataColumn, and so on. Now let's break down the
on Performance
ADO.NET
Appendix A -model to see
Relational how it works.
Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
The Connection Object
Appendix C - ADO.NET Frequently Asked Questions
Index
The
List ofConnection
Figures object is the first component of ADO.NET that we should talk about. A connection sets
a link between a data source and ADO.NET. A Connection object sits between a data source and a
List of Tables
DataAdapter (via Command). You need to define a data provider and a data source when you create a
List of Listings
connection. With these two, you can also specify the user ID and password depending on the type of data
List of Sidebars
source.Figure 1-5 shows the relationship between a connection, a data source, and a DataAdapter.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Figure
Applied 1-5: The relationship
ADO.NETBuilding between
Data-Driven Connection,DataAdpater, and a data source
Solutions
Introduction
You can
Chapter 1 also connectBasics
- ADO.NET a Connection object to a Command object to execute SQL queries, which you can
use to retrieve,
Chapter 2 - Dataadd, update, and
Components delete
in Visual data .NET
Studio in a data source. Figure 1-6 shows the relationship between
theCommand and Connection objects.
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Figure 1-6: The relationship between the Command object and the Connection object
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
TheConnection
Chapter object
10 - Working alsoODBC
with the plays.NET
a useful
Data role in creating a transaction. Transactions are stored in
Provider
Transaction
Chapter objects,
11 - Stored and Transaction
Procedures and Views classes have all those nice features for dealing with
transactions
Chapter such as
12 - Oracle, commitand
SQLXML, andOther
rollback.
.NETFigure 1-7 shows the relationship between the Connection
Data Providers
object and
Chapter 13 - aDeveloping
transaction.
a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Figure 1-7: Creating a transaction from a Connection object
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Each data provider has a Connection class. The OleDbConnection,SqlConnection, and
Chapter 20 - COM Interoperability
OdbcConnection and ADO.NET
classes represent the Connection object of the OleDb, Sql, and Odbc data
Chapter 21 - Messaging
providers, respectively.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
NoteA ADO.NET
Appendix - Relationalconnections are discussed
Databases: Some in more detail in "The Connection: Connecting to a Data
Basic Concepts
Appendix B Source" section
- Commonly UsedofSQL
Chapter 4.
Statements
Appendix C - ADO.NET Frequently Asked Questions
The Command Object
Index
List of Figures
You
List of will usually use the Command object to execute SQL queries and stored procedures. You can execute
Tables
SQL
List of queries
Listings to return data in a DataSet or a DataReader object. To retrieve, add, update, and delete
data,
List you use SELECT,INSERT,UPDATE, and DELETE SQL queries. A DataAdapter generated using
of Sidebars
the VS .NET Integrated Development Environment (IDE) has these queries. Figure 1-8 shows the
relationship between a DataAdaper and a Command object.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table Figure 1-8: The relationship between DataAdapter and Command


of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Similar to the Connection class, each data provider has its own Command class. Each data provider has
Chapter 1 - ADO.NET
aConnection class.Basics
The OleDbCommand,SqlCommand, and OdbcCommand classes represent the
Chapter
Command2 -object
Data Components inSql,
of the OleDb, Visual Studio
and Odbc.NET
data providers, respectively.
Chapter 3 - ADO.NET in Disconnected Environments
You call
Chapter 4 the ExecuteReader
- ADO.NET method
in Connected of a Command object, which executes the query and returns data in
Environments
aDataReader
Chapter object
5 - Handling (see Figure
ADO.NET 1-9).
Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Figure
Chapter 10 - 1-9: Creating
Working a DataReader
with the from
ODBC .NET Data a Command object
Provider
Chapter 11 - Stored Procedures and Views
Note ADO.NET commands are discussed in more detail in "The Command: Executing SQL
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Statements" section of Chapter 4.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
The Command Builder
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
The SQL SELECT command is fairly easy to construct. Even if you don't know how to construct a SQL
Chapter
SELECT 17command,
- Building Real-World Web Applications
the Query Builder in VS helps you. But notice there are three other commands in
Chapter
Figure 18
1-8-toObject-Relational Mapping in .NET
construct: InsertCommand, UpdateCommand, and DeleteCommand. These commands
Chapter
can get19 - Mapped
quite Objects:
complicated in Performance
.NET becauseConsiderations
they requireand Data Binding
complex parameter objects and often involve
Chapter 20 of
large lists - COM Interoperability
columns. ADO.NETand ADO.NET
provides a nice utility known as the CommandBuilder that automatically
Chapter 21 - Messaging
builds these commands for you. Figure 1-10 describes the relationship between CommandBuilder and
Chapter Server and ADO.NET:isNotes
22 - SQL CommandBuilder
DataAdapter. constructed with DataAdapter and immediately generates the
on Performance
remaining
Appendix - Relationalobjects.
A Command Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List ofFigure
Tables 1-10: The relationship between DataAdapter and CommandBuilder
List of Listings
Note TheCommandBuilder is discussed in more detail in "Command-Builder: Easing the Work of
List of Sidebars
Programmers" section of Chapter 4.

The DataAdapter Object


TheDataAdapter object serves as a conduit between the data source and the DataSet. The
DataAdapter knows about the DataSet, and the DataAdapter knows how to populate the DataSet.
TheDataAdapter also knows about the connection to the data source. Figure 1-11 is a model that
shows the simple relationship between the DataAdapter, a DataSet, and a data source.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Figure 1-11:Apress
The relationship between DataAdapter and DataSet
2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
As you can see from Figureand
interfaces, 1-11, the aFill
takes method oflook
comprehensive a DataAdapter
at XML fills data from a DataAdapter to
theDataSet, andnamespaces
the UPDATE method
and makes
classes, DataSet
and how changes
to integrate both to the final data source.
with
ADO.NET.
There's a DataAdapter available for each data provider. OleDbDataAdapter,SqlDataAdapter,
OdbcDataAdapter represent the DataAdapter classes for OleDb, Sql, and Odbc data providers,
Table of Contents
respectively.
Applied ADO.NETBuilding Data-Driven Solutions
Note ADO.NET DataAdapters are discussed in more detail in "The DataAdapter: Adapting to Your
Introduction
Chapter 1 Environment" section of Chapter 4.
- ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
DataSet
Chapter 3Structure
- ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
ADataSet
Chapter object falls
5 - Handling into theEvents
ADO.NET disconnected components category. You can use it with or without data
providers.
Chapter 6 -The DataSet
Integrating consists
XML of a collection of tables, rows, columns, and relationships. Figure 1-12
with ADO.NET
illustrates
Chapter 7 the relationship
- Data between
Binding and a DataSet
Windows and its contents.
Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - 1-12:
Figure Relational
TheDatabases:
relationshipSome Basicclasses
between Concepts
in a DataSet
Appendix B - Commonly Used SQL Statements
As you can
Appendix see from Frequently
C - ADO.NET Figure 1-12, a DataSet
Asked is a collection of DataTables and DataRelations. All
Questions
DataTable objects of a DataSet are accessed through the Tables property, which represents a
Index
collection
List of DataTable objects. The Relations property of a DataSet represents the collection of
of Figures
DataRelation
List of Tables objects associated with a DataSet.
List of Listings
ADataTable is a collection of DataRow,DataColumn, and Constraint objects. The Rows,
List of Sidebars
Columns, and Constraints properties of a DataTable represent a collection of DataRow,
DataColumn, and Constraint objects. DataRow,DataColumn, and Constraint are the smallest
units of a DataSet, and they represent a table row, column, and constraint, respectively.

Note The DataSet object is discussed in more detail in the "The DataSet: The Heart of ADO.NET"
section of Chapter 3 and the "The DataSet in Connected Environments" section of Chapter 4.

DataSets in DataViews
Another thing you can do with the contents of DataSets is sort and filter them using DataViews. You can
have multiple views of a single DataSet. A DataView is a view of your data created according to certain
criteria. Each DataView has a one-to-one mapping to a DataTable in a DataSet. For example, say
you have three tables in a DataSet: table1, table2, and table3. Using three different data tables and data
Applied this
views, you can represent ADO.NET:
DataSet Building Data-Driven
in three Solutions
different views. Using sort and filters, you can even sort
by Mahesh Chand and David Talbot
and filter the data based on some criteria. Figure 1-13 shows three ISBN:1590590732
different views of a DataSet in the
Apressdata
form of three different 2003 (928 pages)
views.
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Figure 1-13: The relationship between DataSet and DataView objects
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
ADataView
Chapter can directly
6 - Integrating XMLattach
with to data-bound controls such as a DataGrid,DataList, or a combo
ADO.NET
box. We
Chapter 7 cover this
- Data data binding
Binding in Chapter
and Windows Forms2Data-Bound
and Chapter 7 in more detail.
Controls
Chapter 8 - Constraints and Data Relations
OK, enough theory. Let's see how to write your first database application and get some hands-on
Chapter 9 - ADO.NET Exception Handling
experience with ADO.NET.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Writing ADO.NET Applications
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
It's now time to write your
Apress first (928
2003 ADO.NET
pages) application. In the following sections, we discuss the necessary
steps required to This
writetext
a database application
provides extensive using ADO.NET.
coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Choosing a .NET Data Provider
namespaces and classes, and how to integrate both with
ADO.NET.
The first step of writing your first ADO.NET application is to decide what .NET data provider you'll use in
your application. As discussed previously, there are many data providers that work with multiple data
Table of Contents
sources. Some of the available data providers are OleDb, Sql, Odbc, and Oracle. You use the OleDb data
Applied ADO.NETBuilding
provider to access OLE-DB Data-Driven Solutions
data sources. The Sql data provider works with SQL Server 7.0 or later
databases. The Odbc data provider works with ODBC data sources, and the Oracle data provider works
Introduction
with Oracle
Chapter databases.
1 - ADO.NET You can also use more than one data provider to access the same database. For
Basics
example, you can use the
Chapter 2 - Data Components OleDb and Odbc
in Visual Studiodata
.NETproviders to work with Microsoft Access databases.
Chapter 3 - ADO.NET in Disconnected Environments
Note Some documentation and authors represent the Sql data provider as the SqlClient data provider.
Chapter 4 - ADO.NET in Connected Environments
This may be because the Sql data provider is defined in the SqlClient namespace.
Chapter 5 - Handling ADO.NET Events
Chapter
For this6example,
- Integrating
you'llXML
learnwith
howADO.NET
to build an application that uses OleDb data providers to access an
Chapter
Access 2000 database. In the "Using aForms
7 - Data Binding and Windows Data-Bound
DataSet to Read Controls
Data" section, you'll learn how to create an
Chapter 8 - that
application Constraints
uses Sqland Data
data Relations
providers to access a SQL Server 2000 database.
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Adding Namespace References
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle,
After you've chosenSQLXML, and
your data Other .NET
provider, you Data
needProviders
to add references to the assembly and include the
namespaces in your project. The System.Data.dll
Chapter 13 - Developing a Custom Data Provider assembly defines ADO.NET namespaces. You can
add references
Chapter to the project
14 - Developing using
Database Webthe Project using
Applications Add Reference
ASP.NET option. The .NET tab of the Add
Reference
Chapter 15 - dialog box lists all
Using ADO.NET the available
in Xml .NET assemblies (see Figure 1-14). Select the
Web Services
System.Data.dll
Chapter assembly.
16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 1-14: Adding a reference to the System.Data.dll assembly

After selecting the System.Data.dll assembly, click the Select button and then the OK button. This
action adds the System.Data assembly and related namespace references to your project.

After adding a reference to the assembly, you need to include namespaces in your project by using the
Imports directive. The Imports directive imports a namespace definition in a project. The following
code shows how to import the System,System.Data,System.Data.Common, and
System.Data.OleDb namespaces:
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Imports System
Apress 2003 (928 pages)
Imports System.Data
This text provides extensive coverage of ADO.NET technology
Imports System.Data.OleDb
including ADO.NET internals, namespaces, classes, and
Imports System.Data.Common
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Note If you are using the Sql data provider, you need to include the System.Data.Sql namespace.
Table of Contents
If you are using the Odbc data provider, you need to include the System.Data.Odbc
Applied ADO.NETBuilding Data-Driven Solutions
namespace.
Introduction
Chapter 1 - ADO.NET Basics
Establishing
Chapter a Connection
2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Now, the next step is to create a Connection object. You create a Connection object using the data
Chapter 4 Connection
provider's - ADO.NET in Connected Environments
class. In the following code, the OleDbConnection class creates a connection
Chapter 5 - Handling ADO.NET Events
with an Access 2000 database. The OleDbConnection constructor takes one parameter of a string type.
Chapter 6 -isIntegrating
This string XML with ADO.NET and it has two parts. First, it has a provider, and second, it has
called connectionString,
Chapter 7 - Data Binding and As
the path of the data source. Windows Forms
you can see, Data-Bound Controls
the following code uses the Microsoft.Jet.OLEDB.4.0
Chapter 8 and
provider - Constraints and Data Relations
the northwind.mdb data source:
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC
Dim connectionString .NET Data Provider
As String
Chapter
'Create11 -connection
Stored Procedures
andand Views
command objects
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
connectionString = "Provider=Microsoft.JET.OLEDB.4.0;" & _
Chapter
"data13source=C:\\Northwind.mdb"
- Developing a Custom Data Provider
Chapter 14 - Developing
Dim conn Database Web Applications
As OleDbConnection using ASP.NET
= New OleDbConnection(connectionString)
'Open15connection
Chapter - Using ADO.NET in Xml Web Services
conn.Open()
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Note In this sample, the northwind.mdb database path is "C:\\northwind.mdb". You can use
Chapter 21 - Messaging
any database you want. You just need to change the path and name of the database and
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
change the table names you are using in the SQL statements to access the data.
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Creating a Command or DataAdapter Object
Appendix C - ADO.NET Frequently Asked Questions
Index
Next, you need to create a DataAdapter or Command object. You create a Command object by using the
List of Figures
OleDbCommand class. (You'll see a DataAdapter object in the SQL Server example at the end of this
List of Tables
chapter; see the "Using a DataSet to Read Data" section.)
List of Listings
The
List ofOleDbCommand
Sidebars constructor takes two parameters. The first is a SQL query, and the second is the
Connection object. You can create a SELECT SQL query from the Customers table in the Northwind
database. The following code shows how to create a Command object:

Dim sql As String


sql = "SELECT CustomerID, ContactName, ContactTitle FROM Customers"
Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

Filling Data to a DataSet or DataReader Object


The next step is to open the connection by calling the Open method of the Connection object and
reading data from the Command object. The ExecuteReader method, OleDbCommand, returns data in
Applied
an OleDbDataReader ADO.NET:
object. Building Data-Driven
A DataReader Solutions
object is used to read fast and forward only cached data.
by Mahesh Chand
The following code shows this: and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
Dim reader Asincluding
OleDbDataReader = cmd.ExecuteReader()
ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Displaying Data
The of
Table next step is to perform some operation on the data. In this example, you'll write data out to the
Contents
console.
Applied The Read method
ADO.NETBuilding of OleDbDataReader
Data-Driven Solutions reads data. The DataReader class has a number of
methods that return different types of data (for example, GetString and GetInteger). These methods
Introduction
take the1 index
Chapter of the field
- ADO.NET Basicsfrom which you want to read data. The following code reads data from two fields
of the Customers
Chapter 2
table, whose indexes are 0 and 1:
- Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
While4 reader.Read()
Chapter - ADO.NET in Connected Environments
Console.Write(reader.GetString(0).ToString()
Chapter 5 - Handling ADO.NET Events + " ,")
Console.Write(reader.GetString(1).ToString()
Chapter 6 - Integrating XML with ADO.NET
+ " ,")
Console.WriteLine("")
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
End While
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Closing the Connection
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Finally, the last step is to close the reader and connection objects by calling their Close methods:
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
reader.Close()
Chapter 15 - Using ADO.NET in Xml Web Services
conn.Close()
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 You
Note - Mapped
must Objects: Performance
always close an openConsiderations
DataReader. and
WeData Binding
discuss DataReaders in more detail in the
Chapter 20 "The
- COMDataReader:
Interoperability
An and
EasyADO.NET
Walk Through the Data" section of Chapter 4.
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Creating a Applied
Simple ADO.NET
ADO.NET: Application
Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Now, let's use allApress
of the
previous
2003 (928steps
pages) and write a console-based application to read data and display it on
the console. In this application, you'll read data
This text provides extensive from the
coverage Northwind
of ADO.NET database's Customers table and display
technology
it on the console.including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces
In this example, you'll use VSand classes,
.NET and ahow
to create to integrate
console both with
application by selecting Visual Basic Projects and
ADO.NET.
then choosing the Console Application template from the Templates listing. Name your project
FirstAdoNetApp1.
Table of Contents
After creating the project, add the Listing 1-1 source code to the project. As you can see from this listing,
Applied ADO.NETBuilding
first you Data-Driven
import the required Solutions
namespace. Second, you create a connection and create an
Introduction
OleDbConnection object by passing the connection string as only one argument. Next, you construct a
Chapter
SELECT 1 statement
- ADO.NETand
Basics
create an OleDbCommand object by passing the SQL statement and connection
Chapter
object. 2Then
- Data
youComponents in Visual Studio
open the connection .NETthe OleDbConnection.Open method. Once a
by calling
connection
Chapter 3 - is opened,inyou
ADO.NET can execute
Disconnected SQL statement by simply calling the ExecuteXXX methods of the
Environments
DataReader.
Chapter As youincan
4 - ADO.NET see, theEnvironments
Connected code calls the ExecuteReader method, which returns data in an
OleDataReader
Chapter object.
5 - Handling ThenEvents
ADO.NET you read data from the DataReader and display it on the console. The last
step is 6to close
Chapter the DataReader
- Integrating and the connection. You do this by calling the Close methods of
XML with ADO.NET
OleDbDataReader
Chapter andand
7 - Data Binding OleDbConnection objects. Controls
Windows Forms Data-Bound
Chapter 8 - Constraints and Data Relations
Listing 1-1: Your ADO.NET Application
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Imports
Chapter 11 -System
Stored Procedures and Views
Imports
Chapter 12 -System.Data
Oracle, SQLXML, and Other .NET Data Providers
Imports System.Data.OleDb
Chapter 13 - Developing a Custom Data Provider
Imports System.Data.Common
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Module Module1
Chapter 16 - ASP.NET Server Controls and Data Binding
Sub Main()
Chapter 17 - Building Real-World Web Applications
Dim connectionString As String
ChapterDim18 -sql
Object-Relational
As StringMapping in .NET
Chapter'Create
19 - Mapped Objects: Performance
connection Considerations
and command objectsand Data Binding
Chapter 20 - COM Interoperability
ConnectionString and ADO.NET
= "Integrated Security=SSPI; Initial Catalog=Northwind; " & _
Chapter
"Data21Source=localhost;"
- Messaging
ChapterDim22 -conn
SQL Server and ADO.NET: Notes on
As OleDbConnection = Performance
New OleDbConnection(connectionString)
AppendixsqlA -=Relational
"SELECT Databases: Some Basic
CustomerID, Concepts
ContactName, ContactTitle FROM Customers"
Dim cmd As OleDbCommand
Appendix B - Commonly Used SQL Statements = New OleDbCommand(sql, conn)
'Open connection
Appendix C - ADO.NET Frequently Asked Questions
Index conn.Open()
'Call ExecuteReader
List of Figures
Dim reader As OleDbDataReader = cmd.ExecuteReader()
List of Tables
Console.WriteLine("Contact Name, Contact Title")
List of Listings
Console.WriteLine("=======================")
List of Sidebars
'Read data until reader has data
While reader.Read()
Console.Write(reader.GetString(0).ToString() + " ,")
Console.Write(reader.GetString(1).ToString() + " ,")
Console.WriteLine("")
End While
' Close reader and connection
reader.Close()
conn.Close()
End Sub
End Module
Applied ADO.NET: Building Data-Driven Solutions
Now compile andbyrun
Mahesh ChandYou
the project. and should
David Talbot
see data on the console (see Figure 1-15).
ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Figure 1-15: Displaying data on the console using DataReader
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Using a DataSet to ReadBuilding
Applied ADO.NET: Data Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
You just saw an example of working
Apress 2003 with an Access 2000 database. For variety, you'll now see an example
(928 pages)
of using a SQL Server database. Instead of writing
This text provides extensive coveragea console-based application, this time you'll create a
of ADO.NET technology
Windows Forms application and use the VS .NET IDE.
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
In this application,namespaces andSQL
you'll use the classes, andNorthwind
Server how to integrate bothYou'll
database. with read data from the Customers
ADO.NET.
table using the same CustomerId,ContactName, and ContactTitle columns you selected in Listing
1-1 and display data in a DataGrid control.
Table of Contents
Begin your first ADO.NET application by launching VS .NET and by creating a new project using File
Applied
New ADO.NETBuilding Data-Driven
Project. Choose Visual Solutionsand select the Windows Application template from the
Basic Projects
Introduction
available templates, name your project DataGridSamp, and click OK.
Chapter 1 - ADO.NET Basics
Clicking2 the
Chapter OK button
- Data creates
Components inaVisual
Windows
Studioapplication
.NET project. Now you read data from the SQL Server
Northwind database and display data in a DataGrid
Chapter 3 - ADO.NET in Disconnected Environments control. This example shows the Customers table
data in a DataGrid control.
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Drag a DataGrid control from the Toolbox onto the form using Toolbox Window Forms and then resize
Chapter
it. Also 6drag
- Integrating
a Button XML withonto
control ADO.NET
the form and assign it a Text property of Fill. Now double-click the
Chapter
button to write a click event handler forForms
7 - Data Binding and Windows Data-Bound
the button. Controls
Fill data from a database to the DataGrid on the Fill
Chapter 8 - Constraints
button click and Data Relations
event handler.
Chapter 9 - ADO.NET Exception Handling
To access
Chapter 10 -SQL Server,
Working withyou
the use
ODBCthe.NET
Sql Data
.NETProvider
data provider, which is defined in the
11 - Stored Procedures namespace.
System.Data.SqlClient
Chapter and Views Before adding this namespace class, you need to import the
namespace
Chapter using the
12 - Oracle, Imports
SQLXML, statement.
and Other Add the
.NET Data following two lines in the beginning of your application:
Providers
Chapter 13 - Developing a Custom Data Provider
Imports
Chapter 14 -System.Data
Developing Database Web Applications using ASP.NET
Imports
Chapter 15 -System.Data.SqlClient
Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
You implement your ADO.NET routine inside this method. To retrieve data from the database, you first
Chapter 18 - Object-Relational Mapping in .NET
need to connect to the database using a Connection object. The DataAdapter is the bridge between
Chapter
the data19source
- Mapped
(sql)Objects:
and thePerformance Considerations
DataSet (memory). and Data Bindingis constructed with two elements in
The DataAdapter
Chapter 20 - COM Interoperability and ADO.NET
this example: a SQL SELECT command to tell the DataAdapter which data to extract into the DataSet
Chapter
and the21 - Messaging object to tell the DataAdapter how to connect to the data source. In this example,
Connection
Chapter
select the -CustomerId,
22 SQL Server and ADO.NET: Notes
ContactName, andonContactTitle
Performance column data from the Customers table.
Appendix A - Relational Databases: Some Basic Concepts
After creating a DataAdapter, the next step is to create and fill data from a DataAdapter to a DataSet,
which you
Appendix B -do by making
Commonly a call
Used SQLtoStatements
the Fill method of DataAdapter. The Fill method takes two
parameters:
Appendix a DataSet
C - ADO.NET and a database
Frequently table name.
Asked Questions
Index
Finally, you'll want to display the data from the Customers table in the DataGrid control. You do this by
List of Figures
simply binding the DataSet to the DataGrid through its DefaultViewManager (Chapters 2 and 4
List of Tables
discussDataViewManager in more detail) by using the DataGrid'sDataSource property. You just set
List of Listings
theDataSource property of the DataSet'sDefaultViewManager.
List of Sidebars
Listing 1-2 shows the button click event handler. As you can see, this code creates a connection using the
localhost server and the Northwind database, and you create a DataAdapter using the Customers table.
After that, create a DataSet object and call the DataAdapter'sFill method. After that, bind
DataSet.DefaultViewManager to the DataGrid using the DataSource property of the DataGrid.

Listing 1-2: Viewing Data from a SQL Server Database to a DataGrid Control

Private Sub Button1_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles Button1.Click
' Create a Connection Object
Dim ConnectionString As String
Dim sql As String
Applied ADO.NET: Building Data-Driven Solutions
ConnectionString by Mahesh Chand and David
= "Integrated Talbot
Security=SSPI; ISBN:1590590732
Initial Catalog=Northwind; " & _
Apress
"Data Source=localhost;" 2003 (928 pages)
This text provides
Dim myConnection extensive coverage=of New
As SqlConnection ADO.NET technology
SqlConnection()
including ADO.NET internals, namespaces,
myConnection.ConnectionString = ConnectionString classes, and
interfaces, and takes a comprehensive look at XML
sql = "SELECT CustomerID,
namespaces ContactName,
and classes, and ContactTitle
how to integrate both with FROM Customers"
Dim adapter As SqlDataAdapter = New SqlDataAdapter(sql, myConnection)
ADO.NET.
' Construct the DataSet and fill it
Dim dtSet As DataSet = New DataSet("Customers")
Table ofadapter.Fill(dtSet,
Contents "Customers")
Applied'ADO.NETBuilding
Bind the Listbox Data-Driven
to the Solutions
DataSet
Introduction
DataGrid1.DataSource = dtSet.DefaultViewManager
Chapter 1 - ADO.NET Basics
Chapter
End Sub2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Now compile
Chapter and run the
6 - Integrating project.
XML Press the Fill button. The output of the program should look like Figure 1-
with ADO.NET
16.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Figure
Chapter 22 - 1-16: Output
SQL Server of ADO.NET:
and the ADO.NET
Notesapplication in a DataGrid control
on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Caution Make sure your SQL Server is running and the name of the SQL Server is correct. If you're not
Appendix C - ADO.NET Frequently Asked Questions
using a local server, you need to pass the correct server name, with valid a user ID and
Index password. We discuss using user IDs and passwords in a connection in Chapter 4. If SQL
List of Figures Server isn't running, you will get an exception.
List of Tables
List of Listings
List of Sidebars
Summary Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
This chapter introduced
Apress the basics
2003 of ADO.NET and its components. You learned why ADO.NET is a
(928 pages)
much better dataThis
access technology than previous
text provides extensive technologies.
coverage of ADO.NET ADO.NET
technology provides data providers such
as OleDb, Sql, andincluding ADO.NET internals, namespaces, classes, ODBC
Odbc to work with OLE-DB, SQL Server, and and data sources, respectively. After
that, you learned interfaces, and ADO.NET
about various takes a comprehensive
objects suchlook at XML
as Connection, Command,DataAdapter,
namespaces
DataReader,DataSet, andand classes, and
DataView howas
as well to the
integrate both with
relationships between them. For example,
ADO.NET.
DataSet is a collection of DataTable objects. A DataTable can attach to a DataView, which can later
be bound with data-bound controls. Further, using a DataView's filter and sort properties, you can provide
different
Table views of a DataSet and a DataView.
of Contents
Applied ADO.NETBuilding Data-Driven Solutions
At the end of this chapter, you learned how to write a simple application to read data from Microsoft
Introduction
Access and SQL Server databases using ADO.NET.
Chapter 1 - ADO.NET Basics
Chapter
Chapter2 2 -isData Components
heavily based oninthe
Visual StudioIDE.
VS .NET .NETIt covers how to take advantage of VS .NET IDE wizards
Chapter 3 - ADO.NET
and utilities to developindatabase
Disconnected Environments
applications without writing much code. You'll probably find this chapter
exciting4because
Chapter you'll
- ADO.NET in learn to build
Connected fully functioning database applications by simply writing a couple of
Environments
lines of5code.
Chapter - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Chapter Applied
2: Data Components inISBN:1590590732 Visual Studio .NET
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot
Apress you
In the previous chapter, 2003 (928the
saw pages)
basics of the ADO.NET model and its components and how they are
This text provides
connected to each other in the ADO.NET extensive coverage ofThe
architecture. ADO.NET
Visualtechnology
Studio .NET (VS .NET) Integrated
including ADO.NET internals, namespaces, classes, and
Development Environment (IDE) provides design-time support to work with data components. In this
interfaces, and takes a comprehensive look at XML
chapter, you'll learn how to use
namespaces andthese dataand
classes, components in the both
how to integrate VS .NET
with IDE at design-time to create
database applications. Using ADO.NET components (also referred to as data components) is similar to
ADO.NET.
using any Windows control. You just drag the component from the Toolbox to a form, set its properties and
methods, and you're up and running.
Table of Contents
We start
Applied this chapter withData-Driven
ADO.NETBuilding the Server Explorer,
Solutions a useful tool for creating database applications. We focus
on developing database applications quickly using data components in VS .NET without writing a lot of
Introduction
code. We
Chapter 1 -also provide
ADO.NET a step-by-step tutorial to help you develop and run a project. After that, we discuss
Basics
theConnection,
Chapter DataAdapter,
2 - Data Components Command,
in Visual StudioDataSet,
.NET and DataView components in more detail. After
finishing
Chapter 3 this chapter,in
- ADO.NET you'll have a good
Disconnected understanding of data components and how to work with them in
Environments
VS .NET.
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Creating
Chapter 6 an ADO.NET
- Integrating Project
XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Begin your project by launching the VS .NET IDE and choosing New Project from the Project menu.
Chapter 8 - Constraints and Data Relations
Choose Visual Basic Projects from Project Types and then pick the Windows Application template. If you
Chapter 9 - ADO.NET Exception Handling
like, type an appropriate name into the Name field for your first ADO.NET application and click OK. Our
Chapter 10 - Working with the ODBC .NET Data Provider
project name is MyFirstADONetApp.
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Using the Server Explorer
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
The Server Explorer is new
Apress to (928
2003 VS pages)
.NET; it enables you to manage your database servers and connections.
You can use the Server Explorer to add a new
This text provides extensive server of
coverage or ADO.NET
a data connection
technologyto your list. The Server Explorer
allows you to view, add, edit, and delete data in a tabular grid. The
including ADO.NET internals, namespaces, classes, and Server Explorer also allows you to
create and deleteinterfaces,
databaseand takessuch
objects a comprehensive look procedures,
as tables, stored at XML views, and triggers.
namespaces and classes, and how to integrate both with
Note If you'veADO.NET.
ever used Open Database Connectivity (ODBC) data source names (DSNs) in your
applications, then you might be familiar with the traditional Windows ODBC Administration. With
ODBC Administration, you created DSNs using ODBC drivers for a data source and then
Table of Contents
connected your application using this DSN. Now, you'll hardly use ODBC Administration at all
Applied ADO.NETBuilding Data-Driven Solutions
because the Server Explorer provides connection management services.
Introduction
Chapter 1 see
OK, let's - ADO.NET Basics
how to use the Server Explorer. You can open the Server Explorer by clicking the View
Chapter
Server 2Explorer
- Data menu
Components
item. in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Tip4The
Chapter use of the
- ADO.NET in Server Explorer
Connected isn't limited to SQL Server databases. You can add any type of
Environments
Chapter 5database, including
- Handling ADO.NET MySQL
Eventsand Access.
Chapter 6
When you-open
Integrating XML with ADO.NET
the Server Explorer, you'll see two root nodes: Data Connections and Servers. Initially, the
Chapter 7 - Data Binding
Data Connections and no
node has Windows
items Forms Data-Bound
listed under Controls
it, but the Servers node displays all the available
Chapter 8 By
servers. - Constraints andServers
expanding the Data Relations
node, you can see all the servers and services running on your
Chapter 9 The
machine. - ADO.NET Exception
SQL Servers Handling
node of the Server Explorer lists all the available SQL Servers running on that
Chapter 10 - Working with the ODBC .NET Data Provider
machine.
Chapter 11 - Stored Procedures and Views
If you don't
Chapter 12 - have any
Oracle, serversand
SQLXML, listed in your
Other .NETServers list, you can right-click the Servers node and select the
Data Providers
Add Server
Chapter menu option.
13 - Developing This option
a Custom Data allows you to add new servers to the list. You can also unregister a
Provider
server by right-clicking it and selecting the "Unregister Server" menu option.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter
You can15also
- Using ADO.NET
manage in Xml
services Web Services
running on your server. If you expand the Services node, you'll see a list of
Chapter
services running on your system. Byand
16 - ASP.NET Server Controls Data Binding
right-clicking a service, you can even start and stop a service using
Chapter 17 -Explorer.
the Server Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
To view19and
Chapter managed
- Mapped database
Objects: objects such
Performance as tables,and
Considerations views,
DataorBinding
stored procedures, you can simply
expand20
Chapter the- database server name
COM Interoperability andlisted under the Data Connections node. After that you can expand
ADO.NET
Tables,21
Chapter Views, and Stored Procedures nodes. To view, delete, and update a database table, you right-
- Messaging
click the table and use the available options.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Adding a New Connection
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Adding a new connection is the next step after adding a server (if you're using a server) to the Server
Index
Explorer. You add a new connection to your list by right-clicking the Data Connections node and choosing
List of Figures
the Add Connection option. This brings up a Data Link Properties Wizard. By default, the Connection tab is
List of Tables
active, but you may want to click the Provider tab. The first tab of this wizard, Provider, displays all the data
List of Listings
source providers installed on your machine; this is where you select your database provider. The list could
List of Sidebars
contain any OLE-DB provider, Jet OLE-DB, or other data driver available on your computer.

The second tab of this wizard, Connection, lets you pick your server and corresponding data source. The
drop-down list displays all the available servers. Our server is a SQL Server with the default name
localhost. After selecting a server, the Database drop-down list displays all the available databases on the
server. Select the Northwind database in this example. You can also pick what security mode you want to
use for your connection on this tab. You can use the user ID and password of a SQL Server or use the
Windows NT Integrated security option. By clicking the Test Connection button, you can make sure your
database connection is working. If you've provided an incorrect user ID or password, the test will throw an
error.

The third tab, Advanced, is for setting connection timeout and access permissions. You can give this
connection read, write, or other permissions using the Advanced tab. The last tab, All, shows you all
properties of the connection in a list, such as server name, connection timeout, data source, initial catalog
(database name),Applied ADO.NET: Building Data-Driven Solutions
security, and other network options. You can also edit this data from this tab by just
by Mahesh Chand and
double-clicking a particular property David
and Talbot
editing the value of it.ISBN:1590590732
Apress 2003 (928 pages)
After adding a data connection,
This you'll
text provides see that
extensive the wizard
coverage adds a node
of ADO.NET technology
including ADO.NET internals,
(Servername.DatabaseName.dbo) to thenamespaces, classes,root
Data Connections andnode. If you expand this node, you
interfaces,
can see the database and
objects takes
such asatables,
comprehensive look at
views, stored XML
procedures, and functions.
namespaces and classes, and how to integrate both with
ADO.NET.
Managing and Viewing Data
Table of Contents
As mentioned earlier, the Server Explorer not only lets you add server and database connections, but it
Applied ADO.NETBuilding
also lets you manage andData-Driven
view data. Solutions
You can add, update, and delete data from a database. Of course,
Introduction
you must have read and write permissions on the server to read and write data. The Server Explorer also
Chapter 1 options
provides - ADO.NET Basics new databases and objects, including tables, views, stored procedures, and
for creating
Chapter 2 - can
so on. You Datado
Components in Visual
this from both Studio (Data
root nodes .NET Connections or Servers).
Chapter 3 - ADO.NET in Disconnected Environments
The Server
Chapter Explorer shows
4 - ADO.NET database
in Connected objects in a tree structure. Each database is a tree node of the
Environments
server. As -you
Chapter 5
expand the Northwind database node, for example, you can see its children listed as tables,
Handling ADO.NET Events
stored procedures, and views.
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data
If you expand thisBinding and Windows
connection Forms Data-Bound
by double-clicking it, you'll Controls
notice it shows tables, views, and stored
Chapter 8 - Constraints
procedures. and Data
You can further Relations
expand these items to see them in more detail.
Chapter 9 - ADO.NET Exception Handling
Note
Chapter 10 We discuss
- Working stored
with procedures
the ODBC and Provider
.NET Data views in more detail in Chapter 11.
Chapter 11 - Stored Procedures and Views
Besides showing a list of database objects such as tables, views, stored procedures, and functions, the
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Server Explorer also lets you retrieve, add, edit, and delete data from a data source. You can always
Chapter 13 - Developing a Custom Data Provider
double-click to view the data of a table. Alternatively, right-clicking a database object (such as a table)
Chapter 14you
provides - Developing Database
menu options basedWeb
on Applications using
the selection. ForASP.NET
example, you can right-click and then select the
Chapter 15 - Using ADO.NET in Xml Web Services
Retrieve Data from Table menu option to retrieve data from that table.
Chapter 16 - ASP.NET Server Controls and Data Binding
Now, if 17
Chapter you- Building
retrieve data from the
Real-World WebEmployees table by double-clicking or using the Retrieve Data from
Applications
Table option,
Chapter data will be displayed
18 - Object-Relational Mappingininan editable grid. You can edit this data at any time-again, assuming
.NET
you have
Chapter 19 the correctObjects:
- Mapped permissions. For example,
Performance to delete
Considerations a row
and DataorBinding
a collection of rows, select the rows and
hit Delete
Chapter 20 or right-click
- COM the selected
Interoperability and rows and then hit the Delete option. The right-click option of the grid
ADO.NET
also provides you options to move to the grid's first, next, previous, and last records.
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Using Visual Data
Applied Components
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
At this time, ADO.NET
Apresshas three
2003 (928base
pages)data providers and multiple add-on data providers to work with
various kinds of data sources, including SQLcoverage
This text provides extensive Server, OLEDB datatechnology
of ADO.NET sources, and ODBC data sources. What
do we mean by base including ADO.NET internals, namespaces, classes, and only three data providers; now, based
data providers? Well, initially Microsoft released
interfaces, and
on additional requirements, takes aand
Microsoft comprehensive
third parties look at XML new data providers to the library. For
are adding
example, Microsoft,namespaces andCore
Oracle, and classes,
Laband how to Development
Software integrate both provide
with three different data providers for
ADO.NET.
the Oracle database. (We discuss these new data providers in later chapters.) The main class hierarchy
model of these data providers remains the same, so programmers will have no problem switching between
dataof
Table providers.
ContentsSome of these data providers are OleDb, Sql, Odbc, and Oracle. In this chapter, we
concentrate on the OleDbData-Driven
Applied ADO.NETBuilding and Sql data providers. Later chapters discuss the ODBC, Oracle, and other data
Solutions
providers.
Introduction
Chapter 1 - ADO.NET Basics
Tip You can even write your own custom data provider. ADO.NET provides a set of interfaces, which
Chapter 2 - Data Components in Visual Studio .NET
you must implement to write your own data provider. Chapter 13 of this book covers writing custom
Chapter 3data
- ADO.NET in Disconnected Environments
providers.
Chapter 4 - ADO.NET in Connected Environments
You can
Chapter 5 use the dataADO.NET
- Handling providerEvents
components at design-time in VS .NET. The data provider components are
available
Chapter 6 at design-timeXML
- Integrating via with
the Toolbox.
ADO.NETThe Toolbox's Data tab shows you the available data controls in
VS .NET.
Chapter 7 Some
- Data of the components
Binding you
and Windows mayData-Bound
Forms see are DataSet,
ControlsDataView, SqlConnection,
SqlCommand,
Chapter SqlDataAdapter,
8 - Constraints OleDbConnection,OleDbCommand,OleDbDataAdapter,
and Data Relations
OdbcCommand,
Chapter 9
OdbcCommandBuilder,OdbcConnection, and OdbcDataAdapter. These
- ADO.NET Exception Handling
components are available only when you're in a design view.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter
Tip11 - Stored
You Procedures
can open Toolboxand
by Views
using the View Toolbox menu item.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
As mentioned
Chapter briefly inaChapter
13 - Developing Custom 1, theProvider
Data .NET Framework Library contains many ADO.NET data providers,
including
Chapter 14 OleDb, Sql, and
- Developing Odbc.Web
Database The Applications
OleDb datausing
provider wraps up the native OLE-DB COM Application
ASP.NET
Programming
Chapter Interface
15 - Using (API)
ADO.NET for working
in Xml with OLE-DB data sources. To access an OLE-DB data source,
Web Services
you need
Chapter 16 to install anServer
- ASP.NET OLE-DB data and
Controls provider
Data for that database. Sql data providers work with SQL Server 7
Binding
or later17
Chapter databases.
- BuildingOdbc data providers
Real-World wrap up the ODBC API to work with ODBC data sources (with the
Web Applications
help of the ODBC Administration and ODBC drivers). Chapter 4 discusses these data providers in more
Chapter 18 - Object-Relational Mapping in .NET
detail. Microsoft and other vendors might introduce more data providers, which you can add to the library
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
later.
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
In the .NET Framework, each of these data providers has its own namespaces. For instance, the
Chapter 22 - SQL Server and
System.Data.OleDb ADO.NET:consists
namespace Notes onofPerformance
classes belonging to the OleDb data providers. All of these
namespace
Appendix classes start
A - Relational with OleDb.
Databases: Some The
BasicMicrosoft.Data.ODBC
Concepts and System.Data.SqlClient
namespaces
Appendix consist ofUsed
B - Commonly classes
SQL belonging
Statementsto the Odbc and Sql data providers, respectively. Similar to
OleDb, classes
Appendix in Odbc
C - ADO.NET start with
Frequently Odbc,
Asked and classes in SqlClient start with Sql.
Questions
Index
In VS .NET, some of these classes (or objects) are available from the Toolbox; you can drag them onto a
List of Figures
form like any other Windows control in the Toolbox. These controls are called data components.
List of Tables
List
VSof.NET
Listings
also provides a set of data-bound controls. The DataGrid,ListBox, and DataList are good
List of Sidebars
examples of some of these data-bound controls. It's fairly easy to work with these controls. You just set a
few properties, and they're ready to display your data. For example, setting a DataGrid control's
DataSource property displays data from a DataSet object. We use these data-bound controls in the
examples throughout this book. In the next section, we discuss how you can add these components to your
Windows Forms applications and set their properties and methods at design-time with the help of the .NET
wizards.

Understanding Data Connections


To connect to a data source, the first thing you need to learn about is a data connection.

Each data provider has a Connection class, and if you're using the VS .NET IDE, you can see these class
objects as components in the Data tab of the Toolbox. For example, the SqlConnection,
OdbcConnection, Applied ADO.NET: Building Data-Driven
and OleDbConnection class objectsSolutions
represent a connection for the Sql, Odbc, and
by Mahesh Chand and
OleDb data providers, respectively: David Talbot ISBN:1590590732
Apress 2003 (928 pages)
SqlConnection creates
This text andextensive
provides managescoverage
SQL Server database
of ADO.NET connections.
technology
including ADO.NET internals, namespaces, classes, and
creates
interfaces,
OdbcConnection and manages
and takes connections
a comprehensive look attoXML
ODBC data sources.
namespaces and classes, and how to integrate both with
ADO.NET.creates and manages connections to OLE-DB data sources.
OleDbConnection

In the VS .NET IDE, you can create a Connection in many ways. You can use the IDE to add a
Table of Contents
Connection object to a project, create it programmatically, or use DataAdapters that automatically
Applied
create ADO.NETBuilding Data-Driven
a Connection object for you.Solutions
In this chapter, we concentrate on adding a connection through the
Introduction
VS .NET IDE.
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual StudioIDE
.NETvs. Manual Coding
Chapter 3 - ADO.NET in Disconnected Environments
In some
Chapter 4
cases, you may not know if you should use the IDE or write code manually. Actually, creating a
- ADO.NET in Connected Environments
connection and other objects such as a Command manually is usually much better. Those types of
Chapter 5 - Handling ADO.NET Events
objects only require a few lines of code, and creating them manually allows you to know where the
Chapter 6 - Integrating XML with ADO.NET
code is and what it means. If you use the VS .NET IDE, it adds a connection and other data
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
components but sometimes also writes lots of code that is difficult to understand. For example, adding
Chapter 8 - Constraints
aDataAdapter and aData
adds Relations
great deal of code that you may not need. If you want to modify it, you'll almost
Chapter 9 - ADO.NET Exception Handling
certainly get lost if you don't have a lot of experience.
Chapter 10 - Working with the ODBC .NET Data Provider
Now,
Chapter 11 your nextProcedures
- Stored question probably is, "Why do I need to use the IDE at all?" Well, the IDE may help you
and Views
if you
Chapter 12 don't know
- Oracle, how to and
SQLXML, create connection
Other .NET Datastrings and other objects because it writes code for you.
Providers
Basically,
Chapter the IDE isa useful
13 - Developing Customfor part-time
Data ADO.NET developers-those who write non-database-related
Provider
code
Chapter 14 but occasionally
- Developing use ADO.NET.
Database It's alsousing
Web Applications useful for beginners, who want to have their application
ASP.NET
ready
Chapter 15 -and running
Using in nointime.
ADO.NET But after
Xml Web completing this book, I bet you'll hate using IDE.
Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational
The easiest Mapping to
way to add a connection in .NET
a project in VS .NET is to drag a connection component
Chapter 19 - Mapped OleDbConnection,
(SqlConnection, Objects: Performance Considerations and Data from
or OdbcConnection) Binding
the Toolbox's Data tab to a form.
Chapter 20 -you
After that, COM Interoperability
can and ADO.NET
set the connection's properties using the Properties window. For this demonstration, drop
aSqlConnection
Chapter from the Toolbox onto the form. You can set the SqlConnection properties from the
21 - Messaging
Properties
Chapter 22 -window by right-clicking
SQL Server and ADO.NET: theNotes
SqlConnection and selecting the Properties menu item.
on Performance
Appendix A - Relational Databases: Some Basic Concepts
Note that the default connection name is the class name with a unique name appended to it. Because this
Appendix B - Commonly Used SQL Statements
is the first connection, the name is sqlConnection1.
Appendix C - ADO.NET Frequently Asked Questions
Index
As you can see in the Properties window, a connection's properties include Database,
List of Figures
ConnectionTimeout, DataSource,PacketSize,WorkstationId,Name, and
List of Tables
ConnectionString.
List of Listings
List ofNote The connection properties depend on the data provider. Some properties may not be available for
Sidebars
some data providers. For example, the WorkstationId property is available in Sql data
providers but not in OleDb or ODBC data providers.

Understanding Connection Strings


TheConnectionString property is the main property of a connection. By clicking the drop-down list of
theConnectionString property, you can see all the available data connections. If you don't have a data
connection, you can use its New Connection option, which launches the Data Link Properties Wizard.
(Refer to the earlier "Using the Server Explorer" section for more information.)

For this example, we use the database server name MCB instead of localhost. The SQLConnection string
for server MCB looks like following:
Applied ADO.NET: Building Data-Driven Solutions
data source=MCB;initial catalog=Northwind;integrated security=SSPI;" & _
by Mahesh Chand and David Talbot ISBN:1590590732
"persist security info=False;workstation id=MCB;packet size=4096
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Note In Chapter 4, we discuss
namespaces a connection
and classes, and howand
to its properties
integrate bothinwith
more detail and show how to set them
programmatically.
ADO.NET. You can also use a user ID and password in a connection string.

Working with SQL DataAdapters


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
ADataAdapter is another important component of a data provider. Similar to a connection, each data
Introduction
provider has a corresponding DataAdapter class. All DataAdapters in ADO.NET work in same way,
Chapter 1 - ADO.NET
which means Basics
if you know how to work with Sql DataAdapters, you can use OleDb, ODBC, and other
Chapter 2 - Data Components
DataAdapters in Visual Studio .NETOleDbDataAdapter, and OdbcDataAdaper classes
easily. The SqlDataAdapter,
Chapter 3 DataAdapter
represent - ADO.NET in Disconnected
components Environments
in Sql, OleDb, and Odbc data providers, respectively.
Chapter 4 - ADO.NET in Connected Environments
Besides5 creating
Chapter a DataAdapter
- Handling programmatically (see Chapter 4 for more details), VS .NET provides
ADO.NET Events
various6ways
Chapter to create DataAdapters.
- Integrating XML with ADO.NETTwo common ways are by using the Server Explorer and by using
the Data
Chapter 7 Adapter Configuration
- Data Binding Wizard.
and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Creating- ADO.NET
DataAdapters with the Server Explorer
Chapter 9 Exception Handling
Chapter
It's easy10to-create
Working with the ODBC .NET
a DataAdapter Data
using theProvider
Server Explorer. You just drag and drop database objects to a
Chapter 11 - Stored Procedures and Views
form and the IDE takes care of everything for you. The IDE writes code that you can use programmatically
Chapter
or bind12 - Oracle,
data SQLXML,
controls and Other
at design-time. To.NET
addData Providers
a new connection to a project, expand your database in the
Chapter
Server 13 - Developing
Explorer, a Custom
and drag a tableData
fromProvider
the Server Explorer to your form. For this example, expand the
Northwind
Chapter 14 -database in Database
Developing the ServerWeb
Explorer and drop
Applications theASP.NET
using Employees table to the form.
Chapter 15 - Using ADO.NET in Xml Web Services
This action adds a SqlConnection and SqlDataAdapter object to the project. You can even drag
Chapter 16 - ASP.NET Server Controls and Data Binding
selected columns or stored procedures onto the form. The VS .NET IDE takes care of the rest. Right-click
Chapter 17 - Building Real-World Web Applications
the form and choose View Code to examine the code generated by the wizard; in this example, you'll see
Chapter 18 - Object-Relational Mapping in .NET
oneSqlConnection component and one SqlDataAdapter component along with a set of
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
SqlCommand components:
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Friend WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
Appendix A - Relational Databases: Some Basic Concepts
Friend WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
Appendix B - Commonly Used SQL Statements
Friend WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand
Appendix C - ADO.NET
Friend Frequently
WithEvents Asked Questions As System.Data.SqlClient.SqlConnection
SqlConnection1
Index
Friend WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
List of Figures
List of Tables
You'll
List also see that the IDE writes SQL statements and sets the CommandText property of SqlCommand
of Listings
objects.
List Once you have a DataAdapter, you can use it to populate DataSets and work with its
of Sidebars
properties. (We discuss DataSet basics and how to construct them manually in Chapter 4.) With the VS
.NET IDE, you can even generate DataSets using the visual representation of the DataAdapter. (We
discuss how to populate a DataSet using VS .NET IDE wizards in the "Generating Typed DataSets Using
a DataAdapter" section of this chapter.)

Creating DataAdapters with the Data Adapter Configuration Wizard


The Data Adapter Configuration Wizard is a powerful tool to develop database applications. To see how
you can create DataAdapters using this wizard, you'll create a new Windows Forms-based project in the
following step-by-step sections.

In this first project, you'll learn how to create SQL DataAdapters, read data from a SQL Server data
source, and display the data from a DataAdapter to a DataGrid control. After completing these steps,
Applied ADO.NET: Building Data-Driven Solutions
you'll see how easy it is to develop database applications using the Data Adapter Configuration Wizard.
by Mahesh Chand and David Talbot ISBN:1590590732
Apress
Step 1: Selecting a Project
2003Template
(928 pages)
This text provides extensive coverage of ADO.NET technology
To create a new Windows application
including ADO.NET using VS
internals, .NET, select
namespaces, File and
classes, New Project Visual Basic Projects
Windows Application.
interfaces, andselect
Next, takes an
a comprehensive look at XML
appropriate directory and type your project name.
namespaces and classes, and how to integrate both with
Step 2: Adding aADO.NET.
DataGrid Control to the Form

Now add a DataGrid control to the form. You do this by just dragging a DataGrid control from the
Table of Contents
Toolbox's Windows Forms category to the form and resizing it.
Applied ADO.NETBuilding Data-Driven Solutions
Step 3: Adding a DataAdapter Component
Introduction
Chapter 1 - ADO.NET Basics
Next, drag a SqlDataAdapter control from the Toolbox's Data category to the form. As you drop the
Chapter 2 - Data Components in Visual Studio .NET
SqlDataAdapter the Data Adapter Configuration Wizard pops up. The following sections walk you
Chapter 3 - ADO.NET in Disconnected Environments
through this wizard.
Chapter 4 - ADO.NET in Connected Environments
Chapter
Welcome5 -Page
Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
The first
Chapter 7 page of Binding
- Data this wizard
and is just a welcome
Windows screen. Click
Forms Data-Bound the Next button to move to the next screen.
Controls
Chapter 8 - Constraints and Data Relations
Choose Your Data Connection Page
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working
The second page of with the ODBC
the wizard lets.NET
you Data Provider
create a new connection or pick from a list of available connections
on your11
Chapter machine.
- StoredAll availableand
Procedures connections
Views are listed in the drop-down list. If you don't have any
connections
Chapter listed inSQLXML,
12 - Oracle, the list, and
you Other
can click
.NETthe New
Data Connection button, which launches the Data Link
Providers
Properties
Chapter 13 -Wizard (discussed
Developing previously
a Custom in the "Adding a New Connection" section).
Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Choose a Query Type
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16page
The next - ASP.NET
of theServer
wizardControls and Data
is for selecting a Binding
SQL query type. If you see this page, three options are
Chapter 17 - Building Real-World Web
available for the SQL Server connection:Applications
Chapter 18 - Object-Relational Mapping in .NET
The
Chapter 19Use SQL Statement
- Mapped option allows
Objects: Performance you to createand
Considerations a new
DataSQL query.
Binding
Chapter 20 - COM Interoperability and ADO.NET
The Create New Stored Procedure option allows you to create a new stored procedure.
Chapter 21 - Messaging
Chapter
The22Use
- SQL ServerStored
Existing and ADO.NET: Notes
Procedure on Performance
option allows you to select a stored procedure from the existing
stored
Appendix A - procedures.
Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Note The last two options aren't available for databases that don't support stored procedures.
Appendix C - ADO.NET Frequently Asked Questions
Index
Generate the SQL Statement
List of Figures
The
List of next page of the Data Adapter Configuration Wizard lets you build a SQL statement or a stored
Tables
procedure.
List of ListingsYou can type a SQL statement directly into the text box, or you can click the Query Builder
button
List to use the Query Builder to build your SQL statement.
of Sidebars

Query Builder

The Query Builder option lets you pick tables from your data source. First, select the Employees table to
read in the Employee data. You actually have the option of selecting as many as tables you want, but for
now select only one table and click the Add button. After clicking the Add button, the Query Builder provides
you with an option to select columns of the table. You can check *(All Columns) to select all the columns of
a table, or you can check only the columns you want to participate in the SQL statement.

If you've ever used the New Query Wizard in Microsoft Access, you'll find that the Query Builder is similar to
it. In Access, you can create queries by dragging tables and their columns to the grid (or checking the
columns), and the Query Builder builds a SQL query for your action.
In this sample, select EmployeeID,FirstName, and LastName from the Employees table to build your
SQL statements. Applied
The SQLADO.NET:
statementBuilding Data-Driven Solutions
after selecting these three columns looks like this:
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
SELECT EmployeeID, LastName, FirstName FROM Employees
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
You can even type this SQL statement
namespaces directly
and classes, intotothe
and how text box.both with
integrate
ADO.NET.
Note You can even write your own SQL statement if you don't want to use the Query Builder. For
performance reasons, if you only want few columns, then use column names instead of using
Table of Contents
SELECT * statements.
Applied ADO.NETBuilding Data-Driven Solutions
One more button is available on this page: Advanced Options. By clicking the Advanced Options button, you
Introduction
launch 1the- Advanced
Chapter SQL Generation Options page. On this page, there are three options: Generate Insert,
ADO.NET Basics
Update,
Chapter 2 and Delete
- Data Statements;
Components Use Optimistic
in Visual Concurrency; and Refresh the DataSet. By default all three
Studio .NET
options are checked. If you don't want to generate INSERT,UPDATE, or DELETE SQL statements, you
Chapter 3 - ADO.NET in Disconnected Environments
should uncheck the first check box.
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - ADO.NET
By default, Handling ADO.NET Events concurrency. If you don't want your program to implement optimistic
uses optimistic
Chapter 6 - Integrating XML with
concurrency, you should uncheck ADO.NET
the second check box.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
View Wizard
Chapter Results and Data Relations
8 - Constraints
Chapter 9 - ADO.NET Exception Handling
The View Wizard Results page shows you the action being taken by the wizard. If everything went well, the
Chapter 10 - Working with the ODBC .NET Data Provider
wizard generates a message saying, "The data adapter was configured successfully." For this example,
Chapter 11 - section
the Details Stored Procedures
shows thatand
the Views
wizard has generated SQL SELECT,INSERT,UPDATE, and DELETE
Chapter 12 - Oracle, SQLXML,
statements and mappings. and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Now you
Chapter 14can click the Finish
- Developing button
Database WebtoApplications
complete the process.
using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Currently, if you examine the design surface at the bottom of the Form window, you'll see two components:
Chapter 16 - ASP.NET Server Controls and Data Binding
sqlConnection1 and sqlDataAdapter1. The wizard sets the properties of these components for you.
Chapter 17 - Building Real-World Web Applications
Now you can use the DataAdapter to populate your DataSets. Don't forget to resize the DataGrid you
Chapter 18 - Object-Relational Mapping in .NET
added to the project.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter
Step 4:20 - COM and
Setting Interoperability
Reviewingand ADO.NET Properties
DataAdapter
Chapter 21 - Messaging
Now that you have a DataAdapter on your form, let's look at the SqlDataAdapter component
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
properties. You can see its properties by right-clicking the adapter and selecting the Properties menu item.
Appendix A - Relational Databases: Some Basic Concepts
The wizard also shows the available command properties, including InsertCommand,DeleteCommand,
Appendix B - Commonly Used SQL Statements
SelectCommand, and UpdateCommand.
Appendix C - ADO.NET Frequently Asked Questions
Index
You can set DataAdapter properties by clicking these properties. . A DataAdapter has four
List of Figures properties-SelectCommand, DeleteCommand, InsertCommand, and UpdateCommand-
SqlCommand
thatofall
List execute SQL commands on the data source. For example, if you look at the SelectCommand
Tables
property,
List you'll see the SQL SELECT statement.
of Listings
List of Sidebars
Note Chapter 4 covers SelectCommand,InsertCommand,UpdateCommand, and DeleteCommand
in more detail.

From the Properties window, you can also set CommandText,CommandType,Connection, and other
properties. If you double-click CommandText, it pops up the Query Builder where you can rebuild your
query.

TheTableMapping class represents a mapping of DataColumns in the data source to DataColumns in


theDataSet. We discuss DataTables and table mappings in more detail in Chapters 3 and 4. If you click
theTableMappings property (which is a collection of TableMapping objects), it brings up the Table
Mappings dialog box.
Question and Answer
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Q: Why Apress
do I need
2003table mappings? I've also heard of column mapping. What is
(928 pages)
it? This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Answers
namespaces and classes, and how to integrate both with
ADO.NET.

A: Sometimes, you won't want to display your actual table names and their columns as
Table of Contents defined in a database. For instance, say you have a MyTable table with two fields, a0 and
a1. The Data-Driven
Applied ADO.NETBuilding table storesSolutions
a customer's information, and the fields a0 and a1 store customer
Introduction
names and addresses. If you display your table in data components such as a DataGrid,
it will display
Chapter 1 - ADO.NET Basics
the table name as MyTable and the column names as a0 and a1, which
makes no sense to user.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected
Using Environments
table mappings, you can change the table name to Customers Table. Using
Chapter 4 - ADO.NET in Connected
column mapping,Environments
you can change the columns' names to Customer Name and Customer
Chapter 5 - Handling ADO.NET
Address. Events
After this mapping, DataGrid will display the table name as Customers Table
Chapter 6 - Integrating
and theXML with ADO.NET
columns as Customer Name and Customer Address, which make more sense to
Chapter 7 a user. and
- Data Binding We discuss
Windowstable
Formsand column mappings
Data-Bound Controls in Chapter 3 in more detail.
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
If you click the TableMappings property, it opens the Table Mappings dialog box. The Table Mappings
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
dialog box has two columns: Source Table and Dataset Table (see Figure 2-1). If you have more than one
Chapter
table in13 - Developing a Custom
a DataAdapter, Dataall
you'll see Provider
the tables listed in the Source Table list box. By default the source
Chapter 14 - Developing Database Web Applications
table Table is listed in the list box. The Source Table using ASP.NET
column is a list of actual columns, and the Dataset
Chapter 15 - Using
Table column is aADO.NET
list of theincolumn
Xml Web Services
names used in the DataSet. By default, DataSet columns names
Chapter
are the16 - ASP.NET
same as the Server
sourceControls andisData
table. This Binding
useful when you want to use different names in a program. You
can change
Chapter 17 - Building
DataSet columnsWeb
Real-World by editing the column itself. Of course, you can't change source columns,
Applications
but you18
Chapter can reorder them by Mapping
- Object-Relational using theinColumn
.NET Mappings drop-down list.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 2-1: Table Mappings dialog box

By using this dialog box, you can even delete columns from your mapping using the Delete button.

Step 5: Reviewing Other Options


If you look closelyApplied
at the DataAdapter's Properties
ADO.NET: Building window,
Data-Driven you'll see three links: Configure Data Adapter,
Solutions
Generate Dataset, and Preview Data.
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
The Configure Data Adapter option calls the Data Adapter Configuration Wizard, discussed earlier in this
This text provides extensive coverage of ADO.NET technology
chapter. If you want to reset the wizard to change your options, you can use this link.
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
The Generate Dataset option lets you generate a DataSet for this DataAdapter. The Generate DataSet
namespaces and classes, and how to integrate both with
option provides you with
ADO.NET.options that allow you to select whether you want to use an existing DataSet or
create a new DataSet. The Existing option is available only when a typed DataSet is available in your
project.
Table of Contents
CreateADO.NETBuilding
Applied a new DataSet with the default
Data-Driven name DataSet1. (You can change the DataSet name if you want.)
Solutions
Clicking the OK button adds a typed DataSet (DataSet1.xsd) to the project.
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio Question
.NET and Answer
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 -Q:
ADO.NET
What'sina typed
Connected Environments
DataSet? Are there other types of
Chapter 5 - Handling ADO.NET
DataSets? Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 -Answers
Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 -A:
There are two types of DataSets: typed and untyped. A typed DataSet is represented
Working with the ODBC .NET Data Provider
by an Extensible Markup Language (XML) schema. See the "Typed and Untyped
Chapter 11 - Stored Procedures and Views
DataSets" section of Chapter 3 for more information.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Actually,
Chapter 16the Generate
- ASP.NET DataSet
Server option
Controls adds
and DataaBinding
class DataSet1 to your project, which is inherited from
17 -IfBuilding
DataSet.
Chapter you go Real-World
to the ClassWeb
View, you can see the DataSet1 class and its members.
Applications
Chapter 18 - Object-Relational Mapping in .NET
The Preview Data option enables you to view the DataSet schema. You can even preview the data in the
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
DataSet by pressing the Fill button, which opens the Data Adapter Preview dialog box (see Figure 2-2).
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 2-2: Previewing data for the DataAdapter


The Fill Dataset button in Figure 2-2 fills data into a grid based upon the current state of the
Applied ADO.NET: Building Data-Driven Solutions
SelectCommand in the DataAdapter. The Parameters grid displays available parameters if a
by Mahesh Chand and David Talbot ISBN:1590590732
DataAdapter has any. The Clear Results button clears the records.
Apress 2003 (928 pages)
Thisthe
Step 6: Reviewing text provides
Source extensive coverage of ADO.NET technology
Code
including ADO.NET internals, namespaces, classes, and
interfaces,
Now it's time to examine theand takes
code anda see
comprehensive look at
what the wizard hasXML
done for you automatically. You can see the
namespaces and classes, and how to integrate both with
source code by right-clicking
ADO.NET.
the form and selecting the View Source option.

Note You should take the time to look at the code so you know what goes under the hood when you
use the IDE to write your code. It'll help you in the long run.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
All source code generated by the Windows form designer is defined in the InitializeComponent
Introduction
method of the file. Right-click your form and choose View Code. Upon examining the source code, you'll
Chapter 1 -the
see where ADO.NET
wizardBasics
has added two components, sqlConnection1 and sqlDataAdapter1, to your
Chapter
source 2file-as
Data Components
well in Visual Studio
as four SqlCommand .NET
components. Scroll down to the Windows Designer Generated
Chapter 3 - ADO.NET
Code option in Disconnected
and expand Environments
it. This reveals the contents of the InitializeComponent routine (see Listing 2-
Chapter
1). 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Listing62-1:
Chapter Added SqlXML
- Integrating Server Provider Components
with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter
Friend 8 WithEvents
- Constraints and Data Relations
DataGrid1 As System.Windows.Forms.DataGrid
Chapter 9 - ADO.NET
Friend Exception
WithEvents Handling
SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
Friend
Chapter WithEvents
10 - Working with theSqlSelectCommand1
ODBC .NET Data ProviderAs System.Data.SqlClient.SqlCommand
Friend
Chapter WithEvents
11 - Stored SqlInsertCommand1
Procedures and Views As System.Data.SqlClient.SqlCommand
Friend
Chapter WithEvents
12 - Oracle, SQLXML, SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
and Other .NET Data Providers
Friend
Chapter WithEvents
13 - Developing SqlDeleteCommand1
a Custom Data Provider As System.Data.SqlClient.SqlCommand
Friend
Chapter WithEvents
14 - Developing SqlConnection1
Database Web ApplicationsAs System.Data.SqlClient.SqlConnection
using ASP.NET
Friend WithEvents DataSet11 As MyFirstADONetApp.DataSet1
Chapter 15 - Using ADO.NET in Xml Web Services
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Chapter 16 - ASP.NET Server Controls and Data Binding
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Chapter 17 - Building Real-World Web Applications
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter()
Chapter 18 - Object-Relational Mapping in .NET
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand()
ChapterMe.SqlUpdateCommand1
20 - COM Interoperability and
= ADO.NET
New System.Data.SqlClient.SqlCommand()
ChapterMe.SqlDeleteCommand1
21 - Messaging = New System.Data.SqlClient.SqlCommand()
Chapter 22 - SQL Server and
Me.SqlConnection1 = ADO.NET: Notes on Performance
New System.Data.SqlClient.SqlConnection()
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Search for the ConnectionString by hitting Ctrl+F to bring up VS .NET's Find dialog box. If you examine
Index
theof
List InitializeComponent()
Figures method, you'll see that the wizard sets SqlConnection's
ConnectionString
List of Tables property to the following:
List of Listings
Me.SqlConnection1.ConnectionString
List of Sidebars = _
"data source=localhost;initial catalog=Northwind;" & _
"integrated security=SSPI;persist " & _
"security info=False;workstation id=MCB;packet size=4096"

It also sets the CommandText property of the SqlCommand with the corresponding SELECT,INSERT,
UPDATE, and DELETE SQL statements. The Connection property of SqlCommand is set to
SqlConnection:

Me.SqlSelectCommand1.CommandText = "SELECT EmployeeID, LastName, " & _


"FirstName FROM Employees"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
If you examine Listing
Apress2-2, you'll
2003 (928see that DataAdapter is connected to a Connection through data
pages)
commands, and theThisTableMapping property
text provides extensive is responsible
coverage for mapping
of ADO.NET tables and their columns. Note
technology
including
that the table mappings ADO.NET
between internals,columns
DataSet namespaces, classes, and columns generated by the wizard
and DataSource
interfaces,
have exactly the same andnames.
column takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Listing 2-2: DataAdapter Connection through TableMapping

TableMe.SqlDataAdapter1.DeleteCommand
of Contents = Me.SqlDeleteCommand1
AppliedMe.SqlDataAdapter1.InsertCommand
ADO.NETBuilding Data-Driven Solutions = Me.SqlInsertCommand1
Introduction
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
ChapterMe.SqlDataAdapter1.TableMappings.AddRange(_
1 - ADO.NET Basics
ChapterNew
2 -System.Data.Common.DataTableMapping()
Data Components in Visual Studio .NET { _
ChapterNew
3 -System.Data.Common.DataTableMapping("Table",
ADO.NET in Disconnected Environments "Employees", _
ChapterNew
4 -System.Data.Common.DataColumnMapping()
ADO.NET in Connected Environments { _
ChapterNew
5 -System.Data.Common.DataColumnMapping("EmployeeID",
Handling ADO.NET Events "EmployeeID"), _
ChapterNew
6 -System.Data.Common.DataColumnMapping("LastName",
Integrating XML with ADO.NET "LastName"), _
ChapterNew
7 -System.Data.Common.DataColumnMapping("FirstName",
Data Binding and Windows Forms Data-Bound Controls "FirstName")})})
Me.SqlDataAdapter1.UpdateCommand
Chapter 8 - Constraints and Data Relations
= Me.SqlUpdateCommand1
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
It looks like the wizard did a lot of the work for you!
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Step 7:13
Chapter Filling the DataGrid
- Developing Control
a Custom with Data
Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Until now, you haven't had to write a single line of code. Now, though, you'll add few lines of code and then
Chapter 15 - Using ADO.NET in Xml Web Services
you'll be all set to see the data from your data source. Now you have two options. First, you can use the
Chapter 16 - ASP.NET Server Controls and Data Binding
typed DataSet to read the data, and second, you can create a DataSet object programmatically and fill it
Chapter 17 - Building Real-World Web Applications
using the SqlDataAdapter'sFill method.
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped
You'll create Objects:
a DataSet Performance
object Considerations
programmatically anditData
and use to fillBinding
data in a DataGrid. Add the code in
Listing 20
Chapter 2-3-toCOM
the Interoperability
form load eventand
handler. As you can see, this code creates a DataSet object and fills it
ADO.NET
by calling
Chapter 21 SqlDataAdapter's
- Messaging Fill method. The Fill method of SqlDataAdapter fills data from a
22 - SQL to
DataAdapter
Chapter the DataSet.
Server You Notes
and ADO.NET: call the
onFill method in the FillDBGrid method. Once you have a
Performance
DataSet
Appendix A -containing
Relational data, you can
Databases: do anything
Some with it, including creating views for that data. In this example,
Basic Concepts
you set aB DataGrid
Appendix - Commonlycontrol's
Used SQL Statements property to the DataSet.DefaultViewManager, which
DataSource
binds theC DataSet
Appendix - ADO.NETobject to theAsked
Frequently DataGrid control.
Questions
Index
Listing 2-3: Form's Load Event Handler
List of Figures
List of Tables
Private
List Sub Form1_Load(ByVal sender As System.Object, _
of Listings
ByVal e As System.EventArgs) Handles MyBase.Load
List of Sidebars
Dim ds As DataSet = New DataSet()
SqlDataAdapter1.Fill(ds)
DataGrid1.DataSource = ds.DefaultViewManager
End Sub

Another option is to use the typed DataSet you generated using the Generate DataSet option. The wizard
added an instance of DataSet1 called DataSet11 to the project. Now just call the Fill method of
DataAdapter and bind the DataSet's default view to the DataGrid. Just write the following two lines on
the form load event handler:
SqlDataAdapter1.Fill(DataSet11)
Applied ADO.NET:
DataGrid1.DataSource Building Data-Driven Solutions
= DataSet11.DefaultViewManager
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Finally, build andThis
run the
text project
providesusing eithercoverage
extensive of the two
of methods. The result looks like Figure 2-3. Easy,
ADO.NET technology
huh? including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Figure 2-3: Output of the Employee data to a DataGrid control
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Using DataSet and
Applied DataView
ADO.NET: BuildingComponents
Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
After discussing DataAdapters
Apress 2003 (928 and data connections, you should have a pretty good idea of how to take
pages)
advantage of the This
VS .NET design-time support
text provides extensive to develop
coverage databound
of ADO.NET Windows Form database
technology
applications. including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
TheDataSet andnamespaces
DataViewand components
classes, andare twotopowerful
how integrateand
botheasy-to-use
with components of the
ADO.NET model.ADO.NET.
In this section, you'll learn how to utilize DataSet and DataView components at
design-time. In Chapter 4, we discuss their properties and methods in more detail and show how to use
them programmatically. The DataSet and DataView components fall into the disconnected
Table of Contents
components category, which means you can use these components with or without data providers. We
Applied ADO.NETBuilding Data-Driven Solutions
discuss disconnected data components in Chapter 3.
Introduction
Chapter 1 - ADO.NET Basics
Understanding
Chapter Typed
2 - Data Components DataSets
in Visual Studio .NET in VS .NET
Chapter 3 - ADO.NET in Disconnected Environments
There are two types of DataSets: typed and untyped. As discussed in Chapter 1 (and in more detail in
Chapter 4 - ADO.NET in Connected Environments
Chapter 4), a typedDataSet has an XML schema attached to it. The XML schema defines members for
Chapter 5 - Handling ADO.NET Events
aDataSet corresponding to database table columns, and you can access data through these columns.
Chapter 6 - Integrating XML with ADO.NET
Untyped datasets are ones that are created at runtime and don't have a schema attached to them. In the
Chapter 7 section,
following - Data Binding
we showandyou
Windows Forms
how you can Data-Bound Controls
generate typed datasets using a VS .NET wizard.
Chapter 8 - Constraints and Data Relations
Generating
Chapter Typed
9 - ADO.NET DataSets
Exception Using
Handling a DataAdapter
Chapter 10 - Working with the ODBC .NET Data Provider
As you 11
Chapter saw earlier,Procedures
- Stored you can generate typed datasets by using the Generate Dataset option from a
and Views
DataAdapter's
Chapter Properties
12 - Oracle, dialog
SQLXML, and box..NET
Other AndData
as mentioned
Providers earlier, the Generate DataSet option adds an
XML schema to the project and also adds a class to the project inherited from the DataSet class. The
Chapter 13 - Developing a Custom Data Provider
designer looks like Figure 2-4.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Figure 2-4: ADataSet's Properties window showing a typed DataSet
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Again, you can see a typed DataSet's properties by right-clicking the DataSet and using the Properties
Chapter 21 - Messaging
menu option.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
EveryDataSet
Appendix generated
A - Relational by theSome
Databases: IDE creates an XML schema for the DataSet. If you look at the bottom
Basic Concepts
of the Properties
Appendix window
B - Commonly of SQL
Used a DataSet, you'll see two links: View Schema and DataSet Properties. The
Statements
View Schema
Appendix option Frequently
C - ADO.NET lets you view the Questions
Asked DataSet schema, and the DataSet Properties hyperlink lets you
set the DataSet properties. By following these links you can also set the DataSet's column names and
Index
other
List properties.
of Figures
List of Tables
Now you can use this DataSet as discussed earlier.
List of Listings
List of Sidebars
Adding a Typed DataSet
In the previous discussion, you saw how you can generate DataSet objects from a DataAdapter. There
are also other ways to create a typed DataSet object.

You can click the Project menu and choose Add New Item. This brings up the Add New Item window
where you'll find the DataSet template (see Figure 2-5). The default name of the DataSet template is
DataSet1.xsd, but you can change it by entering something in the Name text box.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Figure
Chapter 3 - 2-5: Creating
ADO.NET a typed DataSet
in Disconnected from the Add New Item window
Environments
Chapter 4 - ADO.NET in Connected Environments
After adding the DataSet, the designer creates an XSD (XML Schema) file and adds it your project area.
Chapter 5 - Handling ADO.NET Events
By default the schema is empty the first time you add a DataSet schema using the Add New Item. You
Chapter 6 - Integrating XML with ADO.NET
can see the contents of a DataSet schema by double-clicking DataSet1.xsd in the Solution Explorer.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
As you can see from Figure 2-6, the designer has two options: the Server Explorer and the Toolbox. If you
Chapter 8 Server
click the - Constraints
Explorerand Data
link, Relations the Server Explorer. The XML designer has its own Toolbox,
it launches
Chapter 9 - ADO.NET Exception Handling
which contains various XML schema items.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
Figure 2-6: EmptyDataset1.xsd in the VS .NET IDE
List of Figures
List of Tables
The
List of next step is to fill this XML schema with data and connect to a database. The easiest way to do this is
Listings
to use
List the Server Explorer. Open the Server Explorer by clicking the Sever Explorer link in the designer.
of Sidebars
You can expand any database and simply drop a database object such as a table, a view, or a stored
procedure on the designer from the Server Explorer. You can even drop multiple database objects. To
keep it simple, drop only one table, the Employees table from the SQL Server Northwind database.

Dragging the Employees table to the designer adds one XML schema (see Figure 2-7).
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Figure
Chapter 8 - 2-7: Designand
Constraints viewData
of the DataSet's XML schema
Relations
Chapter 9 - ADO.NET Exception Handling
It also automatically adds the typed DataSet class that inherits from DataSet. If you go to the Class
Chapter 10 - Working with the ODBC .NET Data Provider
View, you can see the DataSet1 class. If you expand this class, you'll notice this class is derived from the
Chapter 11 - Stored Procedures and Views
DataSet class. You can explore the code added by the designer. However, you'll never use these class
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
members directly.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 -have
Once you Developing
a typedDatabase
DataSet,Web
youApplications
can bind itusing
to anyASP.NET
data-bound control (as discussed earlier in this
Chapter 15 - Using ADO.NET in Xml Web Services
chapter).
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Understanding the DataView
Chapter 18 - Object-Relational Mapping in .NET
ADataView
Chapter represents
19 - Mapped a view
Objects: of a DataSet
Performance object. and Data Binding
Considerations
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging Question and Answer
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Q: Why do I need a DataView when I can use a DataSet's
Appendix B - Commonly Used SQL Statements
DefaultViewManager?
Appendix C - ADO.NET Frequently Asked Questions
Index Answers
List of Figures
List of Tables
A:
List of Listings Actually, the DefaultViewManager provides a view of all the available tables in a
List of Sidebars DataSet. What if you want to display only one table? Or what if you want to filter or sort
data?

ADataView provides you with Filter and Sort properties; by using them you can filter and sort the
data. There are many occasions when you don't want all data from a table. You can apply a filter criteria
on a DataView and bind the DataView to the data-bound controls. You can also separate multiple
tables of a DataSet by associating each table with a DataViewl. For example, you can create a
DataSet with three tables and create three different DataView objects for each table. Once you have a
DataView object, you can attach it with any data-bound control, such as a DataGrid or a ComboBox
control using the data-bound control's DataSource property. Or you can filter only records based on a
date or an ID.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh
Note In this chapter, weChand and David
concentrate Talbot
on the VS .NET part of DataView. We discuss the DataView
ISBN:1590590732
class andApress
its members
2003 (928in more detail in Chapter 4.
pages)
This text provides extensive coverage of ADO.NET technology
Creating and attaching a DataView
including with a data
ADO.NET internals, source is classes,
namespaces, simple inand
the designer. In this sample, you'll
create and attachinterfaces,
a DataViewand takes
with aaDataSet.
comprehensive look
Before at XMLa DataView, though, you need to have a
creating
DataSet. namespaces and classes, and how to integrate both with
ADO.NET.
First, you create a DataAdapter by simply dragging a database table from the Server Explorer to the
form. This action adds a Connection and a DataAdapter to the application and makes them available
Table of Contents
at design-time. Second, you generate a DataSet from a DataAdapter by selecting the Generate
Applied ADO.NETBuilding Data-Driven Solutions
Dataset menu option available after right-clicking the DataAdapter.
Introduction
Chapter
Now you1 need
- ADO.NET
to addBasics
a DataView. You simply drag a DataView by dragging the DataView control from
Chapter
Toolbox2 -Data
Dataonto
Components in Visual
your form. Studioadds
This action .NETa DataView1 to the project.
Chapter 3 - ADO.NET in Disconnected Environments
Next, attach
Chapter DataView1
4 - ADO.NET to the DataSet.
in Connected You can do that by simply setting the Table property of the
Environments
DataView.
Chapter 5
The Table property of DataView1 lists the DataSet and DataSet tables. In this case,
- Handling ADO.NET Events
selectDataSet11.Employees as the Table property of DataView1 (see Figure 2-8).
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Figure 2-8: DataView Properties window
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET
This is where Frequently
the DataView Asked Questions
is useful. The DataView has two properties: Sort and RowFilter. The
Index
Sort property takes an expression with the name of the column followed by the sort order. For example, if
List
youofwant
Figures
to sort data based on the FirstName column in ascending order, you set the FirstName ASC
string
List as the Sort property of DataView.
of Tables
List of Listings
TheRowFilter property filters the data of a DataView. This property works as a WHERE clause of the
List of Sidebars
SELECT statement. The RowFilter property takes an expression with the column name and the criteria.
For example, if you want to retrieve records where the country is USA, you simply set "Country='USA'"
as the filter. As you can see in Figure 2-8, we set the RowFilter and Sort properties.

Note To sort in descending order, use DESC instead of ASC.

Once you have a DataView, you can bind it to a data-bound control such as a DataGrid by setting data-
bound control's DataSource property. To test this, you can drop a DataGrid control onto the form and
setDataGrid'sDataSource property as DataView1. You can simply set that from the Properties
window of DataGrid.

Now if you compile and run your project, you'll only see USA records sorted on the FirstName column
(see Figure 2-9).
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Figure
Applied 2-9: Sorted and
ADO.NETBuilding filtered data
Data-Driven in a DataGrid
Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Using the Data Form
Applied Wizard
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
The Data Form Wizard
Apress is one (928
2003 most useful tools for developing database applications. You can use the Data
pages)
Form Wizard to develop your database application
This text provides extensive coveragewith
of viewing,
ADO.NETupdating,
technologyand deleting capabilities. This is
probably the fastest way to develop database applications in .NET
including ADO.NET internals, namespaces, classes, and (unless you're an extremely fast typist!).
interfaces, and takes a comprehensive look at XML
namespaces
In the following sections, you'lland classes,
use andForm
the Data how to integrate
Wizard bothawith
to write fully functional database application including
ADO.NET.
features such as inserting, updating, and deleting data without writing a single line of code. In this simple
example, you'll use the familiar Northwind database, and you'll use both the Customers and Orders tables to
build a data relationship between table data.
Table of Contents
Applied ADO.NETBuilding
First, you'll Data-Driven
create a Windows Solutions
application. Second, you'll add a Data Form Wizard to it and then call the Data
Introduction
Form Wizard from the main application. Like many parts of this book, this topic is in the form of tutorial. Just
Chapter 1 -simple
follow the ADO.NET Basics
steps, and in a few minutes you'll be able to run the application.
Chapter 2 - Data Components in Visual Studio .NET
Step 1:
Chapter 3 Selecting
- ADO.NET in a Project Template
Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Create 5a new
Chapter Windows
- Handling by selecting File New Project Visual Basic Projects Windows Application
projectEvents
ADO.NET
and typing-MyDataFormWizardSamp
Chapter 6
as your application name.
Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Step 2: Adding a Data Form Wizard Item
Chapter 8 - Constraints and Data Relations
Chapter
Now add9 a- Data
ADO.NET
FormException Handling
Wizard by selecting Project Add New Item Data Form Wizard from the available
Chapter 10 - You
templates. Working with the
can type ODBC .NET Data
DataForm1.vb Provider
as the name of your DataForm class in the Name field of the dialog box
(see Figure
Chapter 11 - 2-10).
Stored (If you're using
Procedures andC# language, the filename would be DataForm1.cs.)
Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
Figure 2-10: Adding a Data Form Wizard
List of Tables
List
Now of click
Listings
Open, which calls the Data Form Wizard.
List of Sidebars
Step 3: Walking through the Data Form Wizard
The first page of the wizard is a welcome page telling you what the wizard is about to do.

Step 4: Choosing the DataSet You Want


On the second page of the wizard, you can choose a DataSet name that will later be used to access the data.
You can either create a new DataSet name or select an existing one. If there's no DataSet available in the
project, the Use the Following Dataset Named option is disabled. For this test, type MyDS as the DataSet name
in the Create a New Dataset Named option and move to the next page by clicking the Next button.

Step 5: Choosing a Data Connection


The next page ofApplied
the wizard asks youBuilding
ADO.NET: to provide a connection.
Data-Driven The combo box displays your available
Solutions
connections. If you
by didn't
Mahesh create
Chanda connection, use the New Connection
and David Talbot button, which launches the Server
ISBN:1590590732
Explorer discussed earlier
Apress in this
2003 (928chapter.
pages) You'll select the usual database, Northwind. If you followed the Server
Explorer discussion
Thisintext
the provides
beginning of this chapter,
extensive coverageyou should have
of ADO.NET a Northwind connection in the list. If you
technology
don't have any connection listed in this
including ADO.NET list, create
internals, a new connection
namespaces, classes, andusing the Northwind SQL Server database.
interfaces, and takes a comprehensive look at XML
namespaces
Step 6: Choosing Tablesand or
classes,
Viewsand how to integrate both with
ADO.NET.
The next page of the wizard lets you pick the tables and views you want to connect to the DataSet. As you can
see in
Table of Figure 2-11, we selected the Customers and Orders tables in the Available Items list on this page and use
Contents
the > button to add these tables to the Selected Items list.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Figure
Chapter 17 - 2-11: Choosing
Building tables
Real-World Weband views
Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Now you're ready to create a relationship between these two tables.
Chapter 20 - COM Interoperability and ADO.NET
Chapter
Step 7:21 Creating
- Messaging a Relationship between Tables
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
The nextApage
Appendix of the wizard
- Relational lets you
Databases: Somedefine
BasicaConcepts
relationship between the Customers and Orders tables. It's useful
to provide
Appendix B -a Commonly
relationship between
Used tables when you have a master-detail relationship in the database. In other
SQL Statements
words, aCcustomer
Appendix may
- ADO.NET have many
Frequently orders
Asked associated with it, so there's a relationship through the CustomerID in
Questions
the Orders table joined to information about the customer in the Customers table. Now, say you want to see all
Index
theoforders
List of a customer based on the CustomerID. If you do this manually, you need to write code to select data
Figures
from the Orders table to correspond to a CustomerID and then fill data to the form. If you use the Data Form
List of Tables
Wizard instead, it does everything for you. Cool, huh?
List of Listings
List
Thisof is
Sidebars
the same step you're going to see on the Create a Relationship between Tables page of the wizard.
You're going to create a relationship between the Customers and Orders tables based on the CustomerID. Name
the relationship between the Customers and Orders table CustOrderRelation. You also need to pick the
associated primary key and foreign key that links the parent to the child table. Once you've chosen the joining key
(CustomerID), you have to press the > button to tell the wizard that you want to add it.

When you run the final program, you'll see how you can filter all orders for a customer based on the CustomerID.
As you can see from Figure 2-12, you need to pick one table as parent and another table as a child based on the
relationship between them. In this example, the Customers table is the parent table, and the Orders table is the
child table.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - 2-12:
Figure Data Binding andCustomers
Selecting Windows Forms
as theData-Bound
parent andControls
Orders as the child table to create the
Chapter relationship
8 - Constraints and Data
CustOrderRelation Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Step 8: Choosing Tables and Columns to Display on the Form
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
The next page of the wizard lets you select which tables and columns you want to show on the form. For this
Chapter 13 select
example, - Developing
all the acolumns
Custom from
Data Provider
both of the tables (this is the default selection). As you can see in Figure 2-
Chapter 14 - Developing Database Web
13, the Customers table is the master, Applications using table
and the Orders ASP.NETis the detail table. On this page, you can select
Chapter 15 - Using ADO.NET in Xml Web Services
whatever column you want to display on the form.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 2-13: Choosing columns to display on the Data Form Wizard

Step 9: Choosing the Display Style


This page is an important part of creating your form. Actually, the Data Form Wizard adds a Form with some
controls on it and writes code to fill, update, delete, and navigate data. There are two ways to view the data, and
you choose your option on this page. These two options are as follows:
All Records in a Grid
Applied ADO.NET: Building Data-Driven Solutions
byin
Single Record Mahesh Chand
Individual and David Talbot
Controls ISBN:1590590732
Apress 2003 (928 pages)
Figure 2-14 displays
This these options.extensive coverage of ADO.NET technology
text provides
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Figure 2-14: Choosing between a grid and individual controls on the Data Form Wizard
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
The output
Chapter 13 - of All Records
Developing in a Grid
a Custom Datalooks like Figure 2-15. After that you can resize or move the controls on the
Provider
form. 14 - Developing Database Web Applications using ASP.NET
Chapter
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
Figure 2-15: Grid DataForm output
List of Sidebars

The second option, Single Record in Individual Controls, shows data in text boxes and provides you with
navigation controls. As you can see from Figure 2-16, the Single Record in Individual Controls option activates
Add, Delete, Cancel, and Navigation Controls check boxes. You can uncheck these check boxes if you don't
want to add those features to your project.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Figure- 2-16:
Chapter 5
The Single Record in Individual Controls option
Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
The form generated by this option looks like Figure 2-17. As you can see, each column of the table has a field on
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
the form.
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Figure
Appendix C - 2-17: Data
ADO.NET Form Wizard-generated
Frequently Asked Questions form for the Single Record in Individual Controls option
Index
List of Figures
After selecting the data display style, click the Finish button. The Data Form Wizard adds the Windows form
List of Tables and the class DataForm1.vb corresponding to it.
DataForm1
List of Listings
Step
List 10: Calling
of Sidebars the Data Form Wizard Form from the Application
Now you need to change one more thing. You need to call the DataForm1 form. Generally, you call a
DataForm from a menu or a button click event handler. Say you've added two DataForm objects, DataForm1
andDataForm2, using both the options. The code in Listing 2-4 calls DataForm1.

Listing 2-4: CallingDataForm1 from a Button Click

Private Sub Button1_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles Button1.Click
Dim dfw1 As DataForm1 = New DataForm1()
dfw1.Show()
End Sub
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Listing 2-5 calls DataForm2 from extensive
This text provides a button click handler.
coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces,
Listing 2-5: Calling and takes
DataForm2 froma a
comprehensive
Button Clicklook at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Table of Contents
Dim dfw2 As DataForm2 = New DataForm2()
Applieddfw2.Show()
ADO.NETBuilding Data-Driven Solutions
Introduction
End Sub
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Note
Chapter 4 If- you have in
ADO.NET modified the Environments
Connected name of your Data Form Wizard-generated form, you need to call that form
Chapter 5 instead of DataForm1
- Handling and DataForm2.
ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Step 11:
Chapter 7 - Viewing the
Data Binding andOutput
Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
When you run any of the modes (DataGrid mode or individual column mode), you'll see Load and Update
Chapter 9 - ADO.NET Exception Handling
buttons. The Load and Update buttons load and update the data, respectively, and Cancel All cancels all the
Chapter 10 - Working with the ODBC .NET Data Provider
operations. The neat thing is if you move into the top grid, corresponding information changes in the bottom grid.
Chapter 11 - Stored Procedures and Views
After compiling
Chapter andSQLXML,
12 - Oracle, running and
yourOther
application, you'll
.NET Data notice that without writing a single line of code, you just
Providers
created13a fully
Chapter functional
- Developing database
a Custom application.
Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
The Load button on the individual control form loads the data, and the Add, Update, and Delete buttons on the
Chapter 15 - Using ADO.NET in Xml Web Services
form inserts, updates, and deletes records, respectively.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Data Form Wizard: Looking under the Hood
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
You just saw how you can develop fully functional database applications with the help of the Data Form Wizard.
Chapter 20 - COM Interoperability and ADO.NET
Now let's see what the wizard does for you in the actual code. (The inherent beauty of the VS .NET IDE is that it
Chapter 21 - Messaging
magically hides all the messy code for you.) The wizard adds two items to your project: MyDS.xsd and
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
DataForm1.vb.
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Understanding MyDS.xsd
Appendix C - ADO.NET Frequently Asked Questions
Index
MyDS.xsd is an XML schema for the DataSet you've added to the project. (It's similar to the one discussed in
List
theof"Understanding
Figures Typed DataSets in VS .NET" section of this chapter.)
List of Tables
List of Listings
Understanding DataForm1.vb
List of Sidebars
The second item added by wizard is the DataForm1 class, a class derived from
System.Windows.Forms.Form. The DataForm1 class defines the class's entire functionality:

Public Class DataForm1


Inherits System.Windows.Forms.Form

ThebtnUpdate_Click and btnLoad_Click methods are the event handlers for the Update and Load
buttons, respectively. As you can see from Listing 2-6, the btnLoad_Click method calls LoadDataSet, and
thebtnUpdate_Click method calls the UpdateDataSet method.
Listing 2-6: The Load and Update Button Click Event Handlers
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Private Sub btnUpdate_Click(ByVal
Apress 2003 (928 pages) sender As System.Object, _
ByVal e As System.EventArgs) Handles btnUpdate.Click
This text provides extensive coverage of ADO.NET technology
Try including ADO.NET internals, namespaces, classes, and
'Attempt to update
interfaces, the
and takes datasource.
a comprehensive look at XML
namespaces and classes, and how to integrate both with
Me.UpdateDataSet()
ADO.NET.
Catch eUpdate As System.Exception
'Add your error handling code here.
'Display error message, if any.
Table of Contents
System.Windows.Forms.MessageBox.Show(eUpdate.Message)
Applied ADO.NETBuilding Data-Driven Solutions
End Try
Introduction
Chapter 1 - ADO.NET Basics
End Sub
Chapter 2 - Data Components in Visual Studio .NET
Private Sub btnLoad_Click(ByVal sender As System.Object, _
Chapter 3 - ADO.NET in Disconnected Environments
ByVal e As System.EventArgs) Handles btnLoad.Click
ChapterTry
4 - ADO.NET in Connected Environments
Chapter 5 'Attempt
- Handling to
ADO.NET
loadEvents
the dataset.
Chapter 6 Me.LoadDataSet()
- Integrating XML with ADO.NET
ChapterCatch
7 - Data Binding
eLoad Asand Windows Forms Data-Bound Controls
System.Exception
Chapter 8 'Add
- Constraints and Data
your error Relations code here.
handling
Chapter 9 'Display
- ADO.NET error
Exception Handling if any.
message,
Chapter 10System.Windows.Forms.MessageBox.Show(eLoad.Message)
- Working with the ODBC .NET Data Provider
End Try
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
TheLoadDataSet method loads the data from the data source into the controls by calling FillDataSet (see
Chapter 15 - Using ADO.NET in Xml Web Services
Listing 2-7). As you can see from the code, it assigns the MyDS DataSet generated by the Data Form Wizard as
Chapter 16 - ASP.NET Server Controls and Data Binding
a temporary DataSet and fills it using the FillDataSet method.
Chapter 17 - Building Real-World Web Applications
Chapter
Listing18 - Object-Relational
2-7: LoadDataSet Mapping in .NET
Method Generated by the Data Form Wizard
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Public Sub LoadDataSet()
Chapter 21 - Messaging
'Create a new dataset to hold the records returned
Chapter'A 22 temporary
- SQL Server and ADO.NET:
dataset is Notes
usedonbecause
Performance
filling the existing dataset would
Appendix A - Relational Databases: Some Basic
'require the databindings to be rebound. Concepts
AppendixDimB -objDataSetTemp
Commonly Used SQL As Statements
MyDataFormWizardSamp.MyDS
Appendix C - ADO.NET Frequently
objDataSetTemp = NewAsked Questions
MyDataFormWizardSamp.MyDS()
Index Try
List of Figures
'Attempt to fill the temporary dataset.
Me.FillDataSet(objDataSetTemp)
List of Tables
List of Listings eFillDataSet As System.Exception
Catch
'Add your error handling code here.
List of Sidebars
Throw eFillDataSet
End Try
Try
'Empty the old records from the dataset.
objMyDS.Clear()
'Merge the records into the main dataset.
objMyDS.Merge(objDataSetTemp)
Catch eLoadMerge As System.Exception
'Add your error handling code here.
Throw eLoadMerge
End Try
End Sub
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
TheFillDataSetApress
fills the
2003 from the DataAdapter by calling the Fill method on each
(928 pages)
DataSet
DataAdapter. Note that provides
This text with the extensive
Data Form Wizard,ofa ADO.NET
coverage is created for each table-one
technology
DataAdapter
including
DataAdapter for ADO.NETtable
the Customers internals, namespaces,
and one classes,for
DataAdapter andthe Orders table. Both DataAdapters fill
interfaces, and takes a comprehensive look at XML
the same DataSet. Listing 2-8 shows the FillDataSet method.
namespaces and classes, and how to integrate both with
ADO.NET.
Listing 2-8: The FillDataSet Method Generated by the Data Form Wizard

Table of Contents
Public Sub FillDataSet(ByVal dataSet As MyDataFormWizardSamp.MyDS)
Applied'Turn
ADO.NETBuilding Data-Driven
off constraint Solutions before the dataset is filled.
checking
Introduction
'This allows the adapters to fill the dataset without concern
Chapter'for
1 - ADO.NET Basics
dependencies between the tables.
ChapterdataSet.EnforceConstraints
2 - Data Components in Visual Studio
= .NET
False
Chapter Try
3 - ADO.NET in Disconnected Environments
Chapter 4 'Open
- ADO.NET
thein connection.
Connected Environments
Chapter 5 Me.OleDbConnection1.Open()
- Handling ADO.NET Events
Chapter 6 'Attempt
- Integratingto
XMLfill the dataset through the OleDbDataAdapter1.
with ADO.NET
Chapter 7 Me.OleDbDataAdapter1.Fill(dataSet)
- Data Binding and Windows Forms Data-Bound Controls
Chapter 8 Me.OleDbDataAdapter2.Fill(dataSet)
- Constraints and Data Relations
Chapter Catch fillException
9 - ADO.NET As System.Exception
Exception Handling
'Add your error handling code here.
Chapter 10 - Working with the ODBC .NET Data Provider
Throw fillException
Chapter 11 - Stored Procedures and Views
Finally
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
'Turn constraint checking back on.
Chapter 13 - Developing a Custom Data Provider
dataSet.EnforceConstraints = True
Chapter 14 - Developing Database Web Applications using ASP.NET
'Close the connection whether or not the exception was thrown.
Chapter 15 Me.OleDbConnection1.Close()
- Using ADO.NET in Xml Web Services
Chapter End- ASP.NET
16 Try Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
End Sub
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Note
Chapter 22 One
- SQLthing youand
Server may notice isNotes
ADO.NET: that the
on Data Form Wizard uses the OleDb provider to write the Data Form
Performance
Appendix A Wizard code.
- Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
TheUpdateDataSource method updates the data source from the DataSet. This method is called by the
Appendix C - ADO.NET
UpdateDataSet Frequently
method Asked Questions
and utilizes the Update command of the DataAdapters. Listing 2-9 shows the
Index
UpdateDataSource method.
List of Figures
Listing
List 2-9: The UpdateDataSource and UpdateDataSet Methods Generated by the Data Form Wizard
of Tables
List of Listings
List ofPublic
Sidebars Sub UpdateDataSet()
'Create a new dataset to hold the changes that have been made to the main dataset.
Dim objDataSetChanges As MyDataFormWizardSamp.MyDS = _
New MyDataFormWizardSamp.MyDS()
'Stop any current edits.
Me.BindingContext(objMyDS, "Customers").EndCurrentEdit()
Me.BindingContext(objMyDS, "Orders").EndCurrentEdit()
'Get the changes that have been made to the main dataset.
objDataSetChanges = CType(objMyDS.GetChanges, MyDataFormWizardSamp.MyDS)
'Check to see if any changes have been made.
If (Not (objDataSetChanges) Is Nothing) Then
Try
'There are changes that need to be made, so attempt to update"
Applied
'calling theADO.NET:
updateBuilding
method Data-Driven
and passingSolutions
the dataset and any parameters.
by Mahesh Chand and David Talbot
Me.UpdateDataSource(objDataSetChanges) ISBN:1590590732
Apress 2003 (928 pages)
objMyDS.Merge(objDataSetChanges)
This text provides extensive coverage of ADO.NET technology
objMyDS.AcceptChanges()
including ADO.NET
Catch eUpdate internals, namespaces, classes, and
As System.Exception
interfaces, and takes a comprehensive look at XML
'Add namespaces
your error and handling codeto here.
classes, and how integrate both with
ThrowADO.NET.
eUpdate
End Try
'Add your code to check the returned dataset for any errors"
Table of Contents
'pushed into the row object's error.
AppliedEnd
ADO.NETBuilding
If Data-Driven Solutions
Introduction
End Sub
Chapter 1 - ADO.NET Basics
Chapter
Public 2 Sub
- Data Components in Visual Studio .NET
UpdateDataSource(ByVal ChangedRows As MyDataFormWizardSamp.MyDS)
ChapterTry
3 - ADO.NET in Disconnected Environments
Chapter 4 'The data insource
- ADO.NET only
Connected needs to be updated if there are changes pending.
Environments
Chapter 5 If- Handling
(Not (ChangedRows)
ADO.NET Events Is Nothing) Then
Chapter 6 -'Open theXML
Integrating connection.
with ADO.NET
Chapter 7 -Me.OleDbConnection1.Open()
Data Binding and Windows Forms Data-Bound Controls
Chapter 8
'Attempt to update the data source.
- Constraints and Data Relations
OleDbDataAdapter1.Update(ChangedRows)
Chapter 9 - ADO.NET Exception Handling
OleDbDataAdapter2.Update(ChangedRows)
Chapter 10 - Working with the ODBC .NET Data Provider
End If
Chapter 11 - Stored Procedures and Views
Catch updateException As System.Exception
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
'Add your error handling code here.
Chapter 13 - Developing
Throw a Custom Data Provider
updateException
ChapterFinally
14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET
'Close in Xml Web Services
the connection whether or not the exception was thrown.
Chapter 16 - ASP.NET Server Controls and Data Binding
Me.OleDbConnection1.Close()
ChapterEnd
17 -Try
Building Real-World Web Applications
Chapter
End Sub18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
ThebtnCancelAll_Click method is the Cancel All button click handler, which calls the DataSet's
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
RejectChanges method (see Listing 2-10).
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Listing 2-10: Cancel All Button Click Event Handler
Appendix C - ADO.NET Frequently Asked Questions
Index
Private Sub btnCancelAll_Click(ByVal sender As System.Object, _
List of Figures
ByVal e As System.EventArgs) Handles btnCancelAll.Click
List of Tables
Me.objMyDS.RejectChanges()
List of Listings
End Sub
List of Sidebars
Summary Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Congratulations! Apress
You'venow
2003completed
(928 pages) one more step toward understanding ADO.NET and its
components. After completing this extensive
This text provides chapter, you should
coverage of have a good
ADO.NET idea of how to write database
technology
applications usingincluding
the VS .NET IDE.
ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces
In this chapter, you and classes,
learned about and components
visual data how to integrate both
in VS with The Server Explorer is a handy
.NET.
ADO.NET.
utility added to VS .NET IDE to help you manage your database connections.

DataAdapters let you connect to a data source at design-time and populate DataSet objects. In
Table of Contents
addition, DataAdapters also allow you to add, update, and delete data through data command objects.
Applied
The VS ADO.NETBuilding Data-Driven
.NET IDE also lets Solutions
you generate typed DataSets, which are DataSets with properties of tables
Introduction
and columns specific to a data source.
Chapter 1 - ADO.NET Basics
ADataView
Chapter is a Components
2 - Data bindable view
in of a DataSet.
Visual You can sort and filter a DataSet with a DataView and
Studio .NET
use it to3 bind
Chapter to a graphical
- ADO.NET component
in Disconnected in many of the Windows form controls.
Environments
Chapter 4 - ADO.NET in Connected Environments
Finally, the Data Form Wizard is a useful tool for generating full-fledged database applications with
Chapter 5 - Handling ADO.NET Events
features such as inserting, deleting, and updating in no time.
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 -chapter,
In the next Data Binding and Windows
we discuss ADO.NETForms Data-Bound classes
disconnected Controls and components.
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Chapter Applied
3: ADO.NET in Disconnected
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot
Environments
ISBN:1590590732
Apress 2003 (928 pages)
Overview This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
In Chapter 1 and Chapter 2,and
interfaces, youtakes
learned about ADO.NET,
a comprehensive look and you learned how to use ADO.NET
at XML
components withnamespaces
Visual Studioand classes,
(VS) .NET.and how
You to learned
also integratehow
bothtowith
write code that binds ADO.NET
ADO.NET.
components to data-bound controls and how to use VS .NET's Integrated Development Environment
(IDE) wizards to write full-fledged database applications.
Table of Contents
In this chapter, you'll get a broad view of the ADO.NET architecture and the basic building blocks of
Applied ADO.NETBuilding Data-Driven Solutions
ADO.NET in a disconnected environment. Now, the first question on your mind might be, "What is a
Introduction
disconnected environment?" Generally, when we talk about ADO.NET and data access, we're talking
Chapter 1 - stored
about data ADO.NETin aBasics
database server such as a SQL Server or Oracle. Actually, ADO.NET not only
Chapter
provides2 a-way
DatatoComponents in Visual Studio
work with databases, it also.NET
allows you to access various kinds of data sources and even
Chapter 3 - representations
in-memory ADO.NET in Disconnected Environments
of data. In-memory data is stored in a system's memory and erased when an
Chapter 4 - is
application ADO.NET
closed. in Connected
Data Environments
that is held in-memory is referred to as a disconnected environment.
Chapter 5 - Handling ADO.NET Events
Why use
Chapter 6 in-memory data?
- Integrating XML Well, there may be occasions when you don't want to store data in a database
with ADO.NET
but you7want
Chapter to perform
- Data Binding some actionsForms
and Windows such as adding, deleting,
Data-Bound Controlssorting, and searching data, and once
you're done,
Chapter 8
you don't care what happens to the data. This scenario would be perfect for working with
- Constraints and Data Relations
disconnected data.
Chapter 9 - ADO.NET Exception Handling
Chapter 10 In
Note - Working
Chapter with
4, wethe
willODBC .NET Data
concentrate onProvider
working with connected data, where data will be stored in a
Chapter 11 database.
- Stored Procedures and Views
In the connected environment, you use a data provider to connect to a database and
Chapter 12 read
- Oracle,
and SQLXML,
write dataand Other
using the.NET
dataData Providers
provider classes.
Chapter 13 - Developing a Custom Data Provider
ADO.NET
Chapter 14 disconnected classes Web
- Developing Database are basic buildingusing
Applications blocks of the ADO.NET architecture. The classes are
ASP.NET
independent
Chapter of data
15 - Using providers
ADO.NET andWeb
in Xml dataServices
sources. In other words, you can use these classes with data
providers as well as without data providers.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter
ADO.NET 17 -data
Building Real-World
providers Web
are sets ofApplications
classes designed to work with multiple data sources to provide the
Chapter
best of 18
all -the
Object-Relational
database access Mapping in .NET ADO.NET provides many data providers to work with
technologies.
Chapter 19kinds
different - Mapped Objects:
of data Performance
sources, Considerations
including OleDb, ODBC,andandData
Sql.Binding
Chapter 20 - COM Interoperability and ADO.NET
Note
Chapter 21 You can access SQL Server 6.5 or previous versions using ODBC or OleDb data providers.
- Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Although ADO.NET is a new programming model, it still uses old native data access technologies to
Appendix A - Relational Databases: Some Basic Concepts
access different data sources. For example, OleDb data providers use the native OLE DB Application
Appendix B - Commonly Used SQL Statements
Programming Interface (API) to access OLE DB data sources. Similar to OLE DB data sources, ODBC
Appendix C - ADO.NET
data providers Frequently
use ODBC Asked
drivers Questions
and the ODBC Administration to access ODBC data sources. ADO.NET
Index
data providers wrap up these technologies for easier programming.
List of Figures
So,ofwhat's
List Tablesnew in ADO.NET? That is a good question. Weren't you using the ODBC and OLE DB APIs in
previous
List data access technologies? You're right. ADO.NET uses the same native API. The only difference
of Listings
is, of
List ADO.NET
Sidebars provides high-level, object-oriented classes that are easy to program and understand.
Further, all the data providers supply the same programming model. So, if you know how to write
applications using OleDb data providers, you'll be able to write applications using ODBC or Sql providers in
no time. It's just a matter of changing the class names and connection strings. When working with SQL
Server databases, the Sql data provider is much more efficient and faster in comparison to the OleDb or
ODBC data providers. Unlike ODBC and OleDb data providers, which use ODBC and OLE DB data layers
to connect to a SQL Server database, the Sql data provider uses the native SQL Server layer to connect to
the databases. We discuss this in more detail in later chapters.
Understanding
Appliedthe ADO.NET
ADO.NET: BuildingArchitecture
Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
You briefly lookedApress
at theADO.NET architecture in Chapter 1. Now you'll see a broad view of the ADO.NET
2003 (928 pages)
architecture and learn how disconnected
This text provides extensiveandcoverage
connected components
of ADO.NET (classes) provide access to multiple
technology
data sources. In this section, we talk about objects in general.
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Figure 3-1 showsnamespaces and classes,ofand
the basic architecture thehow to integrate
ADO.NET both
model. Aswith
you can see, the entire ADO.NET
ADO.NET.
model sits between the data source and client applications that can be built using Windows Forms, Web
Forms, or even console-based applications. The Connection is the first component that talks to a data
source. In ADO.NET, each data provider (OleDb, Sql, Odbc, or Oracle) has its own Connection class.
Table of Contents
TheConnection component is a mediator between a DataAdapter or a Command component. The
Applied ADO.NETBuilding Data-Driven Solutions
DataAdapter components create SQL INSERT,SELECT,UPDATE, and DELETE statements that add,
Introduction
read, update, and delete data from a data source, respectively. Not only does a DataAdapter create
Chapter 1 - ADO.NET Basics
these SQL statements, it also executes these statements. Basically, a DataAdapter (with the help of
Chapter 2 -physically
Command) Data Components
updatesinand
Visual Studio
reads data.NET
from a data source. In Figure 3-1, an arrow shows this flow
Chapter 3
of data. - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - 3-1:
Figure Relational Databases:
ADO.NET Some Basic Concepts
architecture
Appendix B - Commonly Used SQL Statements
You canCuse
Appendix the Command
- ADO.NET components
Frequently with or without a DataAdapter. You can directly execute
Asked Questions
commands and read data in a DataReader, which provides a read-only, forward-only fast access to data.
Index
This
List of is best when you need to read data in applications that are not data-bound. You can also see from
Figures
Figure
List 3-1 that all arrows are double-sided arrows except the arrow connecting a DataReader and
of Tables
Command.
List A double-sided arrow means data transfer is possible on both sides. A DataReader can only
of Listings
read data, which is why the DataReader has only a one-sided arrow. This shows that you can fill a
List of Sidebars
DataReader from the Command, but you can't send back data from a DataReader to the Command.

ADataAdapter sits between a data source and a DataSet. It provides Fill and Update methods to
fill a DataSet from a data source based on the SELECT statement, and the Update method saves a
DataSet's changes to the data source. The DataSet plays a vital role in data-bound Graphical User
Interface (GUI) applications to display and manipulate data. Not only does it provide fast data manipulation
using Extensible Markup Language (XML) schemas, it also provides multiple views of data that can be
bound with multiple Windows Forms and Web Forms data-bound controls.

ADataSet is a collection of DataTable components. A DataTable represents a table in a data source.


You can apply a filter or do sorts on a table. You can bind a DataTable to data-bound controls such as a
DataGrid,DataList,ListBox, or ComboBox using DataView. You can also apply sorts and filters on
aDataView. As you can see from Figure 3-1, there are three DataSets represented and three
Applied
DataTables. Each ADO.NET:
DataTable Building
binds Data-Driven
to different Solutions
data-bound controls using different DataViews.
by Mahesh Chand and David Talbot ISBN:1590590732
This section gaveApress
you an overview of the ADO.NET architecture. We discuss these controls in more detail
2003 (928 pages)
throughout this chapter and
This text Chapter
provides 4, depending
extensive on of
coverage their category.
ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Exploring the ADO.NET
Applied ClassData-Driven
ADO.NET: Building Hierarchy
Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Before you start swimming in the
Apress 2003 (928 deep
pages) ocean of ADO.NET, you need to take a quick look at the ADO.NET
class hierarchy provided by the .NET Runtime
This text provides extensive Class Library
coverage (alsotechnology
of ADO.NET known as Base Class Library, or BCL).
These classes represent ADO.NET components.
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces
All ADO.NET functionality and.NET
in the classes, and how
Runtime to integrate
Class Library isboth with in three general namespaces and
defined
ADO.NET.
a number of provider-specific namespaces. System.Data and System.Data.Common are two general
namespaces common to all data providers. The System.Data.SqlTypes namespace is a SQL Server-
specific namespace. The remainder of the functionality is defined in the data provider-specific
Table of Contents
namespaces; some of them are System.Data.OleDb,System.Data.ODBC, and
Applied ADO.NETBuilding Data-Driven Solutions
System.Data.SqlClient.
Introduction
Chapter 1 - ADO.NETnamespace
TheSystem.Data Basics defines classes that you can use with all the data providers or without
Chapter 2 - Data
data providers at Components in Visual Studio
all. This namespace .NET interfaces that are base classes for the data provider
also defines
classes.
Chapter 3Figure 3-2 shows
- ADO.NET the System.Data
in Disconnected namespace's class hierarchy.
Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List ofFigure
Tables 3-2: TheSystem.Data namespace class hierarchy
List of Listings
The
List ofSystem.Data.Common
Sidebars namespace defines classes common to all data providers. Figure 3-3 shows
theSystem.Data.Common namespace hierarchy.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Figure 3-3: TheSystem.Data.Common namespace hierarchy
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
TheSystem.Data.SqlTypes
Chapter 12 - Oracle, SQLXML, and namespace defines
Other .NET Data classes for native SQL Server data types that provide
Providers
type-safe
Chapter 13 conversion
- Developingbetween the
a Custom .NET
Data data types and the SQL Server native data types. Figure 3-4
Provider
shows the System.Data.SqlTypes namespace hierarchy.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 3-4: TheSystem.Data.SqlTypes namespace hierarchy

In addition to these three namespaces, there are a number of provider-specific namespaces. The class
hierarchy models of these namespaces are similar except for the name of the class. The classes defined
in the provider-specific namespaces start with the data provider name. For example, the Sql data provider
classes start withApplied
Sql, and ADO.NET:
the OleDb Building Data-Driven
data provider classesSolutions
start with OleDb. The Command object class
by Mahesh Chand and David Talbot
of the Sql data provider is SqlCommand, and the Command object ISBN:1590590732
class of the OleDb data provider is
OleDbCommand. Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including
In this section, you'll ADO.NET
see only internals, namespaces, namespace
the System.Data.OleDb classes, and class hierarchy because all the data
interfaces,
providers (including and takes a comprehensive
System.Data.Sql look at XML
and System.Data.Odbc) implement the same class hierarchy
namespaces and classes, and how to integrate both with
model (with minorADO.NET.
changes). Figure 3-5 shows the System.Data.OleDb namespace hierarchy.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Figure
Chapter 19 - 3-5: TheObjects:
Mapped System.Data.OleDb namespaceand
Performance Considerations hierarchy
Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
We discuss these namespaces and their classes in more detail throughout this chapter and Chapter 4.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Choosing aApplied
DataADO.NET:
Provider
Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
As you saw earlier, many
Apress data(928
2003 providers
pages) are available in ADO.NET. There may be cases when you can
access a data source
This text providesthan
using more one data
extensive provider.
coverage Now thetechnology
of ADO.NET question arises: Which one is better?
When choosing aincluding
data provider, the main criteria of selection is
ADO.NET internals, namespaces, classes, andperformance and multiple data source
connectivity. interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
In brief, to work with SQL Server 7 or later databases, the Sql data provider is the best choice. The Sql
data provider bypasses many layers and directly connects to the internal layer of the infrastructure. Not
only that, but the Sql data provider provides classes that help you to convert from native SQL Server data
Table of Contents
types to the .NET data types, and vice versa.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
OleDb data provider uses native OLE DB providers to access OLE DB data sources. OleDb data providers
Chapter 1 -because
are useful ADO.NETsome
Basicsdatabase providers don't implement ODBC drivers to connect to a database
Chapter
through2 ODBC.
- Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
The Odbc
Chapter 4 -data provider
ADO.NET provides aEnvironments
in Connected way to work with different kinds of data sources through ODBC drivers.
ODBC 5is an
Chapter older data
- Handling accessEvents
ADO.NET technology, but many applications still use ODBC to access data sources.
ODBC data
Chapter 6
providers provide a way to access ODBC data sources through ODBC drivers and ODBC
- Integrating XML with ADO.NET
Administration. Database venders generally provide the ODBC drivers. Using ODBC you can access any
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
data source for which you have an ODBC driver installed.
Chapter 8 - Constraints and Data Relations
Chapter 9 - Odbc
In general, ADO.NET
dataException
provider Handling
connectivity is faster than OLE-DB because of OLE DB's COM nature,
Chapter
which is10very
- Working with
"chatty." Sqlthe ODBC
data .NET is
provider Data Provider
faster than ODBC to work with SQL Server databases. But
Chapter
ODBC 11 - Stored
is useful whenProcedures
you needandto Views
write a generic class that can access multiple data sources through
ODBC.12
Chapter For- Oracle,
example, say you're
SQLXML, writing.NET
and Other an application that can work with multiple back-end servers
Data Providers
including
Chapter 13 SQL Server, Oracle,
- Developing Access,
a Custom and MySql. When you install these back-end servers, you also
Data Provider
install the
Chapter 14 ODBC driverDatabase
- Developing for theseWeb
databases. So, you
Applications can
using write a generic application that accesses these
ASP.NET
data sources
Chapter based
15 - Using on the in
ADO.NET ODBC DataServices
Xml Web Source Name (DSN). You can also pass the driver and data
source 16
Chapter information
- ASP.NETinServer
the application itself,
Controls and DatabutBinding
the user has to only create a DSN from ODBC, and the
application
Chapter 17 - can use Real-World
Building that DSN as theApplications
Web connection string in the application for the Odbc data provider.
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Understanding
AppliedADO.NET Disconnected
ADO.NET: Building Classes
Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
ADO.NET disconnected
Apress classes
2003 (928are the basic building blocks of the ADO.NET architecture. These classes
pages)
loosely couple with data providers. The coverage of namespace
System.Data
This text provides extensive defines these classes.
ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
The System.Data Namespace
namespaces and classes, and how to integrate both with
ADO.NET.
TheSystem.Data namespace consists of classes that are the basic building blocks of the ADO.NET
architecture. These classes are also known as disconnected classes because they store disconnected
Table
dataof Contents
and can work without data providers.
Applied ADO.NETBuilding Data-Driven Solutions
Table 3-1 describes some of the common classes of the System.Data namespaces. We discuss these
Introduction
classes1in -more
Chapter detailBasics
ADO.NET throughout this chapter.
Chapter 2 - Data Components in Visual Studio .NET
Table 3-1: The System.Data Namespace Classes
Chapter 3 - ADO.NET in Disconnected Environments
Chapter
CLASS 4 - ADO.NET in Connected Environments
DESCRIPTION
Chapter 5 - Handling ADO.NET Events
Constraint,
Chapter 6 Constraints are rules set on a database table and its
- Integrating XML with ADO.NET
ConstraintCollection, columns to help maintain the integrity of the data or to
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
UniqueConstraint, enforce business rules. The Constraint class object
Chapter 8 - Constraints and Data Relations
ForeignKeyConstraint represents a constraint that you can apply on a
Chapter 9 - ADO.NET Exception Handling
DataColumn object. Some of these constraints are
Chapter 10 - Working with the ODBC .NET Data Provider
primary key, uniqueness, and foreign key. The
Chapter 11 - Stored Procedures and ViewsConstraintCollection class represents a collection of
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
constraints for a DataTable.UniqueConstraintand
Chapter 13 - Developing a Custom Data Provider
ForeignKeyConstraint represent unique and foreign
Chapter 14 - Developing Database Web Applications using ASP.NET
key constraints.
Chapter 15 - Using ADO.NET in Xml Web Services
DataColumn, TheDataColumn object represents a column of a table.
Chapter 16 - ASP.NET Server Controls and Data Binding
DataColumnCollection DataColumnCollection represents a collection of
Chapter 17 - Building Real-World Web Applications
columns of a table.
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: PerformanceThe
DataRelation, DataRelation
Considerations object
and Data represents a parent/child
Binding
DataRelationCollection
Chapter relationship between two DataTable objects.
20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging DataRelationCollection represents a collection of
DataRelation.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational
DataRow, Databases: Some Basic
DataRowCollection Concepts
ADataRow object represents a row of a table, and
Appendix B - Commonly Used SQL Statements
DataRowCollection represent a collection of rows.
Appendix C - ADO.NET Frequently Asked Questions
DataRowView
Index
DataRowView represents a view of DataRow. It's useful
when you want to attach a DataRow with data-bound
List of Figures
controls such as a DataGrid.
List of Tables
ListDataSet
of Listings In ADO.NET, a DataSet object is a replacement of the
List of Sidebars ADO recordset and represents an in-memory cache of
data. A DataSet is a collection of DataTableobjects.
DataTable, ADataTable object represents an in-memory cache of a
DataTableCollection table, and DataTableCollection is a collection of one
or more DataTable objects.
DataView Represents a data-bindable, customized view of a
DataTable for sorting, filtering, searching, editing, and
navigation.
DataViewManager TheDefaultViewManager represents the default view of
aDataSet or DataTableCollection.
TheSystem.Data namespace also defines many enumerations and delegates that we discuss
Applied ADO.NET: Building Data-Driven Solutions
throughout this chapter.
by Mahesh Chand and David Talbot ISBN:1590590732
Once you have an idea
Apress of2003
what classes
(928 pages) the System.Data.Common namespace constaints, you'll have no
problem understanding how
This text to useextensive
provides these classes in aofsample
coverage application.
ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
The System.Data.Common Namespace
namespaces and classes, and how to integrate both with
ADO.NET.
As its name says, the System.Data.Common namespace contains classes shared by the .NET data
providers. Some of these classes are the base classes for the .NET data provider classes. Table 3-2
Table of Contents
defines some of these classes.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Table 3-2: The System.Data.Common Namespace Classes
Chapter 1 - ADO.NET Basics
CLASS
Chapter DESCRIPTION
2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
DataAdapter TheDataAdapter class represents a DataAdapter
Chapter 4 - ADO.NET in Connected Environments
that works as a bridge between a data source and
Chapter 5 - Handling ADO.NET Events DataSet. It implements the Fill and Update
Chapter 6 - Integrating XML with ADO.NET methods. The Fill method fills data from a data source
Chapter 7 toData-Bound
- Data Binding and Windows Forms a DataSetControls
using Command objects. This class is a
Chapter 8 - Constraints and Data Relations base class for the DbDataAdapter class, which itself is
Chapter 9 - ADO.NET Exception Handling a base class for the OleDbDataAdapter,
SqlDataAdapter,
Chapter 10 - Working with the ODBC .NET Data Provider and ODBC DataAdapter classes.
Chapter 11 - Stored Procedures and Views These classes are discussed in the "The DataAdapter:
Chapter 12 - Oracle, SQLXML, and Other .NETAdapting to Your Environment" section of Chapter 4.
Data Providers
Chapter 13 - Developing a Custom Data Provider
DataColumnMapping If you don't want to use the default column names of a
Chapter 14 - Developing Database Web Applications
table tousing ASP.NET
access these columns, you can define your own
Chapter 15 - Using ADO.NET in Xml Web Services
names. You map your custom names with the original
column
Chapter 16 - ASP.NET Server Controls and Data names through DataAdapters and use these
Binding
names in your application. The SourceColumn and
Chapter 17 - Building Real-World Web Applications
DataSetColumnproperties of this class represent the
Chapter 18 - Object-Relational Mapping in .NET
source column
Chapter 19 - Mapped Objects: Performance Considerations name
and Data and DataSet column name.
Binding
Chapter 20 - COM Interoperability and ADO.NET
DataColumnMappingCollection Collection of DataColumnMapping objects.
Chapter 21 - Messaging
22 - SQL Server and ADO.NET: NotesYou
DataTableMapping
Chapter can even map a database table to a DataTable
on Performance
and
Appendix A - Relational Databases: Some Basic use this DataTable as the source in your
Concepts
Appendix B - Commonly Used SQL Statements
applications. The ColumnMappings property returns
DataColumnMappingCollection for the
Appendix C - ADO.NET Frequently Asked Questions
DataTable. The DataSetTable and SourceTable
Index
properties represent the DataTable and source tables.
List of Figures
ListDataTableMappingCollection
of Tables Collection of DataTableMapping objects.
List of Listings
DbDataAdapter Inherited from DataAdapter, this class implements the
List of Sidebars
IdbDataAdapter interface. This class is used as the
base class of a data provider's DataAdapterclasses.
DBDataPermission Ensures that data providers have security to access
data. This class is a base class from the
OleDbDataPermission,OdbcDataPermission,
andSqlDataPermission classes.
Working with DataTables
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
ADataTable object Apressrepresents
2003 (928apages)
database table. A data table is a collection of columns and rows. The
DataRow object Thisrepresents a table row, andcoverage
text provides extensive the DataColumn
of ADO.NET object represents a column or a field of the
technology
table. Assuming you haven't imported, reengineered, or otherwise
including ADO.NET internals, namespaces, classes, and "inherited" a database and its
DataTables, theinterfaces,
first step and takes a with
to working comprehensive
these threelook at XML
objects is to create a DataTable schema, which is
namespaces and classes, and
defined by the DataColumnCollection how to
object. integrate
You use theboth
Addwithmethod of DataColumnCollection
ADO.NET.
to add columns to the collection. The Columns property of the DataTable object represents the
DataColumnCollection, which is a collection of DataColumn objects in a DataTable. You use a
DataRow
Table object to add data to a DataTable. The DataRowCollection object represents a collection
of Contents
of rows of a DataTable Data-Driven
Applied ADO.NETBuilding object, which can be accessed by its Rows property.
Solutions
Introduction
Figure 3-6 shows the relationship between the DataTable, the DataRow, and the DataColumn.
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Figure 3-6: Relationship between the DataTable, the DataRow, and the DataColumn
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
A database table schema is the structure or design of the table, which includes table columns, their types,
Chapter 21 - Messaging
relationships, and constraints. You can view a database schema from the Server Explorer as well as from
Chapter 22 - SQL
other places Server
such and
as the ADO.NET:
SQL Server Notes on Performance
Enterprise Manager or Visio. To view a table schema from the
Appendix A - Relational
Server Explorer, Databases:
simply right-clickSome BasicinConcepts
the table the Server Explorer and then click the Design Table menu
Appendix B - see
item. You'll Commonly Used SQL
the Column Name,Statements
Data Type, Length, and Allow Nulls columns. These columns
Appendix C the
represent - ADO.NET Frequently
name, data Asked Questions
type, length, and whether the column allows null values, respectively. The
Columns area represents the properties of a column; you can see and specify the description, default
Index
value,
List formula if any, and other properties of a column.
of Figures
List of Tables
Note A database table column is also called a field.
List of Listings
List
So,ofwhen
Sidebars
we talk about creating a DataTable, we mean building a database table (in memory, of
course) and its schema using the DataTable and the DataColumn objects. Based on a column
requirement, you can also apply constraints on a column. A constraint is a rule that restricts adding
unwanted data to a column. For example, the uniqueness of a column means that a column can't have
duplicate values. You'll see constraints in more detail in Chapter 8. After creating a data table schema, the
next step is to add DataRows to the DataTable. You use DataRows using the DataRow object. A
DataRow object represents a row in a table.

Tip To see data in the Customers table, double-click the Customers table in the Server Explorer.

OK, now you'll learn how to accomplish this task in ADO.NET using the DataTable,DataColumn, and
DataRow objects. First we discuss the DataColumn and the DataRow objects, followed by the
DataRelation and the DataTable objects. Then, you'll create a sample project containing all these
objects to build a DataTable and add data to it programmatically.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
The DataColumn
Apress 2003 (928 pages)

To understand a This text provides extensive coverage of ADO.NET technology


DataTable, you must first understand DataRows and DataColumns. As you can see
including ADO.NET internals, namespaces, classes, and
fromFigure 3-6, the DataColumnCollection type returns
interfaces, and takes a comprehensive look at aXML
collection of columns that you can access
through the Columns property
namespaces andof classes,
the DataTable.
and how to integrate both with
ADO.NET.
TheDataColumnCollection object represents a collection of columns attached to a DataTable. You
add a DataColumn to the DataColumnCollection using its Add method. The DataColumn object
Table of Contents
represents a column of a DataTable. For example, say you want to create a Customers table that
Applied ADO.NETBuilding
consists Data-Driven
of three columns: Solutions
ID, Address, and Name. So, you create three DataColumn objects and add
Introduction
these columns to the DataColumnCollection using the DataTable.Column.Add method.
Chapter 1 - ADO.NET Basics
TheDataColumn
Chapter has some properties.
2 - Data Components These
in Visual Studio properties describe a column, such as its uniqueness,
.NET
what kind of data you can store in that column,
Chapter 3 - ADO.NET in Disconnected Environments its default value, its caption, its name, and so on. Table 3-3
describes some of the DataColumn class
Chapter 4 - ADO.NET in Connected Environments
members.
Chapter 5 - Handling ADO.NET Events
Table 3-3: The DataColumn Class Properties
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and DESCRIPTION
PROPERTIES Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
AllowDBNull
Chapter 9 - ADO.NET ExceptionBoth read and write; represents whether the column can store null
Handling
values
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored ProceduresRepresents
AutoIncrement and Views whether column's value is auto increment
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
AutoIncrementSeed Starting value of auto increment, applicable when AutoIncrement is
Chapter 13 - Developing a Custom Data Provider
True
Chapter 14 - Developing Database Web Applications using ASP.NET
15 - Using ADO.NET inIndicates
AutoIncrementStep
Chapter Xml Web the increment value
Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Caption Caption of the column
Chapter 17 - Building Real-World Web Applications
18 - Object-Relational Represents
ColumnMapping
Chapter the MappingType of the column
Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
ColumnName Name of the column
Chapter 20 - COM Interoperability and ADO.NET
DataType
Chapter 21 - Messaging Data type stored by the column
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
DefaultValue Default value of the column
Appendix A - Relational Databases: Some Basic Concepts
Expression
Appendix Represents
B - Commonly Used SQL the expression used to filter rows, calculate values, and so
Statements
on Asked Questions
Appendix C - ADO.NET Frequently
Index
MaxLenght Represents maximum length of a text column
List of Figures
ReadOnly
List of Tables Represents if a column is read only
ListUnique
of Listings Indicates whether the values in a column must be unique
List of Sidebars

Creating a DataColumn
TheDataColumn class provides five overloaded constructors to create a DataColumn. By using these
constructors you can initialize a DataColumn with its name, data type, expressions, attributes, and any
combination of these.

This is the format for creating a DataColumn with no arguments:

public DataColumn()
For example:
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Dim dtColumn As DataColumn = New DataColumn()
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
This is the formatincluding ADO.NET internals, namespaces, classes, and
for creating a DataColumn with the column name:
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Public DataColumn(String)

Table of Contents
Applied ADO.NETBuilding
where String Data-Driven
is the column Solutions
name. For example:
Introduction
Chapter 1 - ADO.NET Basics
// Create Quantity Column
Chapter 2 - Data Components in Visual Studio .NET
DataColumn qtCol = new DataColumn("Quantity");
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
This is 5the-format
Chapter for ADO.NET
Handling creating aEvents
DataColumn with the column name and its type:
Chapter 6 - Integrating XML with ADO.NET
Chapter
Public 7 DataColumn(String,
- Data Binding and Windows Forms Data-Bound Controls
Type)
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
where String
Chapter is thewith
10 - Working column name.NET
the ODBC and Data is the column data type.
TypeProvider
Chapter 11 - Stored Procedures and Views
This is the format for creating a DataColumn with the column name, its type, and its expression:
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Public DataColumn(String, Type, String)
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 -String
where first ASP.NET is
Server Controls
the column and Data
name, TypeBinding
is the data type, and the second String is an expression.
Chapter 17 - Building Real-World Web Applications
For example:
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability
Dim myDataType and ADO.NET
As System.Type
myDataType
Chapter = System.Type.GetType("System.String")
21 - Messaging
Dim dtColumn
Chapter As DataColumn
22 - SQL Server and ADO.NET: = NewonDataColumn("Name",myDataType)
Notes Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
This is the format for creating a DataColumn with the column name, expression, and MappingType:
Appendix C - ADO.NET Frequently Asked Questions
Index
public DataColumn(String, Type, String, MappingType);
List of Figures
List of Tables
List of Listings
where String is the column name, Type is the data type, the second String is an expression, and
List of Sidebars is an attribute.
MappingType

In the following example, strExpr is an expression, which is the result of the Price and the Quantity
column multiplication:

// Creating an expression
string strExpr = "Price * Quantity";
// Create Total Column, which is result of Price*Quantity
DataColumn totCol = new DataColumn("Total", myDataType, strExpr,
MappingType.Attribute);
Applied ADO.NET: Building Data-Driven Solutions
Note As you can see from the previous code, the expression strExpr is a multiplication of the Price
by Mahesh Chand and David Talbot ISBN:1590590732
andQuantity columns. The Price and Quantity columns must exist in the table before you
Apress 2003 (928 pages)
use them in an expression. Otherwise, the compiler will throw an exception of "column not
This text provides extensive coverage of ADO.NET technology
found."including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Listing 3-1 summarizes all theand
namespaces constructors.
classes, andAs youtocan
how see, dcConstructorsTest
integrate both with creates the Price,
Quantity, and Total columns of a DataTable, which later is added to a DataSet. The DataSet
ADO.NET.
binds to a DataGrid using the SetDataBinding method.

Table of Contents
Listing 3-1: Creating Columns Using Different DataColumn Constructors
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Private Sub dcConstructorsTest()
Chapter 1 - ADO.NET Basics
' Create Customers table
ChapterDim2 -custTable
Data Components in Visual Studio
As DataTable = .NET
New DataTable("Customers")
ChapterDim3 -dtSet
ADO.NET Asin Disconnected
DataSet = Environments
New DataSet()
Chapter'4 Create
- ADO.NET in Connected
Price ColumnEnvironments
ChapterDim5 -myDataType
Handling ADO.NET As Events
System.Type
ChaptermyDataType
6 - Integrating
= XML with ADO.NET
System.Type.GetType("System.Int32")
ChapterDim7 -priceCol
Data BindingAs andDataColumn
Windows Forms= Data-Bound Controls
New DataColumn("Price", myDataType)
priceCol.Caption = "Price"
Chapter 8 - Constraints and Data Relations
ChaptercustTable.Columns.Add(priceCol)
9 - ADO.NET Exception Handling
Chapter'10 Create
- WorkingQuantity
with the ODBCColumn
.NET Data Provider
Chapter 11 - Stored Procedures and Views= New DataColumn()
Dim qtCol As DataColumn
ChapterqtCol.ColumnName
12 - Oracle, SQLXML, and = "Quantity"
Other .NET Data Providers
qtCol.DataType = System.Type.GetType("System.Int32")
Chapter 13 - Developing a Custom Data Provider
qtCol.Caption = "Quantity"
Chapter 14 - Developing Database Web Applications using ASP.NET
custTable.Columns.Add(qtCol)
Chapter 15 - Using ADO.NET in Xml Web Services
' Creating an expression
Chapter 16 - ASP.NET Server Controls and Data Binding
Dim strExpr As String = "Price * Quantity"
Chapter 17 - Building Real-World Web Applications
' Create Total Column, which is result of Price*Quantity
ChapterDim18 -totCol
Object-Relational Mapping in .NET
As DataColumn = New DataColumn("Total", myDataType, strExpr, _
Chapter 19 - Mapped Objects: Performance Considerations and Data BindingMappingType.Attribute)
ChaptertotCol.Caption
20 - COM Interoperability and ADO.NET
= "Total"
Chapter'21 - Messaging
Add Name column to the table.
ChaptercustTable.Columns.Add(totCol)
22 - SQL Server and ADO.NET: Notes on Performance
Appendix' AAdd
- Relational
custTable Databases: Some Basic Concepts
to DataSet
dtSet.Tables.Add(custTable)
Appendix B - Commonly Used SQL Statements
Appendix' CBind dataset
- ADO.NET to the
Frequently data
Asked grid
Questions
Index DataGrid1.SetDataBinding(dtSet, "Customers")
End Sub
List of Figures
List of Tables
List of Listings
List of Sidebars
To test this source code, you need to create a Windows application with a form and a DataGrid control
on it. After that you can call dcConstructorsTest from either Form_Load or the button click event
handler:

Private Sub Form1_Load(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles MyBase.Load
dcConstructorsTest()
End Sub

Setting DataColumn Properties


TheDataColumn class provides properties to set a column type, name, constraints, caption, and so on.
You already saw Applied ADO.NET: Building
a list of DataColumn Data-Driven
properties Solutions
in Table 3-3. You'll now learn how to use these
by Mahesh Chand and
properties to get and set their values. David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Listing 3-2 creates a column
This with aextensive
text provides name IDcoverage
and sets of
itsADO.NET
DataType, ReadOnly,AllowDBNull,Unique,
technology
AutoIncrement, including ADO.NET internals,and
AutoIncremetnSeed, namespaces, classes, and properties.
AutoIncrementStep
interfaces, and takes a comprehensive look at XML
namespaces
Listing 3-2: Creating and classes,
a DataColumn andand how toIts
Setting integrate both with
Properties
ADO.NET.

Dim IdCol As DataColumn = New DataColumn()


Table of Contents
IdCol.ColumnName = "ID"
Applied ADO.NETBuilding
IdCol.DataType Data-Driven Solutions
= Type.GetType("System.Int32")
Introduction
IdCol.ReadOnly = True
Chapter 1 - ADO.NET Basics
IdCol.AllowDBNull = False
Chapter 2 - Data Components
IdCol.Unique = True in Visual Studio .NET
IdCol.AutoIncrement
Chapter = True Environments
3 - ADO.NET in Disconnected
IdCol.AutoIncrementSeed
Chapter 4 - ADO.NET in Connected=Environments
1
IdCol.AutoIncrementStep
Chapter = 1
5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
As you can see from Listing 3-2, this code sets the AutoIncrement property as True along with the
Chapter 9 - ADO.NET Exception Handling
AutoIncrementSeed and AutoIncrementStep properties. The AutoIncrement property sets a
Chapter 10 - Working with the ODBC .NET Data Provider
column value as an auto number. When you add a new row to the table, the value of this column is
Chapter 11 automatically
assigned - Stored Procedures and Views
depending on the values of AutoIncrementStep and AutoIncrementSeed.
Chapter 12 - Oracle, SQLXML, and Other
The first value of the column starts with.NET Data Providers
AutoIncrementSeed, and the next value will be the previous
Chapter
column13 - Developing
value added toathe
Custom Data Provider
AutoIncrementStep. In this code, the ID number value starts with 1 and
Chapter 14 -byDeveloping
increases Database
1 if you add Webto
a new row Applications
the table. Ifusing
you ASP.NET
set the AutoIncrementStep value to 10, the
Chapter
value of15the
- Using ADO.NETcolumn
auto number in Xml Web Services by 10.
will increase
Chapter 16 - ASP.NET Server Controls and Data Binding
Having17
Chapter a primary key
- Building in a tableWeb
Real-World is a common practice to maintaining the data's integrity. A primary key in a
Applications
table is a unique key that identifies a DataRow. For example, in the Customers table, each customer
Chapter 18 - Object-Relational Mapping in .NET
should have a unique ID. So, it's always a good idea to apply a primary key constraint on the ID table. The
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
propertiesAllowDBNull as False and Unique as True set a key value as the primary key, and you
Chapter 20 - COM Interoperability and ADO.NET
use the PrimaryKey property of a DataTable to assign a DataTable's primary key. Listing 3-2 shows
Chapter 21 - Messaging
theAllowDBNull as False and the Unique property as True. Now you'll set DataTable's
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
PrimaryKey property as the ID column (see Listing 3-3).
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly
Listing 3-3: Setting a Used SQL Statements
DataColumn as the Primary Key
Appendix C - ADO.NET Frequently Asked Questions
Index
' Make the ID column the primary key column.
List
Dim of Figures
PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
List of Tables
PrimaryKeyColumns(0) = custTable.Columns("ID")
List of Listings
custTable.PrimaryKey = PrimaryKeyColumns
List of Sidebars

Adding a DataColumn to a DataTable


You add a DataColumn to a DataTable using the DataTable.Column.Add method. The Add
method takes one argument of the DataColumn type. Listing 3-4 creates two data columns, Id and
Name, and adds them to the DataTable custTable.

Listing 3-4: Creating the Id and Name Data Columns of the Customers Table

Dim custTable As DataTable = New DataTable("Customers")


Dim IdCol As DataColumn = New DataColumn()
Applied ADO.NET: Building Data-Driven Solutions
custTable.Columns.Add(IdCol)
by Mahesh
Dim nameCol As Chand and
DataColumn = David Talbot
New DataColumn() ISBN:1590590732
Apress 2003 (928
custTable.Columns.Add(nameCol) pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
Now you'll put allADO.NET.
the pieces together (see Listing 3-5). In Listing 3-5, you create a Customers table with
the columns ID, Name, Address, DOB (date of birth), and VAR where ID is a primary key. Name and
Address are string types. DOB is a date type field, and VAR is a Boolean type field.
Table of Contents
ListingADO.NETBuilding
Applied 3-5: Creating a Table UsingSolutions
Data-Driven DataTable and DataColumn
Introduction
Chapter
Private1 -Sub
ADO.NET Basics
CreateCustTable()
Chapter'2 Create
- Data Components in Visual Studio .NET
a new DataTable
ChapterDim
3 -custTable
ADO.NET in Disconnected
As DataTableEnvironments
= New DataTable("Customers")
Chapter'4 Create
- ADO.NET
ID inColumn
Connected Environments
ChapterDim
5 -IdCol
HandlingAs DataColumn
ADO.NET Events = New DataColumn()
ChapterIdCol.ColumnName = "ID"
6 - Integrating XML with ADO.NET
ChapterIdCol.DataType = Windows
7 - Data Binding and Type.GetType("System.Int32")
Forms Data-Bound Controls
ChapterIdCol.ReadOnly
8 - Constraints and=Data
TrueRelations
ChapterIdCol.AllowDBNull
9 - ADO.NET Exception =Handling
False
IdCol.Unique = True
Chapter 10 - Working with the ODBC .NET Data Provider
IdCol.AutoIncrement = True
Chapter 11 - Stored Procedures and Views
IdCol.AutoIncrementSeed = 1
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
IdCol.AutoIncrementStep = 1
Chapter 13 - Developing a Custom Data Provider
custTable.Columns.Add(IdCol)
Chapter 14 - Developing Database Web Applications using ASP.NET
' Create Name Column
ChapterDim15 -nameCol
Using ADO.NET in Xml Web Services
As DataColumn = New DataColumn()
ChapternameCol.ColumnName
16 - ASP.NET Server Controls and Data Binding
= "Name"
ChapternameCol.DataType
17 - Building Real-World= Web Applications
Type.GetType("System.String")
ChaptercustTable.Columns.Add(nameCol)
18 - Object-Relational Mapping in .NET
Chapter'19 - Mapped
Create Objects: Performance
Address Column Considerations and Data Binding
ChapterDim20 -addCol
COM Interoperability and ADO.NET
As DataColumn = New DataColumn()
ChapteraddCol.ColumnName
21 - Messaging = "Address"
ChapteraddCol.DataType = Type.GetType("System.String")
22 - SQL Server and ADO.NET: Notes on Performance
AppendixcustTable.Columns.Add(addCol)
A - Relational Databases: Some Basic Concepts
Appendix' BCreate DOB Used
- Commonly Column
SQL Statements
Dim dobCol As DataColumn
Appendix C - ADO.NET Frequently = New DataColumn()
Asked Questions
Index
dobCol.ColumnName = "DOB"
dobCol.DataType = Type.GetType("System.DateTime")
List of Figures
custTable.Columns.Add(dobCol)
List of Tables
' VAR Column
List of Listings
Dim fullTimeCol As DataColumn = New DataColumn()
List of Sidebars
fullTimeCol.ColumnName = "VAR"
fullTimeCol.DataType = Type.GetType("System.Boolean")
custTable.Columns.Add(fullTimeCol)
' Make the ID column the primary key column.
Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
PrimaryKeyColumns(0) = custTable.Columns("ID")
custTable.PrimaryKey = PrimaryKeyColumns
' Create a dataset
Dim ds As DataSet = New DataSet("Customers")
' Add Customers table to the dataset
ds.Tables.Add(custTable)
' Attach the dataset to a DataGrid
dataGrid1.DataSource = ds.DefaultViewManager
End Sub Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
Note To test including ADO.NET
this program, internals,
create namespaces,
a Windows classes,
application and a DataGrid control to the form.
and add
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
The output of Listing 3-5 looks like Figure 3-7, which shows a table with empty columns in a DataGrid
ADO.NET.
controlempty columns in a DataGrid control.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Figure 3-7: The DataGrid view of an empty DataTable
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
More 12
Chapter DataColumn Properties
- Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
You just saw some common properties of the DataColumn class, and you learned how to create and add
Chapter 14 - Developing Database Web Applications using ASP.NET
aDataColumn to a DataTable. But guess what? DataColumn isn't finished yet. It has more to offer.
Chapter 15 - Using ADO.NET in Xml Web Services
Now let's see the remaining properties of DataColumn.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World
TheColumnMapping property Web Applications represents how a DataColumn is mapped in a DataSet
of DataColumn
Chapter
and how18the
- Object-Relational Mapping
value of the column in .NET
will be written when a DataSet is saved in XML format using the
Chapter 19 - method
WriteXml Mapped Objects: Performance
of DataSet. Considerations and
The ColumnMapping Data
takes Binding
a value of MappingType enumeration.
TheMappingType
Chapter enumerationand
20 - COM Interoperability hasADO.NET
four values: Attribute,Element,Hidden, and
SimpleContent,
Chapter which are mapped to an XML attribute, element, internal structure, and XmlText
21 - Messaging
(respectively).
Chapter 22 - SQL Listing
Server3-6and
shows you how
ADO.NET: to use
Notes the ColumnMapping property.
on Performance
Appendix A - Relational Databases: Some Basic Concepts
Listing 3-6: Using the ColumnMapping Property of DataColumn
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Dim custTable As DataTable = New DataTable("Customers")
Index
Dim
List nameCol As DataColumn = New DataColumn("Name", _
of Figures
Type.GetType("System.Int32"),
List of Tables MappingType.Attribute)
nameCol.DataType
List of Listings = Type.GetType("System.String")
nameCol.ColumnMapping = MappingType.Element
List of Sidebars
custTable.Columns.Add(nameCol)
DataGrid1.DataSource = custTable

So far you saw how to create a new table column, set its various properties, and add it to a table. But what
if you wanted to store some custom information about a column, such as a description? Guess what? You
can do it. The Extended-Properties property of DataColumn lets you read and write custom
properties about a DataColumn. The ExtendedProperties property takes a value of type
PropertyCollection, which is inherited from the HashTable class. A hash table is a table that stores
data in a key/value pair. The PropertyCollection class defines methods to add, clear, and remover
properties to a collection. Some of the PropertyCollection class properties are Count,
IsFixedSize,IsReadOnly,IsSynchronized,Item,Keys, and Values.Listing 3-7 creates a
column and addsApplied ADO.NET:
four custom Building
properties to it. Data-Driven
After that, theSolutions
code removes the Description property
by Mahesh Chand and David Talbot ISBN:1590590732
and displays the rest of the properties using the ExtendedProperties property of DataColumn.
Apress 2003 (928 pages)
Listing 3-7: Adding
This and
text Removing Custom
provides extensive Properties
coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Dim custTablenamespaces
As DataTable = New
and classes, DataTable("Customers")
and how to integrate both with
ADO.NET.
Dim nameCol As DataColumn = New DataColumn("Name", _
Type.GetType("System.Int32"), MappingType.Attribute)
' Add custom properties
Table of Contents
nameCol.DataType = Type.GetType("System.String")
Applied ADO.NETBuilding Data-Driven Solutions
nameCol.ExtendedProperties.Add("Description", "The Name Column")
Introduction
nameCol.ExtendedProperties.Add("Author", "Mahesh Chand")
Chapter 1 - ADO.NET Basics
nameCol.ExtendedProperties.Add("UserId", "MCB")
Chapter 2 - Data Components in Visual Studio .NET
nameCol.ExtendedProperties.Add("PWD", "Password")
Chapter 3 - ADO.NET in Disconnected Environments
custTable.Columns.Add(nameCol)
Chapter
' Remove4 - ADO.NET
Author inproperty
Connected Environments
Chapter 5 - Handling ADO.NET Events
nameCol.ExtendedProperties.Remove("Author")
Chapter
' Read 6 custom
- Integrating XML with ADO.NET
properties
Dim str
Chapter 7 -AsDataString
Binding and Windows Forms Data-Bound Controls
Dim i8 As- Constraints
Chapter Integer and Data Relations
str = nameCol.ExtendedProperties("Description").ToString()
Chapter 9 - ADO.NET Exception Handling
str =10str
Chapter + ", with
- Working " +the
nameCol.ExtendedProperties("UserId").ToString()
ODBC .NET Data Provider
str = str + ", " + nameCol.ExtendedProperties("PWD").ToString()
Chapter 11 - Stored Procedures and Views
MessageBox.Show(str)
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using
TheOrdinal ADO.NET
property in only)
(read Xml Web Services
of DataColumn returns the position of a column in a
Chapter 16 - ASP.NET Server Controls
DataColumnCollection, and the MaxLengthand Data Binding
(get and set both types) property represents the
Chapter
maximum17 -length
Building
of Real-World Web Applications
a column. Listing 3-8 shows how to use the Ordinal and MaxLength properties.
Chapter 18 - Object-Relational Mapping in .NET
Listing19
Chapter 3-8: Using the
- Mapped Ordinal
Objects: and MaxLength
Performance Properties
Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Dim custTable As DataTable = New DataTable("Customers")
Chapter 22 - SQL Server
Dim nameCol and ADO.NET:= Notes
As DataColumn New on Performance
DataColumn("Name", _
Appendix A - Relational Databases: Some Basic
Type.GetType("System.Int32"), Concepts
MappingType.Attribute)
Appendix B - Commonly Used SQL Statements
custTable.Columns.Add(nameCol)
nameCol
Appendix C -=ADO.NET
New DataColumn()
Frequently Asked Questions
nameCol.DataType = Type.GetType("System.String")
Index
nameCol.Caption
List of Figures = "New Column"
nameCol.ColumnName
List of Tables = "Col2"
nameCol.MaxLength
List of Listings = 240
custTable.Columns.Add(nameCol)
List of Sidebars
Dim str As String
str = "Ordinal " + nameCol.Ordinal.ToString()
str = str + " ,Length " + nameCol.MaxLength.ToString()
MessageBox.Show(str)

The DataRow
ADataRow represents a row of data in a DataTable. You add data to the DataTable using the
DataRow object. A DataRowCollection object represents a collection of DataRows of a DataTable.
You use the DataTable'sNewRow method to return a DataRow object, add values to the DataRow, and
add the row to theApplied ADO.NET:
DataTable againBuilding
by usingData-Driven Solutions Add method.
DataRowCollection's
by Mahesh Chand and David Talbot ISBN:1590590732
Table 3-4 describes
Apress
DataRow class
2003 (928 properties, and Table 3-5 describes the methods.
pages)
This text provides extensive coverage of ADO.NET technology
including Class
Table 3-4: The DataRow ADO.NET internals, namespaces, classes, and
Properties
interfaces, and takes a comprehensive look at XML
PROPERTY namespaces
DESCRIPTION and classes, and how to integrate both with
ADO.NET.
Item Represents an item of a row

of Contents Represents all values in a row


ItemArray
Table
Applied ADO.NETBuilding Data-Driven Solutions
RowState Indicates the current state of a row
Introduction
Table
Chapter 1 Returns
- ADO.NET Basicsthe DataTable to which this row is attached
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Table 3-5:
Chapter 4 - The DataRow
ADO.NET Class Methods
in Connected Environments
Chapter
METHOD5 - Handling ADO.NET Events
DESCRIPTION
Chapter 6 - Integrating XML with ADO.NET
7 - Data BindingCommits
AcceptChanges
Chapter all theForms
and Windows changes made toControls
Data-Bound this row
Chapter 8 - Constraints and Data Relations
BeginEdit Starts an edit operation on a row
Chapter 9 - ADO.NET Exception Handling
CancelEdit
Chapter Cancels
10 - Working with the.NET
the ODBC current
Dataedit on a row
Provider
Chapter
Delete11 - Stored Procedures
Deletesand Views
a DataRow
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
EndEdit
Chapter Ends
13 - Developing a the current
Custom edit on a row
Data Provider
Chapter 14 - Developing Database
GetChildRows Web rows
Returns child Applications using ASP.NET
of a DataRow
Chapter 15 - Using ADO.NET in Xml Web Services
GetParentRows Returns parent rows of a DataRow.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World
RejectChanges Rejects Web Applications
all the changes made since last AcceptChanges
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
You access
Chapter DataRow
20 - COM membersand
Interoperability through the DataTable columns. A column acts as an item of the row.
ADO.NET
For example,
Chapter if a DataTable has three columns such as Id, Name, and Address, then a row will have
21 - Messaging
three members: Id, Name, and Address. You access DataTable members using the column names. For
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
example,Listing 3-9 sets values of the Id, Name, and Address columns.
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly
Listing 3-9: Used
Setting the SQL Statements
Values of the Id, Address, and Name Columns of a DataRow
Appendix C - ADO.NET Frequently Asked Questions
Index
Dim row1 As DataRow = custTable.NewRow()
List of Figures
row1("id") = 1001
List of Tables
row1("Address") = "43 Lanewood Road, Cito, CA"
List of Listings
row1("Name") = "George Bishop "
List of Sidebars

After setting a row member's values, you add the row to the row collection with the
DataTable.Rows.Add method. The following code adds a row to the collection:

custTable.Rows.Add(row1)

TheRejectChanges method of the DataRow rejects recent changes on that row. For example, if you
have recently added row1 to the DataTable, then calling the RejectChanges method as follows:
row1.RejectChanges()
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
won't add the rowApress
to theDataTable.
2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
You can also delete a rowADO.NET
including from a DataTable by calling the
internals, namespaces, classes, and Delete method:
DataRow's
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
Row1.Delete()ADO.NET.

Table of Contents
Caution
Applied TheRejectChanges
ADO.NETBuilding Data-Drivenand Delete methods may not work together if you're applying both
Solutions
Introduction methods on the same row because RejectChanges doesn't add a row to the DataTable.
Chapter 1 - ADO.NET Basics
Listing 3-10 shows a program that creates a DataTable with three columns (Id, Name, and Address) and
Chapter 2 - Data Components in Visual Studio .NET
adds three rows to the DataTable. At the end, this program attaches the newly created DataTable to a
Chapter 3 - ADO.NET in Disconnected Environments
DataGrid control using a DataSet.
Chapter 4 - ADO.NET in Connected Environments
Chapter
Listing53-10:
- Handling
AddingADO.NET
Rows toEvents
a DataTable Using DataRow
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
' This method creates Customers table
Chapter 8 - Constraints
Private and Data Relations
Sub CreateCustomersTable()
Chapter'9 Create
- ADO.NET Exception Handling
a new DataTable.
ChapterDim10 -custTable
Working with As
the ODBC .NET Data Provider
System.Data.DataTable = New DataTable("Customers")
ChapterDim11 -dtColumn
Stored Procedures and Views
As DataColumn
Chapter'12 - Oracle,id
Create SQLXML, and Other .NET Data Providers
Column.
ChapterdtColumn
13 - Developing
= Newa Custom Data Provider
DataColumn()
ChapterdtColumn.DataType
14 - Developing Database = Web
System.Type.GetType("System.Int32")
Applications using ASP.NET
ChapterdtColumn.ColumnName
15 - Using ADO.NET in Xml = Web"id"
Services
ChapterdtColumn.Caption = "Cust
16 - ASP.NET Server Controls and ID"
Data Binding
ChapterdtColumn.ReadOnly
17 - Building Real-World=Web True
Applications
ChapterdtColumn.Unique
18 - Object-Relational=Mapping
True in .NET
' Add id Column to the DataColumnCollection.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
custTable.Columns.Add(dtColumn)
Chapter 20 - COM Interoperability and ADO.NET
' Create Name column.
Chapter 21 - Messaging
dtColumn = New DataColumn()
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
dtColumn.DataType = System.Type.GetType("System.String")
Appendix A - Relational Databases: Some Basic Concepts
dtColumn.ColumnName = "Name"
Appendix B - Commonly Used SQL
dtColumn.Caption Statements
= "Cust Name"
AppendixdtColumn.AutoIncrement =Questions
C - ADO.NET Frequently Asked False
Index dtColumn.ReadOnly = False
List of Figures
dtColumn.Unique = False
List of Tables
' Add Name column to the table.
List of Listings
custTable.Columns.Add(dtColumn)
' Create Address column.
List of Sidebars
dtColumn = New DataColumn()
dtColumn.DataType = System.Type.GetType("System.String")
dtColumn.ColumnName = "Address"
dtColumn.Caption = "Address"
dtColumn.ReadOnly = False
dtColumn.Unique = False
' Add Address column to the table.
custTable.Columns.Add(dtColumn)
' Make the ID column the primary key column.
Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
PrimaryKeyColumns(0) = custTable.Columns("id")
custTable.PrimaryKey = PrimaryKeyColumns
Appliedthe
' Instantiate ADO.NET:
DataSet Building Data-Driven Solutions
variable.
by Mahesh Chand and David
Dim ds As DataSet = New DataSet("Customers")Talbot ISBN:1590590732
Apress 2003 (928 pages)
' Add the custTable to the DataSet.
This text provides extensive coverage of ADO.NET technology
ds.Tables.Add(custTable)
' Add rows including ADO.NET
to the internals,using
custTable namespaces,
its classes,
NewRowand method
interfaces, and takes a comprehensive look at XML
' I add three customers
namespaces and classes, with thier
and how addresses,
to integrate name and id
both with
Dim row1 ADO.NET.
As DataRow = custTable.NewRow()
row1("id") = 1001
row1("Address") = "43 Lanewood Road, Cito, CA"
Table of Contents
row1("Name") = "George Bishop "
AppliedcustTable.Rows.Add(row1)
ADO.NETBuilding Data-Driven Solutions
Introduction
Dim row2 As DataRow = custTable.NewRow()
Chapterrow2("id")
1 - ADO.NET = Basics
1002
Chapterrow2("Name")
2 - Data Components
= "Rock in Visual
JoeStudio
" .NET
Chapterrow2("Address") = "KingEnvironments
3 - ADO.NET in Disconnected of Prusssia, PA"
ChaptercustTable.Rows.Add(row2)
4 - ADO.NET in Connected Environments
ChapterDim
5 -row3 AsADO.NET
Handling DataRow = custTable.NewRow()
Events
Chapterrow3("id")
6 - Integrating= XML
1003 with ADO.NET
Chapterrow3("Name")
7 - Data Binding=and "Miranda
Windows Forms " Data-Bound Controls
row3("Address") = "279
Chapter 8 - Constraints and Data Relations
P. Avenue, Bridgetown, PA"
custTable.Rows.Add(row3)
Chapter 9 - ADO.NET Exception Handling
'row3.RejectChanges()
Chapter 10 - Working with the ODBC .NET Data Provider
'row2.Delete()
Chapter 11 - Stored Procedures and Views
' Bind dataset to the data grid
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
dataGrid1.DataSource = ds.DefaultViewManager
Chapter
End Sub13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Caution
Chapter As youReal-World
17 - Building can see from
WebListing 3-10, the Id column of the Customers table is read only (the
Applications
ReadOnly property
Chapter 18 - Object-Relational is True).
Mapping in .NETThat means you won't be able to add data to the table. If you
want toObjects:
Chapter 19 - Mapped add data from the front
Performance end, you need
Considerations and to setBinding
Data the ReadOnly property to False.
Chapter 20 - COM Interoperability and ADO.NET
The output of the program looks like Figure 3-8.
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 3-8: ADataTable with three rows

Note To run this program, create a Windows application and drop a DataGrid control on the form.
After that you can either call CreateCustomersTable in Listing 3-3 from the form's Load
event or from a button click handler.You can write a Load event by double-clicking on the form
or by opening the Properties window.

If you uncomment row3.RejectChanges() and row2.Delete() at the end of Listing 3-10, the
RejectChanges method rejects the addition of row 3, and the Delete method deletes row 2.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh
Listing 3-11: Calling Chand RejectChanges
DataRow's and David Talbot and Delete Methods
ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
row3.RejectChanges()
including ADO.NET internals, namespaces, classes, and
row2.Delete()interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

TheDataRowState enumeration returns the current state of a row. It's useful during operations when
Table of Contents
you want to know the current state of a row. Table 3-6 lists the values of the DataRowState
Applied ADO.NETBuilding Data-Driven Solutions
enumeration.
Introduction
Chapter 1 - The
Table 3-6: ADO.NET Basics
DataRowState Enumeration Members
Chapter 2 - Data Components in Visual Studio .NET
MEMBER
Chapter DESCRIPTION
3 - ADO.NET in Disconnected Environments
Chapter
Added 4 - ADO.NET
Rowinhas
Connected Environments
been added, and AcceptChanges has not been called.
Chapter 5 - Handling ADO.NET Events
Deleted
Chapter Row was
6 - Integrating XML deleted using the Delete method.
with ADO.NET
Chapter 7 - Data Binding
Detached Row wasandcreated
WindowsbutForms Data-Bound
deleted Controls
before it was added to the collection.
Chapter 8 - Constraints and Data Relations
Modified
Chapter RowException
9 - ADO.NET has beenHandling
modified, but AcceptChanges has not been called yet.
Chapter 10 - Working
Unchanged with
Row the
has ODBC
not .NET Data
changed sinceProvider
the last AcceptChanges was called.
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
TheRowState
Chapter propertya of
13 - Developing a DataRow
Custom returns the DataRowState enumeration. You can use this to find
Data Provider
out the14
Chapter current state of Database
- Developing a row. ForWeb
example, Listing
Applications 3-12ASP.NET
using calls RowState just after the Delete and
RejectChanges
Chapter methods.in Xml Web Services
15 - Using ADO.NET
Chapter 16 - ASP.NET Server Controls and Data Binding
Listing 3-12: Calling the RowState Property
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
row3.RejectChanges()
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
MessageBox.Show(row2.RowState.ToString())
Chapter 20 - COM Interoperability and ADO.NET
row2.Delete()
Chapter 21 - Messaging
MessageBox.Show(row3.RowState.ToString())
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
The DataRelation
Index
List of Figures
To provide data integrity and consistency, you should use relationships between two tables. You achieve
List of Tables
this relationship by defining a primary key in one table and using a foreign key in the other table. Say a
List of Listings
customer has multiple orders; the Customers table stores the customer details, and the Orders table
List of Sidebars
stores all the order details. To avoid the redundancy of data, you define the primary key of the Customers
table as a foreign key in the Orders table.

Note In general this relationship is called the parent/child,ormaster/details, relationship.

In this example, the Customers table is also the parent table, and the Orders table is also the child table.
TheParentRelations property of DataTable represents the parent relationship, and
ChildRelations represents the child relationship.

Caution The data type of both columns, which you're linking through a relationship in the Customers
and the Orders tables, must be identical.

You can also access this relationship though a DataSet using its Relations property. To create a
relationship between two columns, you create two DataColumn objects and pass them as
DataRelation Applied ADO.NET: Building Data-Driven Solutions
arguments.
by Mahesh Chand and David Talbot ISBN:1590590732
Apress
Listing 3-13 shows you how to(928
2003 create a customer/order relationship between the Customers and Orders
pages)
table through theThis
Customers table's
text provides id column,
extensive referenced
coverage as CustId
of ADO.NET in the Orders table. The
technology
DataRelation including ADO.NET
constructor internals,
takes three namespaces,
arguments: classes,
the name and relationship, the first DataColumn,
of the
interfaces, andAfter
and the second DataColumn. takesthat
a comprehensive look at XML
you call DataTable's ParentRelation.Add method with
namespaces and classes, and how to integrate both with
as an argument. (Listing 3-15 shows the full source code of this example.) In this code,
DataRelation ADO.NET.
dtSet is a DataSet object, which we discuss in the following section.

Listing
Table 3-13: Creating a Customer/Order Relationship Using DataRelation
of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Private Sub BindData()
ChapterDim
1 -dtRelation
ADO.NET BasicsAs DataRelation
ChapterDim
2 -CustCol
Data Components in Visual Studio
As DataColumn .NET
= dtSet.Tables("Customers").Columns("id")
ChapterDim
3 -orderCol
ADO.NET in As DataColumn
Disconnected = dtSet.Tables("Orders").Columns("CustId")
Environments
ChapterdtRelation
4 - ADO.NET =
in New DataRelation("CustOrderRelation",
Connected Environments CustCol, orderCol)
ChapterdtSet.Tables("Orders").ParentRelations.Add(dtRelation)
5 - Handling ADO.NET Events
ChapterdataGrid1.SetDataBinding(dtSet,"Customers")
6 - Integrating XML with ADO.NET
End Sub
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
The DataTable
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
In the previous
Chapter sectionsa you've
13 - Developing Customseen
Datathat columns and rows are the building blocks of a DataTable. You
Provider
need to work with the DataColumn and DataRow objects to create DataTables and add data to them.
Chapter 14 - Developing Database Web Applications using ASP.NET
Besides creating a DataTable schema and adding rows to it, a DataTable has more to offer. The
Chapter 15 - Using ADO.NET in Xml Web Services
DataTable object represents a DataTable.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 -take
First you'll Building Real-World
a look Web Applications
at the DataTable class properties and methods. Table 3-7 describes some
Chapter
common 18DataTable
- Object-Relational Mapping
properties, andin .NET3-8 summarizes some of the common DataTable methods.
Table
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - The
Table 3-7: COMDataTable
Interoperability
ClassandProperties
ADO.NET
Chapter 21 - Messaging
PROPERTIES DESCRIPTION
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix
ColumnsA - Relational Databases: Some
Represents allBasic
tableConcepts
columns
Appendix B - Commonly Used SQL Statements
Constraints Represents all table constaints
Appendix C - ADO.NET Frequently Asked Questions
Index
DataSet Returns the DataSet for the table
List of Figures
DefaultView Customized view of the DataTable
List of Tables
ListChildRelation
of Listings Returns child relations for the DataTable
List of Sidebars
ParentRelation Returns parent relations for the DataTable
PrimaryKey Represents an array of columns that function as primary key for the table
Rows All rows of the DataTable
TableName Name of the table

Table 3-8: The DataTable Class Methods


METHOD DESCRIPTION
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
AcceptChanges Commits all the changes made since the last AcceptChanges was called
Apress 2003 (928 pages)
Clear Deletes
This text all DataTable
provides data of ADO.NET technology
extensive coverage
including ADO.NET internals, namespaces, classes, and
Clone Creates
interfaces, and a clone
takes of a datatable including
a comprehensive its schema
look at XML
namespaces and classes, and how to integrate both with
Copy Copies a DataTable including its schema
ADO.NET.

NewRow Creates a new row, which is later added by calling the Rows.Addmethod
Table of Contents
RejectChanges Rejects all changes made after the last AcceptChanges was called
Applied ADO.NETBuilding Data-Driven Solutions
Reset
Introduction Resets a DataTable's original state
Chapter
Select1 - ADO.NET Basics
Gets an array of rows based on the criteria
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
TheDataTable
Chapter classinprovides
4 - ADO.NET methods
Connected and properties to remove, copy, and clone data tables. Not only
Environments
that, but
Chapter 5 you can alsoADO.NET
- Handling apply filters on a DataTable. The Constraints property provides access to all
Events
the constraints
Chapter that a DataTable
6 - Integrating has. You can also access the child/parent relationship using
XML with ADO.NET
ChildRelation
Chapter and ParentRelation.
7 - Data Binding and Windows FormsNow we'll create
Data-Bound two tables-Customers and Orders-and set a
Controls
relationship
Chapter between them.
8 - Constraints To test
and Data this application, you build a Windows application using Visual Basic and
Relations
add a DataGrid control to the form. After that you call the CreateCustomersTable,
Chapter 9 - ADO.NET Exception Handling
CreateOrdersTable, and BindData methods from the form constructor after
Chapter 10 - Working with the ODBC .NET Data Provider
InitializeComonent. The form constructor looks like Listing 3-14.
Chapter 11 - Stored Procedures and Views
Chapter
Listing12 - Oracle,
3-14: Form'sSQLXML, and Other
Constructor .NET Data
Calling Providers
CreateCustomersTable, CreateOrdersTable, and
Chapter 13 - Developing a Custom Data Provider
BindData
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Private Sub Form1_Load(ByVal sender As System.Object, _
Chapter 16 - ASP.NET Server Controls and Data Binding
ByVal e As System.EventArgs) Handles MyBase.Load
ChapterCreateCustomersTable()
17 - Building Real-World Web Applications
ChapterCreateOrdersTable()
18 - Object-Relational Mapping in .NET
ChapterBindData()
19 - Mapped Objects: Performance Considerations and Data Binding
Chapter
End Sub20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
You alsoBneed
Appendix to add aUsed
- Commonly DataSet variable, dtSet, in the beginning of your form:
SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Public Class Form1
Index
Inherits
List of Figures System.Windows.Forms.Form
Private
List of Tables dtSet As System.Data.DataSet
List of Listings
List of Sidebars
In Listing 3-15, the CreateCustomersTable method creates the Customers data table with Id, Name,
and Address columns and adds three data rows to it. The CreateOrdersTable method creates the
Orders table with OrderId, CustId, Name, and Description columns and adds data to it. The BindData
method creates a customer/orders relationship and binds the data to a DataGrid control using
DataSet.

Listing 3-15: Customer/Orders Relationship Example

Private Sub CreateCustomersTable()


' Create a new DataTable.
Dim custTable As System.Data.DataTable = New DataTable("Customers")
' Create id, Name, and Address Columns and
' add these columns to custTable
' See source Applied ADO.NET:
code Building Data-Driven Solutions
for details
' Make the ID column theDavid
by Mahesh Chand and Talbot key column.
primary ISBN:1590590732
Apress 2003 (928 pages)
Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
This text provides extensive
PrimaryKeyColumns(0) coverage of ADO.NET technology
= custTable.Columns("id")
including ADO.NET internals,
custTable.PrimaryKey = PrimaryKeyColumns namespaces, classes, and
interfaces, and takes a comprehensive look at XML
' Instantiate the and
namespaces DataSet variable.
classes, and how to integrate both with
dtSet = New DataSet("Customers")
ADO.NET.
' Add the custTable to the DataSet.
dtSet.Tables.Add(custTable)
Table of ' Contents
Add rows to the custTable using its NewRow method
Applied'ADO.NETBuilding Data-Driven Solutions
I add three customers with thier addresses, name and id
Introduction
' See source code for details
Chapter
End1 Sub - ADO.NET Basics
Chapter 2 - Data
' This Components
method creates in Visual
Orders Studio .NET with
table
Private
Chapter Sub CreateOrdersTable()
3 - ADO.NET in Disconnected Environments
Chapter'4 Create
- ADO.NETOrders table.
in Connected Environments
ChapterDim5 -ordersTable
Handling ADO.NET AsEvents
DataTable = New DataTable("Orders")
Chapter'6 Create OrderId,
- Integrating XML with Name,
ADO.NET CustId, Description columns
Chapter'7 Add them
- Data to and
Binding ordersTable
Windows Forms Data-Bound Controls
' See source code for more details
Chapter 8 - Constraints and Data Relations
' Add ordersTable to the dataset
Chapter 9 - ADO.NET Exception Handling
dtSet.Tables.Add(ordersTable)
Chapter 10 - Working with the ODBC .NET Data Provider
' Add two rows to Customer Id 1001
Chapter 11 - Stored Procedures and Views
dtRow = ordersTable.NewRow()
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
dtRow("OrderId") = 0
ChapterdtRow("Name")
13 - Developing a Custom
= "ASPData Book"Provider
ChapterdtRow("CustId")
14 - Developing Database= 1001 Web Applications using ASP.NET
ChapterdtRow("Description")
15 - Using ADO.NET in Xml Web Services
= "Same Day"
ChapterordersTable.Rows.Add(dtRow)
16 - ASP.NET Server Controls and Data Binding
ChapterdtRow
17 - Building Real-World Web Applications
= ordersTable.NewRow()
ChapterdtRow("OrderId")
18 - Object-Relational Mapping
= 1 in .NET
ChapterdtRow("Name")
19 - Mapped Objects: = "C# Book" Considerations and Data Binding
Performance
ChapterdtRow("CustId") = 1001
20 - COM Interoperability and ADO.NET
ChapterdtRow("Description")
21 - Messaging = "2 Day Air"
ChapterordersTable.Rows.Add(dtRow)
22 - SQL Server and ADO.NET: Notes on Performance
Appendix' AAdd two rows
- Relational to Customer
Databases: Some Basic Id 1002
Concepts
dtRow = ordersTable.NewRow()
Appendix B - Commonly Used SQL Statements
dtRow("OrderId") = 2
Appendix C - ADO.NET Frequently Asked Questions
dtRow("Name") = "Data Quest"
Index
dtRow("Description") = "Monthly Magazine"
List of Figures
dtRow("CustId") = 1002
List of Tables
ordersTable.Rows.Add(dtRow)
List of Listings
dtRow = ordersTable.NewRow()
List of Sidebars
dtRow("OrderId") = 3
dtRow("Name") = "PC Magazine"
dtRow("Description") = "Monthly Magazine"
dtRow("CustId") = 1002
ordersTable.Rows.Add(dtRow)
' Add two rows to Customer Id 1003
dtRow = ordersTable.NewRow()
dtRow("OrderId") = 4
dtRow("Name") = "PC Magazine"
dtRow("Description") = "Monthly Magazine"
dtRow("CustId") = 1003
ordersTable.Rows.Add(dtRow)
dtRow = ordersTable.NewRow()
Applied ADO.NET:
dtRow("OrderId") = 5 Building Data-Driven Solutions
by Mahesh Chand
dtRow("Name") = "C# Book" and David Talbot ISBN:1590590732
Apress 2003
dtRow("CustId") = 1003 (928 pages)
This text provides extensive
dtRow("Description") = "2 Day coverage
Air" of ADO.NET technology
including ADO.NET
ordersTable.Rows.Add(dtRow) internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
End Sub namespaces and classes, and how to integrate both with
ADO.NET.
' This method creates a customer order relationship and binds data tables
' to the data grid cotnrol using dataset.
TablePrivate
of Contents
Sub BindData()
AppliedDim
ADO.NETBuilding
dtRelation Data-Driven Solutions
As DataRelation
Introduction
Dim CustCol As DataColumn = dtSet.Tables("Customers").Columns("id")
ChapterDim
1 -orderCol
ADO.NET Basics
As DataColumn = dtSet.Tables("Orders").Columns("CustId")
Chapter 2 - Data Components in Visual Studio .NET
ChapterdtRelation
3 - ADO.NET =
in New DataRelation("CustOrderRelation",
Disconnected Environments CustCol, orderCol)
ChapterdtSet.Tables("Orders").ParentRelations.Add(dtRelation)
4 - ADO.NET in Connected Environments
ChapterDataGrid1.SetDataBinding(dtSet,
5 - Handling ADO.NET Events "Customers")
End6 Sub
Chapter - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Listing 3-15 shows all three CreateCustomerTable,CreateOrdersTable, and BindData methods.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter
As you 11
can- Stored Procedures
see from and Views
the CreateCustomersTable method in Listing 3-15, it creates the Customers
table using
Chapter 12 - DataTable andand
Oracle, SQLXML, adds the Id,
Other .NETName
Data and Address columns to the table. You use DataColumn
Providers
to add these
Chapter columns. The
13 - Developing Id column
a Custom has properties such as ReadOnly and Unique. As discussed
Data Provider
earlier,14
Chapter to add a columnDatabase
- Developing to a DataTable, you create
Web Applications usingaASP.NET
DataColumn object, set its properties, and then
call the15
Chapter DataTable.Coumns.Add method.
- Using ADO.NET in Xml Web Similar to the id column, you add two more columns, Name
Services
and Address,
Chapter of string
16 - ASP.NET type to
Server the table.
Controls and After that you make the id column the primary key by setting
Data Binding
DataTable.PrimaryKey
Chapter as Web
17 - Building Real-World the idApplications
column:
Chapter 18 - Object-Relational Mapping in .NET
PrimaryKeyColumns(0)
Chapter = custTable.Columns("id")
19 - Mapped Objects: Performance Considerations and Data Binding
custTable.PrimaryKey
Chapter 20 - COM Interoperability= and
PrimaryKeyColumns
ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
After creating a DataTable you add it to a DataSet using the DataSet.Tables.Addmethod. This
Appendix A - Relational Databases: Some Basic Concepts
method takes one argument of type DataTable:
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
dtSet = New DataSet("Customers")
Index
dtSet.Tables.Add(custTable)
List of Figures
List of Tables
List
Now,of Listings
the last step is to add data to DataTable. You add data using DataRow. First, you create a
List object using DataTable'sNewRow method, add data to a DataRow's items, and add DataRow
of Sidebars
DataRow
to the DataTable using the DataTable.Rows.Add method. You'll follow the same method for the
second table in CreateOrdersTable to create the Orders table. The Orders table has the fields
OrderId, Name, Description, and CustId. The BindData method creates a relationship by using
DataRelation and binds the id column of the Customers tables to the CustId column of the Orders
table. The name of the relationship is CustOrderRelation. After that you bind DataTable to the
DataGrid using the SetDataBinding method.

The output of Listing 3-15 looks like Figure 3-9.


Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Figure 3-9: A DataGrid with data relations
Introduction
Chapter 1
If you click-the
ADO.NET Basics
CustOrderRelation link, the output looks like Figure 3-10.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Figure 3-10: Orders record for Customers id 1001
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter
As you 16
can- ASP.NET
see fromServer
FigureControls and Data Binding
3-10,DataGrid shows all the orders for Customer id 1001.
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
More DataTable Operations
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Adding and deleting are two common operations when working with databases. You've already learned
Chapter 21 -to
about how Messaging
add data to a DataTable using a DataRow. In this section you'll see how to add, delete,
Chapter 22 - SQL Server
sort, and search and ADO.NET: Notes
data programmatically. on Performance
You'll keep all data in memory in the form of DataTables, but
Appendix A -see
you'll also Relational
a SaveDatabases: Some
option, which willBasic
saveConcepts
data in XML format. This is a simple Windows Forms
Appendix B - Commonly Used SQL Statements
application.
Appendix C - ADO.NET Frequently Asked Questions
The first step is to build a GUI (see Figure 3-11). To build this GUI, you create a Windows Application
Index
project. After that, add a DataGrid control, four Button controls, seven TextBox controls, five
List of Figures
GroupBox controls, and some Label controls. Then adjust them on your form. You can also change the
List of Tables
background color of the controls.
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Figure
Chapter 3 - 3-11: Add,
ADO.NET indelete, sort, and
Disconnected search operations in a DataTable
Environments
Chapter 4 - ADO.NET in Connected Environments
In this application,
Chapter 5
we show you all basic common operations such as adding, deleting, sorting, and
- Handling ADO.NET Events
searching rows. The Add Row button reads the name and address, adds a new row in the DataTable,
Chapter 6 - Integrating XML with ADO.NET
and displays the added row in the DataGrid. The Delete Rows button deletes rows based on the entered
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
criteria. To delete rows, you enter the column name and value, and the Delete Rows button will delete all
Chapter 8 - Constraints and Data Relations
the rows that meet the criteria. The Search button reads column name and values entered in the text
Chapter 9 - ADO.NET Exception Handling
boxes and returns data that matches the criteria. The Apply Sort button reads a column and sorts rows
Chapter
based on10 that
- Working with
column. theSave
The ODBCmethod
.NET Data Provider
saves the current data of the DataTable to an XML document.
Chapter 11 - Stored Procedures and Views
Note
Chapter 12 You don'tSQLXML,
- Oracle, have to create the .NET
and Other sameData
formProviders
as shown in Figure 3-11. The only thing you need to
Chapter 13 have is a DataGrid
- Developing a Customwith
Datathe same number of TextBox and Button controls.
Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Now, you change the names of the form controls and add the DataSet variable dtSet and the
Chapter 15 - Using ADO.NET in Xml Web Services
DataTable variable custTable to the beginning of the form. Besides the variables defined for the
Chapter 16 - ASP.NET Server Controls and Data Binding
controls (see the MoreDataTableOpsSamp sample available with the source code for details), add two
Chapter 17 - Building Real-World Web Applications
variables of type DataTable and DataSet, as shown in Listing 3-16.
Chapter 18 - Object-Relational Mapping in .NET
Chapter
Listing19 - Mapped
3-16: Objects:
Class-Level Performanceand
DataTable Considerations and Data Binding
DataSet Variables
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Private custTable As DataTable
Chapter
Private22 -dtSet
SQL Server
As and ADO.NET: Notes on Performance
DataSet
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Now you create the Customers table with three columns: id, Name, and Address. You've already learned
Index
how
List of to add columns to a DataTable using a DataColumn and binding it to a DataGrid. The
Figures
CreateCustomersTable
List of Tables method creates the Customers table. After creating the Customers table, you
addofthe
List DataTable to the DataSet using the DataSet.Tables.Add method. The
Listings
CreateCustomersTable
List of Sidebars method looks like Listing 3-17.

Listing 3-17: The CreateCustomersTable Method

' This method creates Customers table


Private Sub CreateCustomersTable()
' Create a new DataTable.
custTable = New DataTable("Customers")
Dim dtColumn As DataColumn
' Create id Column
dtColumn = New DataColumn()
dtColumn.DataType = System.Type.GetType("System.Int32")
dtColumn.ColumnName = "id"
Applied ADO.NET: Building
dtColumn.AutoIncrement = True Data-Driven Solutions
by Mahesh Chand
dtColumn.AutoIncrementSeed and David
= Talbot
100 ISBN:1590590732
Apress 2003 (928
dtColumn.AutoIncrementStep = 1 pages)
This text provides
dtColumn.Caption = "Custextensive
ID"coverage of ADO.NET technology
including ADO.NET
dtColumn.ReadOnly = True internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
dtColumn.Unique
namespaces= and Trueclasses, and how to integrate both with
' Add id ADO.NET.
Column to the DataColumnCollection.
custTable.Columns.Add(dtColumn)
' Create Name column.
Table ofdtColumn
Contents = New DataColumn()
ApplieddtColumn.DataType
ADO.NETBuilding Data-Driven Solutions
= System.Type.GetType("System.String")
Introduction
dtColumn.ColumnName = "Name"
ChapterdtColumn.Caption
1 - ADO.NET Basics = "Cust Name"
ChapterdtColumn.AutoIncrement
2 - Data Components in Visual=Studio False.NET
ChapterdtColumn.ReadOnly
3 - ADO.NET in Disconnected = False Environments
ChapterdtColumn.Unique
4 - ADO.NET in Connected= False Environments
Chapter'5 Add Name column
- Handling to the table.
ADO.NET Events
ChaptercustTable.Columns.Add(dtColumn)
6 - Integrating XML with ADO.NET
Chapter'7 Create Address
- Data Binding column.Forms Data-Bound Controls
and Windows
dtColumn = New DataColumn()
Chapter 8 - Constraints and Data Relations
dtColumn.DataType = System.Type.GetType("System.String")
Chapter 9 - ADO.NET Exception Handling
dtColumn.ColumnName = "Address"
Chapter 10 - Working with the ODBC .NET Data Provider
dtColumn.Caption = "Address"
Chapter 11 - Stored Procedures and Views
dtColumn.ReadOnly = False
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
dtColumn.Unique = False
Chapter'13 - Developing
Add Addressa Custom
columnData to Provider
the table.
ChaptercustTable.Columns.Add(dtColumn)
14 - Developing Database Web Applications using ASP.NET
Chapter'15 - Using
Make the ADO.NET in Xml Web
ID column theServices
primary key column.
ChapterDim
16 -PrimaryKeyColumns()
ASP.NET Server Controls andAs Data Binding
DataColumn = New DataColumn(1) {}
ChapterPrimaryKeyColumns(0)
17 - Building Real-World Web=Applications
custTable.Columns("id")
ChaptercustTable.PrimaryKey
18 - Object-Relational Mapping = in .NET
PrimaryKeyColumns
Chapter'19Instantiate
- Mapped Objects:thePerformance
DataSet Considerations
variable. and Data Binding
ChapterdtSet
20 - COM= New DataSet("Customers")
Interoperability and ADO.NET
Chapter'21Add the custTable to the DataSet.
- Messaging
ChapterdtSet.Tables.Add(custTable)
22 - SQL Server and ADO.NET: Notes on Performance
RefreshData()
Appendix A - Relational Databases: Some Basic Concepts
End Sub
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List
At of
theFigures
end of the CreateCustomersTable method you call the RefreshData method, which refreshes
List of Tables
theDataGrid contents and fills them with the current data of the DataTable by setting DataGrid's
List of Listings property to the DataSet'sDefaultViewManager. The RefreshData method looks like
DataSource
List
theoffollowing:
Sidebars

Private Sub RefreshData()


dataGrid1.DataSource = dtSet.DefaultViewManager
End Sub

As you can see from Figure 3-12, the Add Row button adds a new row to the Customers' DataTable with
the Name and Address columns reading from the Name and Address text boxes. The Delete Rows button
deletes the row number inserted in the Enter Row # text box. The Search button searches and returns
rows that contain the name entered in the Enter Name text box of the Search group box.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - 3-12:
Figure ADO.NET in Disconnected
Adding rows to the Environments
DataTable
Chapter 4 - ADO.NET in Connected Environments
Chapter
OK, now5 it's
- Handling ADO.NET
time to write codeEvents
for the button event handlers. You can write button event handlers by
double-clicking
Chapter the buttons
6 - Integrating XML or using
with the Properties windows. First, you write the event handler for the Add
ADO.NET
Row button
Chapter with the
7 - Data handler
Binding and name AddRow_Click.
Windows After
Forms Data-Bound that, write event handlers for the Delete Rows
Controls
and Search
Chapter buttons; theand
8 - Constraints event handler
Data names for these buttons are DeleteRow_Click and
Relations
SearchButton_Click,
Chapter respectively.
9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
You add a new row to the DataTable using DataRow and call the Add and AcceptChanges methods
Chapter 11 - Stored Procedures and Views
of the DataTable.Listing 3-18 shows the Add Row button click event handler.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter
Listing13 - Developing
3-18: a Custom Data Provider
The AddRowBtn_Click Method
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Private Sub AddRowBtn_Click(ByVal sender As System.Object, _
Chapter
ByVal16 -eASP.NET Server Controls and Data
As System.EventArgs) Binding AddRowBtn.Click
Handles
Chapter'17 - Building Real-World Web Applications
Add rows to the custTable using its NewRow method
Chapter'18
I -add
Object-Relational Mapping inwith
three customers .NET thier addresses, name and id
ChapterDim
19 -myDataRow
Mapped Objects:
As Performance
DataRow =Considerations and Data Binding
custTable.NewRow()
ChaptermyDataRow("Name")
20 - COM Interoperability= and ADO.NET
TextBox4.Text.ToString()
ChaptermyDataRow("Address")
21 - Messaging = TextBox5.Text.ToString()
ChaptercustTable.Rows.Add(myDataRow)
22 - SQL Server and ADO.NET: Notes on Performance
custTable.AcceptChanges()
Appendix A - Relational Databases: Some Basic Concepts
RefreshData()
Appendix B - Commonly Used SQL Statements
End Sub
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
As you can see, the code adds Name as TextBox4.Text and Address as TextBox4.Text.
List of Listings
List
You of call
Sidebars
NewRow of DataTable to add a new row to DataTable, set its field values, and call the
DataTable.Rows.Add method to add it. After the Add method, you also call
DataTable.AcceptChanges method to save the changes. At the end, you call the RefreshData
method to fill the DataGrid with the records.

If you add six rows to the DataTable using the Add Row button, the result looks like Figure 3-12.

The Delete Rows button deletes the records from a column that matches the given criteria. The Remove
Rows Section takes a column name and column value, and the Delete Rows button click event handler
removes the rows from the DataTable.

TheDeleteRowsBtn_Click method is the click event handler for the Delete Rows button (see Listing
3-19). As you can see, the code uses the Select method of DataTable. The Select method of
DataTable takes a criterion and returns the rows that meet the given criteria. In this case, you pass a
column name and Applied ADO.NET:
its value Building
as a criterion. TheData-Driven Solutions
Select method returns all the matching rows in an array of
by Mahesh Chand and David Talbot ISBN:1590590732
DataRow. Once you've retrieved these rows, you call DataRow.Delete method to delete the rows one
by one, followed Apress 2003
by a call (928 pages)
of DataRow.AcceptChanges method to save the changes.
This text provides extensive coverage of ADO.NET technology
Listing 3-19: Theincluding ADO.NET internals,
DeleteRow_Click Methodnamespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Private Sub DeleteRowsBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles DeleteRowsBtn.Click
Dim str As String = TextBox7.Text + "='" + TextBox6.Text + "'"
Table of Contents
Dim rows() As DataRow = custTable.Select(str)
Applied ADO.NETBuilding Data-Driven Solutions
' If no record found
Introduction
If rows.Length = 0 Then
Chapter 1 MessageBox.Show("Record
- ADO.NET Basics not found!")
Chapter 2 Return
- Data Components in Visual Studio .NET
ChapterEnd
3 -If ADO.NET in Disconnected Environments
ChapterDim
4 -iADO.NET in Connected Environments
As Integer
ChapterFor
5 -iHandling
= 0 To ADO.NET Events
rows.Length - 1 Step i + 1
Chapter 6 rows(i).Delete()
- Integrating XML with ADO.NET
Chapter 7 rows(i).AcceptChanges()
- Data Binding and Windows Forms Data-Bound Controls
Next
Chapter 8 - Constraints and Data Relations
ChapterRefreshData()
9 - ADO.NET Exception Handling
End Sub
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
In the end, you call the RefreshData method to refresh the grid.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter
As you 15
can- Using ADO.NET
see from Figurein3-13,
Xml Web
if youServices
enter column name as Name with the value Puneet, then the
Chapter
Delete 16 - ASP.NET
Rows Server Controls
button removes the row and Data
with Id =Binding
102.
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 3-13: Deleting rows from the DataTable

You just saw the usage of the DataTable.Select method, which sorts and filters the DataTable rows.
TheSelect method has four overloaded forms. The Select method can take an expression with
optional sort and row state parameters.

Afilter is a conditional statement that uses the SELECTWHERE clause to select conditional data from the
database. All SQL conditional operators are valid in the filter string. For example, to filter rows where the id
is greater than 22, the filter string will be Id>22; for selecting records with the name Ross, the filter string
will be Name='Ross'. The SeachBtn_Click method searches for the criteria you specify on the form.
Applied
As you can see from ADO.NET:
Listing 3-20, theBuilding
code firstData-Driven Solutions
calls the Select method with the given criteria, which
by Mahesh Chand and David Talbot
returns an array of DataRow objects. It creates a new DataTable ISBN:1590590732
called searchTable and adds all
rows returned by Apress 2003 (928
the Select pages)to this DataTable, and it later binds the searchTable to
method
DataGrid to view This
thetext provides extensive coverage of ADO.NET technology
rows.
including ADO.NET internals, namespaces, classes, and
Listing 3-20: Theinterfaces, and takes a comprehensive
SearchButtonClick Method look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Private Sub SearchBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SearchBtn.Click
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Dim searchTable As DataTable = New DataTable("SortTable")
Introduction
searchTable = custTable.Clone()
ChapterDim
1 -strADO.NET Basics
As String = TextBox1.Text & " ='" & TextBox2.Text + "'"
ChapterDim
2 -rows()
Data Components in Visual
As DataRow = Studio .NET
custTable.Select(str)
Chapter'3 If- ADO.NET
no recordin Disconnected
found Environments
ChapterIf
4 rows.Length
- ADO.NET in Connected
= 0 Then Environments
Chapter 5 MessageBox.Show("Name
- Handling ADO.NET Events not found!")
Chapter 6 Return
- Integrating XML with ADO.NET
ChapterEnd
7 -If Data Binding and Windows Forms Data-Bound Controls
Dim i As Integer
Chapter 8 - Constraints and Data Relations
For i = 0 ToException
Chapter 9 - ADO.NET rows.Length
Handling - 1 Step i + 1
Chapter 10 - Working with the ODBC =
Dim row As DataRow searchTable.NewRow()
.NET Data Provider
row("Name") = rows(i)("Name")
Chapter 11 - Stored Procedures and Views
row("Address") = rows(i)("Address")
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
searchTable.Rows.Add(row)
Chapter 13 - Developing a Custom Data Provider
Next
Chapter 14 - Developing Database Web Applications using ASP.NET
Dim sortdtSet As DataSet = New DataSet("SortedData")
Chapter 15 - Using ADO.NET in Xml Web Services
sortdtSet.Tables.Add(searchTable)
Chapter 16 - ASP.NET Server Controls and Data Binding
DataGrid1.DataSource = sortdtSet.DefaultViewManager
Chapter
End Sub17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Now using
Chapter 21 -the Search Section text boxes, you can search for records with the name Amy by entering
Messaging
Name in
Chapter 22the Enter
- SQL Column
Server Name box,Notes
and ADO.NET: entering Amy in the Enter Value box, and clicking the Search
on Performance
button. The
Appendix result looks
A - Relational like Figure
Databases: 3-14.
Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 3-14: Result of clicking the Search button for Name="Amy"


Now using the Select method, you can also sort the data of a DataTable. As you can see from Figure
Applied ADO.NET: Building Data-Driven Solutions
3-14, the Sort Section has a DESC check box and an Enter Column
by Mahesh Chand and David Talbot
Name text box. If the DESC check
ISBN:1590590732
box is clicked, rows will be sorted in descending
Apress 2003 (928 pages)
order; otherwise, the default order is ascending. A filter
followed by values ASC and DESC sorts the rows filtered using the Select method.
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Listing 3-21 shows the code written on the Sort button click event handler. As you can see, first the code
interfaces, and takes a comprehensive look at XML
builds a filter string, checking and
namespaces whether theand
classes, DESC
howcheck box is both
to integrate checked.
with After that it simply calls the
Select method ADO.NET.with the filter string and creates a new DataTable from the rows returned by the
Select method and binds the new DataTable to the grid.

Table of Contents
Listing 3-21: Sorting Data Using the Select Method
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Private Sub SortBtn_Click(ByVal sender As System.Object, _
Chapter 1
ByVal -eADO.NET Basics
As System.EventArgs) Handles SortBtn.Click
Chapter 2 - Data Components in Visual Studio .NET
ChapterDim
3 -sortTable
ADO.NET in Disconnected
As DataTableEnvironments
= New DataTable("SortTable")
ChaptersortTable
4 - ADO.NET
= incustTable.Clone()
Connected Environments
ChapterDim
5 -strSort
Handling ADO.NET Events= TextBox3.Text
As String
ChapterIf
6 (CheckBox1.Checked)
- Integrating XML with ADO.NET
Then
Chapter 7 strSort
- Data Binding and Windows
= strSort & " Forms
DESC"Data-Bound Controls
ChapterElse
8 - Constraints and Data Relations
Chapter 9 strSort = Exception
- ADO.NET strSortHandling
& " ASC"
ChapterEnd
10 -If
Working with the ODBC .NET Data Provider
ChapterDim
11 -rows() As DataRow
Stored Procedures = _
and Views
ChaptercustTable.Select(String.Empty,
12 - Oracle, SQLXML, and Other .NET Data strSort)
Providers
Dim i As Integer
Chapter 13 - Developing a Custom Data Provider
For i = 0 To rows.Length - 1 Step i + 1
Chapter 14 - Developing Database Web Applications using ASP.NET
Dim row As DataRow = sortTable.NewRow()
Chapter 15 - Using ADO.NET in Xml Web Services
row("Name") = rows(i)("Name")
Chapter 16 - ASP.NET Server Controls and Data Binding
row("Address") = rows(i)("Address")
Chapter 17 - Building Real-World Web Applications
sortTable.Rows.Add(row)
ChapterNext
18 - Object-Relational Mapping in .NET
ChapterDim
19 -sortdtSet
Mapped Objects:
As Performance
DataSet =Considerations and Data Binding
New DataSet("SortedData")
ChaptersortdtSet.Tables.Add(sortTable)
20 - COM Interoperability and ADO.NET
ChapterDataGrid1.DataSource
21 - Messaging = sortdtSet.DefaultViewManager
Chapter
End Sub22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
To sort the records, you enter the column name Name and check the DESC check box. Now, if you click
Index
theofApply
List Sort button, you'll see the records are sorted on the name. If you uncheck the DESC check box,
Figures
you'll see the rows sorted in ascending order.
List of Tables
List
The of last
Listings
thing to do in this example is to create the Save button. The DataSet class provides methods to
List
readof Sidebars
and write XML documents. The WriteXml method of DataSet saves data to an XML document.
(We discuss XML and ADO.NET in more detail in Chapter 6.)

Listing 3-22 shows the Save method button click event handler. As you can see, the code calls a
SaveFileDialog and reads the name and path of the file. You can browse the path where you want to
save the file. After saving data, if you open the XML file, you'll see all data from the DataTable is stored in
this file.

Listing 3-22: Saving a DataTable Data in an XML Document

Private Sub SaveBtn_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles SaveBtn.Click
Dim fdlg As SaveFileDialog = New SaveFileDialog()
Applied
fdlg.Title ADO.NET: ADO.NET
= "Applied Building Data-Driven
Save FileSolutions
Dialog"
by Mahesh Chand and
fdlg.InitialDirectory = "c:\\" David Talbot ISBN:1590590732
Apress 2003 (928 pages)
fdlg.Filter = "XML files (*.xml)|*.*|All files (*.*)|*.*"
This text provides extensive
If fdlg.ShowDialog() coverage of ADO.NET
= DialogResult.OK Then technology
including ADO.NET internals,
dtSet.WriteXml(fdlg.FileName) namespaces, classes, and
interfaces, and takes a comprehensive look at XML
End If namespaces and classes, and how to integrate both with
End Sub ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
The Row and Column Collections
Introduction
Chapter 1 - ADO.NET Basics
Do you remember the Rows and Columns property of DataTable? As mentioned earlier, the Columns
Chapter 2 - Data Components in Visual Studio .NET
property of DataSet, represented by a DataColumnCollection object, stands for all columns of a
Chapter 3 - ADO.NET
DataTable. The Rowsin Disconnected Environmentsrepresents a collection of rows of a DataTable.
property of DataTable
Chapter 4 - ADO.NET in Connected Environments
Generally, you access the rows and columns collections through the Rows and Columns properties of a
Chapter 5 - Handling
DataTable. You've ADO.NET Events
already seen how to add columns and rows to a DataTable in the previous sections.
Chapter 6 - Integrating XML with ADO.NET
In the following sections, we discuss the methods and properties.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8
- Constraints and Data Relations
The DataRowCollection
Chapter 9
- ADO.NET Exception Handling
Class
Chapter 10 - property
TheCount Working with the ODBC .NET Data Provider
of DataRowCollection returns the total number of rows in a collection. The
Chapter 11 code
following - Stored Procedures
reads the total and Views
number of rows in custTable:
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing
Dim rows a Custom Data Provider
As DataRowCollection = custTable.Rows
Chapter 14 - Developing
Dim counter Database Web Applications using ASP.NET
As Integer
Chapter
counter15 -=Using ADO.NET in Xml Web Services
rows.Count
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
TheClear
Chapter 18 - method of DataRowCollection
Object-Relational Mapping in .NET removes all the rows in a collection and sets the Rows
count to19zero:
Chapter - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Dim rows
Chapter As DataRowCollection = custTable.Rows
21 - Messaging
rows.Clear()
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
TheRemove and RemoveAt methods remove a specified DataRow based on the DataRow and its index,
Appendix C - ADO.NET Frequently Asked Questions
respectively.
Index
The
List ofContains
Figures method looks for a primary key value in the collection. This method returns True if a
collection
List finds the specified value in the primary key column. The Contains method takes an argument
of Tables
of an
List object type.
of Listings
List of Sidebars
TheFind method is one more useful method of DataRowCollection; it searches for a row in the
collection. Similar to the Contains method, the Find method also takes an argument of object type,
which means Find can be used to find any types of values.

Listing 3-23 checks if the row collection's primary key column contains value 1002. If it does, it uses the
Find method, which returns a DataRow and calls the DataRowCollection.Remove method to
remove the row from the collection.

Listing 3-23: Using the Contains,Find, and Remove Method of DataRowCollection

Dim rows As DataRowCollection = custTable.Rows


Dim row As DataRow
If rows.Contains(1002) Then
Applied ADO.NET: Building Data-Driven Solutions
row = rows.Find(1002)
by Mahesh Chand and David Talbot
rows.Remove(row) ISBN:1590590732
Apress 2003 (928 pages)
MessageBox.Show("Row Removed")
Else This text provides extensive coverage of ADO.NET technology
including ADO.NET
MessageBox.Show("Row notinternals,
found")namespaces, classes, and
interfaces, and takes a comprehensive look at XML
End If namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
As you saw in earlier sections, the Add method of DataRowCollection adds a new row at the end of a
Applied ADO.NETBuilding
collection. But what if youData-Driven Solutions
want to insert a row between two rows? The InsertAt method does the trick
Introduction
for you and inserts a row at the specified index, and the row at the index becomes the next row. Listing 3-
24 inserts
Chapter 1 -and removes
ADO.NET rows at the specified index using the InsertAt and RemoveAt methods.
Basics
Chapter 2 - Data Components in Visual Studio .NET
Listing 3-24:
Chapter 3
Using the InsertAt and RemoveAt Methods
- ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Dim newRow
Chapter As DataRow
5 - Handling ADO.NET Events
newRow
Chapter 6 =- Integrating
custTable.NewRow()
XML with ADO.NET
newRow("id")
Chapter = 1005
7 - Data Binding and Windows Forms Data-Bound Controls
newRow("Address")
Chapter = Data
8 - Constraints and "NewRelations
Address"
newRow("Name")
Chapter 9 - ADO.NET = "New Name"
Exception Handling
rows.InsertAt(newRow, 3)
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
If (Not rows(1).IsNull("id")) Then
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
rows.RemoveAt(1)
Chapter 13 - Developing a Custom Data Provider
End If
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
The DataColumnCollection Class
Chapter 18 - Object-Relational Mapping in .NET
Similar19
Chapter to DataRowCollection, the Count
- Mapped Objects: Performance property of
Considerations DataColumnCollection
and Data Binding returns the number
of columns
Chapter 20 - in
COMa collection. You already
Interoperability saw in previous samples that you can use the Add method to add
and ADO.NET
a new column
Chapter to a collection.
21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
You can use the CanRemove method to find out if a column can be removed from a collection. It returns
Appendix A - Relational Databases: Some Basic Concepts
True if a column can be removed from a collection; otherwise, it returns False. This method performs
Appendix B - Commonly Used SQL Statements
several operations to make sure the column exists in the table and it's not involved in a constraint or
Appendix
relation. CThe
- ADO.NET Frequently
Contains methodAsked Questions
checks if a column with a given name exists in the collection.
Index
The
List Figures and RemoveAt methods remove a column from a collection. The Remove method takes a
ofRemove
parameter
List of Tablesof type DataColumn or a name of the column. Listing 3-25 uses some of these methods.
List of Listings
Listing 3-25: Removing a Column from a Collection
List of Sidebars

Dim cols As DataColumnCollection = custTable.Columns


Dim str As String
str = cols.Count.ToString()
MessageBox.Show(str)
If cols.Contains("Name") Then
If cols.CanRemove(cols("Name")) Then
cols.Remove("Name")
End If
End If
Applied ADO.NET: Building Data-Driven Solutions
TheRemoveAt method removes
by Mahesh Chandaand
column
DavidatTalbot
the given index. The Clear method removes all the
ISBN:1590590732
Apress collection.
columns from a column The following codes uses these two methods:
2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
cols.RemoveAt(2)
interfaces, and takes a comprehensive look at XML
cols.Clear() namespaces and classes, and how to integrate both with
ADO.NET.

The DataRow States and Versions


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
ADataRow changes its states as you work on it. The RowState property of DataRow represents the
Introduction
state of a row, which is a type of DataRowState enumeration. The DataRowState enumeration has five
Chapter 1 - ADO.NET Basics
members:
Chapter 2 - Data Components in Visual Studio .NET
The
Chapter 3 Added state in
- ADO.NET means the row Environments
Disconnected has been added to a collection and the DataRow.AcceptChanges
method
Chapter hasn't been
4 - ADO.NET called yet.Environments
in Connected
Chapter 5 - Handling ADO.NET Events
TheDeleted state means the row was deleted using DataRow.Delete.
Chapter 6 - Integrating XML with ADO.NET
Chapter
The7 Detached
- Data Binding
stateand Windows
means Forms
the row hasData-Bound Controls
been created but not added to a collection.
Chapter 8 - Constraints and Data Relations
The
Chapter - ADO.NETstate
9 Modified means
Exception the row has been changed, and DataRow.AcceptChanges has not
Handling
been
Chapter 10 called yet.with the ODBC .NET Data Provider
- Working
Chapter 11 - Stored Procedures and Views
TheUnchanged state means that DataRow.AcceptChanges has been called but the row wasn't
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
changed.
Chapter 13 - Developing a Custom Data Provider
Chapter
You can14examine
- Developing Database
the state Web Applications
of a DataRow using
after any ASP.NET
operation on the row. Listing 3-26 uses the RowState
Chapter 15to- determine
property Using ADO.NET in Xml
the state of Web Services
a row.
Chapter 16 - ASP.NET Server Controls and Data Binding
Listing17
Chapter 3-26: Determining
- Building theWeb
Real-World State of a Row
Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter
Dim row19 -As
Mapped Objects: Performance Considerations and Data Binding
DataRow
row =20custTable.NewRow()
Chapter - COM Interoperability and ADO.NET
MessageBox.Show(row.RowState.ToString())
Chapter 21 - Messaging
row("id")
Chapter = Server
22 - SQL 1007 and ADO.NET: Notes on Performance
row("Name")
Appendix = "Noak
A - Relational "
Databases: Some Basic Concepts
row("Address")
Appendix = Used
B - Commonly "Tame
SQL Lack, NJ"
Statements
custTable.Rows.Add(row)
Appendix C - ADO.NET Frequently Asked Questions
MessageBox.Show(row.RowState.ToString())
Index
custTable.Rows.Remove(row)
List of Figures
custTable.AcceptChanges()
List of Tables
List of Listings
List of Sidebars
TheDataRowView is one more important class you should know when working with tables and rows. You
can create a DataRowView that corresponds to a DataRow, which you can then bind to data-bound
controls directly.

ADataRowView class represents a customized view of a DataRow, which can be bound to data-bound
controls such as a DataGrid. When you work with a DataRow (update data of a DataRow), the
DataRow has different versions. These states are represented by a DataRowVersion enumeration,
which has four values: Current,Default,Proposed , and Original. The version of a DataRow
changes when you call the BeginEdit,CancelEdit, and EndEdit methods of DataRow as well as
theAcceptChanges and RejectChanges methods of DataTable.

After calling DataRow.BeginEdit and changing the value of a row, both the Current and Proposed
values become available. After calling DataRow.CancelEdit, the Proposed value isn't available. After
Applied ADO.NET:
a call of DataRow.EndEdit, Buildingvalue
the Current Data-Driven
becomesSolutions
the Proposed value, and the Proposed
by Mahesh Chand and David Talbot
value becomes unavailable. The call of DataRow.AcceptChanges ISBN:1590590732
means the original value becomes
Apress 2003 (928 pages)
identical to the Current value. A call of DataTable.RejectChanges means the Proposed value is
This text
removed. The version provides
becomes extensive coverage of ADO.NET technology
Current.
including ADO.NET internals, namespaces, classes, and
interfaces,
TheDataView property and takes a comprehensive
of DataRowView look
class returns at XML
a DataView object for a row, which can be
namespaces and classes, and how to integrate both with
bound to data-bound controls.
ADO.NET.
The IsEdit and IsNew properties represent whether a row is in edit mode
or new mode, respectively. The DataRow property returns the DataRow being viewed, and the
RowVersion property returns the version of a row.
Table of Contents
TheBeginEdit
Applied method Data-Driven
ADO.NETBuilding starts editingSolutions
a row, and the CancelEdit method cancels editing. The Delete
method deletes a row, and the EndEdit method ends the editing. Listing 3-27 starts editing a row and
Introduction
later calls the CancelEdit and EndEdit methods, followed by the AcceptChanges method.
Chapter 1 - ADO.NET Basics
Chapter
Listing23-27:
- Data Components
Editing in Visual
Rows Using Studio .NETCancelEdit, and EndEdit Methods
BeginEdit,
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Dim row As DataRow
Chapter 5 - Handling ADO.NET Events
row = custTable.NewRow()
Chapter 6 - Integrating XML with ADO.NET
MessageBox.Show(row.RowState.ToString())
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
row("id") = 1007
Chapter 8 - Constraints and Data Relations
row("Name") = "Noak "
Chapterrow("Address")
9 - ADO.NET Exception Handling
= "Tame Lack, NJ"
ChaptercustTable.Rows.Add(row)
10 - Working with the ODBC .NET Data Provider
ChapterMessageBox.Show(row.RowState.ToString())
11 - Stored Procedures and Views
ChaptercustTable.Rows.Remove(row)
12 - Oracle, SQLXML, and Other .NET Data Providers
ChaptercustTable.AcceptChanges()
13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter'15
Start
- UsingEditing the
ADO.NET in Xml Row
Web Services
ChaptercustTable.Rows(0).BeginEdit()
16 - ASP.NET Server Controls and Data Binding
ChaptercustTable.Rows(0)("id") = 1008
17 - Building Real-World Web Applications
ChaptercustTable.Rows(0)("Name") = "New Name"
18 - Object-Relational Mapping in .NET
ChaptercustTable.Rows(0)("Address") = "New Address"
19 - Mapped Objects: Performance Considerations and Data Binding
' Find out DataRowVersion
Chapter 20 - COM Interoperability and ADO.NET
If custTable.Rows(0).HasVersion(DataRowVersion.Proposed) Then
Chapter 21 - Messaging
MessageBox.Show(DataRowVersion.Proposed.ToString())
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
End If
Appendix A - Relational Databases: Some Basic Concepts
If custTable.Rows(0).HasVersion(DataRowVersion.Original) Then
Appendix B - Commonly Used SQL Statements
MessageBox.Show(DataRowVersion.Original.ToString())
AppendixEnd C -If
ADO.NET Frequently Asked Questions
Index ' Cancel the edit.
List of Figures
custTable.Rows(0).CancelEdit()
List of Tables
' BeginEdit again
List of Listings
custTable.Rows(0).BeginEdit()
List of Sidebars
custTable.Rows(0)("id") = 1008
custTable.Rows(0)("Name") = "New Name"
custTable.Rows(0)("Address") = "New Address"
custTable.Rows(0).EndEdit()
custTable.Rows(0).AcceptChanges()
DataGrid1.DataSource = custTable
Using DataSet,
AppliedDataView, andData-Driven
ADO.NET: Building DataViewManager
Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
As you saw in Chapter
Apress2, ADO.NET
2003 provides components to view data in data-bound controls. You just
(928 pages)
drag and drop controls to a form, set their properties,
This text provides extensive and
coverage of bind them
ADO.NET to data-bound controls. Then you're
technology
all set to run yourincluding
programs. ADO.NET internals, namespaces, classes, and classes fall into this category. You
The DataView and DataViewManager
interfaces,
can use these classes and takes
to represent a comprehensive
different views of alook at XML
DataTable based on different filter and sort
criterion. You cannamespaces and classes,
use these classes eitherand how to integrate
at design-time or atboth with
runtime.
ADO.NET.
ADataSet is a key component in the ADO.NET data model. It's an in-memory representation of one or
multipleDataTables, relations, and constraints. A DataSet stores data from a data source and provides
Table of Contents
data to the client applications (Web Forms or Windows Forms). A DataSet's contents can be bound to
Applied ADO.NETBuilding Data-Driven Solutions
data-bound controls. (We discuss the DataSet class in more detail in "TheDataSet: The Heart of
Introduction
ADO.NET."
Chapter 1 - ADO.NET Basics
Chapter 2 - DataisComponents
TheDataView useful wheninyou
Visual
needStudio .NET
to bind a DataTable or multiple DataTable objects with data-
Chapter 3 - ADO.NET
bound controls. in Disconnected
You can Environments
also represent multiple views of the same data by applying a filter and sort on a
DataTable,
Chapter and youincan
4 - ADO.NET bind these
Connected multiple views to different data-bound controls such as DataGrid,
Environments
DataList,
Chapter 5 - ComboBox, and ListBox
Handling ADO.NET Events controls.
Chapter 6 - Integrating XML with ADO.NET
Figure 3-15 shows the relationship between a DataSet,DataTable,DataView, and Windows and Web
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Forms controls. As you can see, a DataSet contains three DataTable objects, which are represented
Chapter 8 - Constraints and Data Relations
by three different views. You can bind the three different DataView objects to different data-bound
Chapter 9 - ADO.NET Exception Handling
controls to represent data in different ways.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - 3-15:
Figure Commonly Used SQLbetween
Relationship Statements
the DataSet,DataTable, and DataView
Appendix C - ADO.NET Frequently Asked Questions
Index

The DataSet: The Heart of ADO.NET


List of Figures
List of Tables
ADataSet
List of Listingsobject plays a vital role in the ADO.NET component model. A DataSet represents a
disconnected
List of Sidebars cache of data in the form of tables, rows, columns, or XML schemas. If you've ever
programmed a database application in previous versions of VS, you're probably familiar with recordsets. A
recordset object was a way to represent data in your programs. Similar to a recordset, a DataSet
represents data in your applications. Once you've constructed a DataSet, you can get or set data in your
application or your data source. As mentioned earlier, the DataSet works in both connected and
disconnected environments. A DataSet communicates with a DataAdapter and calls the
DataAdapter'sFill method to fill data from a DataAdapter. You can fill a DataSet object with
multiple tables or stored procedures with the aid of the DataAdapters. You'll see how a DataSet works
in a connected environment to access data from multiple data sources in the next chapter. In this section,
you'll see how it works with DataTable and DataView objects. (We discuss DataAdapter in more
detail in Chapter 4.) Once data is filled to a DataSet from a DataAdapter, you can view it in Windows or
Web applications by binding data to data-bound controls through a DataView. You can generate one or
multiple views for a DataTables based on the filter and sort criteria.
Applied ADO.NET: Building Data-Driven Solutions
Figure 3-16 shows
bythe relationship
Mahesh between
Chand and David DataSet,
Talbot and DataView objects.
ISBN:1590590732
DataAdapter,
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Figure 3-16: The relationship between DataSet,DataAdapter, and DataView objects
Chapter 1 - ADO.NET Basics
As you 2saw
Chapter earlier,
- Data a DataSet
Components can have
in Visual multiple
Studio .NET DataTable objects. The DataTableCollection
object represents
Chapter 3 - ADO.NETall DataSet objects
in Disconnected related to a DataSet. The Tables property of the DataSet
Environments
represents
Chapter 4 - the collection
ADO.NET of DataTable
in Connected objects. The tables relate to each other with DataRelation
Environments
objects, discussed
Chapter 5
earlier. DataRelationCollection represents all the available relations in the
- Handling ADO.NET Events
DataSet. The Relations property represents the DataRelationCollection of a DataSet. A
Chapter 6 - Integrating XML with ADO.NET
DataSet object stores data in XML format. An XML schema represents a DataSet. A DataSet also
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
defines methods to read and write XML documents. The ReadXml method reads an XML document and
Chapter 8 - Constraints and Data Relations
fills the DataSet with the XML data, and the WriteXML method writes DataSet data to an XML
Chapter 9 - ADO.NET Exception Handling
document. This class also defines the methods ReadXmlSchema and WriteXmlSchema to read and
Chapter 10 - Working with the ODBC .NET Data Provider
write XML schema.
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Creating a DataSet
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing
TheDataSet Database
class provides Web
three Applications
overloaded using ASP.NET
constructors to create a DataSet object. You create a
Chapter 15 - Using ADO.NET in Xml Web Services
DataSet with or without passing a DataSet name in the DataSet contructor. The following code
Chapter
creates16 - ASP.NET
two DataSet Server Controls
objects and Data
in different Binding
ways:
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Dim dtSet1 As DataSet
Chapter
dtSet1 19 =- Mapped Objects: Performance Considerations and Data Binding
New DataSet("DataSet1")
Chapter 20 - COMAs
Dim dtSet2 Interoperability
DataSet = and ADO.NET
New DataSet("DataSet2")
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
The DataSet
Appendix Properties
A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
You justClearned
Appendix howFrequently
- ADO.NET to create aAsked
DataSet object. In most cases, you'll probably create a DataSet and
Questions
use the DataAdapter'sFill and Update methods to fill and save data from a data source. But the
Index
DataSet class provides properties that you can get and set as needed.
List of Figures
List
The ofCaseSensitive
Tables property indicates whether the string comparison within DataTable objects is case
List of Listings
sensitive. This property affects how sorting, searching, and filtering operations are performed on
List of Sidebarsobjects of a DataSet, when |you use the Select method of DataTable to filter and sort
DataTable
data. By default the |CaseSensitive property is False. Use the following to set it to True:

DtSet1.CaseSensitive = True

TheDataSetName property represents the name of a DataSet. The following code first sets a DataSet
name and then displays it in a message box:

Dim dtSet1 As DataSet


dtSet1 = New DataSet("DataSet1")
dtSet1.DataSetName = "CustomDataSet"
MessageBox.Show(dtSet1.DataSetName.ToString())
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
TheDefaultViewManager (readextensive
This text provides only) property returns
coverage a custom
of ADO.NET view of the data of a DataSet. (We
technology
including
discuss this in more detailsADO.NET internals, namespaces, classes, and
in "The DefaultViewManager.")
interfaces, and takes a comprehensive look at XML
namespaces and
TheEnforceConstraints classes,
property is aand how totype
Boolean integrate bothwhich
property, with makes sure constraint rules are
ADO.NET.
followed when data of a DataSet is being updated. The default value of the EnforceConstraints
property is True. If you don't want the Update method to check the constaints, just set this property as
False.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Do you remember the ExtendedProperties property of the DataTable? The
Introduction
ExtendedProperties property attaches some additional custom information about a DataTable.
Chapter
Similar1to the
- ADO.NET Basics the DataSet also provides the ExtendedProperties property. Listing 3-28
DataTable,
Chapter 2 reads
sets and - Datathe
Components in Visual of
custom properties Studio .NET
a DataSet.
Chapter 3 - ADO.NET in Disconnected Environments
Listing43-28:
Chapter Settinginand
- ADO.NET Reading
Connected Custom Properties of a DataSet
Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating
Dim dtSet1 XML with ADO.NET
As DataSet
Chapter
dtSet1 7 =- Data
New Binding and Windows Forms Data-Bound Controls
DataSet("DataSet1")
Chapter 8 - Constraints and=Data
dtSet1.DataSetName Relations
"CustomDataSet"
Chapter 9 - ADO.NET Exception Handling
dtSet1.ExtendedProperties.Add("Description", "The Name Column")
Chapter 10 - Working with the ODBC .NET Data Provider "Mahesh Chand")
dtSet1.ExtendedProperties.Add("Author",
dtSet1.ExtendedProperties.Add("UserId",
Chapter 11 - Stored Procedures and Views "MCB")
dtSet1.ExtendedProperties.Add("PWD",
Chapter "Password")
12 - Oracle, SQLXML, and Other .NET Data Providers
' Remove
Chapter Author property
13 - Developing a Custom Data Provider
dtSet1.ExtendedProperties.Remove("Author")
Chapter 14 - Developing Database Web Applications using ASP.NET
' Read
Chapter 15 custom properties
- Using ADO.NET in Xml Web Services
Dim str As String
Chapter 16 - ASP.NET Server Controls and Data Binding
Dim i As Integer
Chapter 17 - Building Real-World Web Applications
str = dtSet1.ExtendedProperties("Description").ToString()
Chapter 18 - Object-Relational Mapping in .NET
str = str + ", " + dtSet1.ExtendedProperties("UserId").ToString()
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
str = str + ", " + dtSet1.ExtendedProperties("PWD").ToString()
Chapter 20 - COM Interoperability and ADO.NET
MessageBox.Show(str)
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
TheHasErrors
Appendix property
B - Commonly Usedfinds
SQLout if any of the rows of a DataSet has any errors. The DataTable also
Statements
providesCthis
Appendix propertyFrequently
- ADO.NET to check ifAsked
a particular row has any errors. (We show you how to use this property in
Questions
a moment.)
Index
List of Figures
ACultureInfo object contains the data about a machine locale. The Locale property gets and sets
List of Tables
this data. The following code uses this property to read the information:
List of Listings
List of Sidebars
Dim cInfo As System.Globalization.CultureInfo
cInfo = dtSet1.Locale
Console.WriteLine(cInfo.DisplayName, cInfo.EnglishName)

As mentioned earlier (and in more detail in Chapter 6), a DataSet is represented by an XML schema,
which has a namespace and prefix. The Namespace and Prefix properties represent the namespace
and prefix of the schema, respectively. These properties are both readable and writeable:

dtSet1.Namespace = "CustDSNamespace"
dtSet1.Prefix = "CustPrefix"
Applied ADO.NET: Building Data-Driven Solutions
TheRelations by property
Maheshreturns
Chandaand collection of relations that aISBN:1590590732
David Talbot DataSet has. (We discuss this property
in later sections and in Chapter
Apress 4.)pages)
2003 (928
This text provides extensive coverage of ADO.NET technology
As discussed earlier, the Tables
including ADO.NETproperty
internals,ofnamespaces,
a DataSet classes,
represents
anda collection of DataTables attached
to a DataSet. The Tablesand
interfaces, property
takes aiscomprehensive
of type DataTableCollection,
look at XML which was discussed earlier.
namespaces and
TheDataTableCollection classes,
class and how
provides to integrate
methods to add both with
and remove DataTable objects to a
ADO.NET.
collection.

Attaching
Table a DataTable
of Contents to a DataSet
Applied ADO.NETBuilding Data-Driven Solutions
You've already seen how to create a DataTable and add columns and rows to a DataTable by using
Introduction
theDataRow and DataColumn. As mentioned earlier, a DataSet is a collection of DataTable objects.
Chapter 1 - ADO.NET
TheTables propertyBasics
of DataSet gets all the attached DataTables of a DataSet. The Tables
Chapter 2 which
property, - DataisComponents in Visual Studio .NET
a type of DataTableCollection, also adds DataTable objects to a DataSet and
Chapter
removes3 them.
- ADO.NET
Listingin3-29
Disconnected Environments
creates four DataTable objects and adds them to a DataSet using the
Chapter 4 - ADO.NET in Connected
DataSet.Tables.Add method. Environments
The DataSet is bound to a DataGrid to view the contents in a
Chapter 5 - Handling ADO.NET Events
DataGrid.
Chapter 6 - Integrating XML with ADO.NET
Listing73-29:
Chapter Adding
- Data DataTable
Binding Objects
and Windows Formsto a DataSet
Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET
Dim tbl1 Exception =Handling
As DataTable New DataTable("One")
Dim tbl2
Chapter As DataTable
10 - Working = New
with the ODBC .NETDataTable("Two")
Data Provider
Dim tbl3
Chapter As DataTable
11 - Stored = New
Procedures and ViewsDataTable("Three")
Dim dtSet1
Chapter As SQLXML,
12 - Oracle, DataSetand
= Other
New .NET
DataSet("CustDtSet")
Data Providers
dtSet1.Tables.Add(custTable)
Chapter 13 - Developing a Custom Data Provider
dtSet1.Tables.Add(tbl1)
Chapter 14 - Developing Database Web Applications using ASP.NET
dtSet1.Tables.Add(tbl2)
Chapter 15 - Using ADO.NET in Xml Web Services
dtSet1.Tables.Add(tbl3)
Chapter 16 - ASP.NET Server Controls and Data Binding
DataGrid1.DataSource = dtSet1.DefaultViewManager
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM
In this code, Interoperability
custTable and ADO.NET
is a DataTable created using the methods discussed.
Chapter 21 - Messaging
The output
Chapter 22 - of
SQLListing 3-29
Server andlooks like Figure
ADO.NET: Notes 3-17. If you click a DataTable link, you can see the data of
on Performance
theDataTable
Appendix (if it has
A - Relational any). Some Basic Concepts
Databases:
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - 3-17:
Figure Data Binding and
Attaching Windows Forms
DataTables to aData-Bound
DataSet Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Copying,
Chapter Cloning,
10 - Working with and Clearing
the ODBC DataSets
.NET Data Provider
Chapter 11 - Stored Procedures and Views
TheClear method removes all the rows from a DataSet; however, it does not destroy the structure of a
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
DataSet. The Copy method copies the contents with schema of a DataSet to another DataSet. The
Chapter 13 - Developing a Custom Data Provider
Clone method only copies the structure of a DataSet to another. Listing 3-30 uses all three members.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter
Listing15 - Using
3-30: ADO.NET
Using in Xml Web
Copy,Clone, andServices
Clear Methods of DataSet
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Dim dtSet1 As DataSet = New DataSet("CustDtSet")
Chapter 18 - Object-Relational Mapping in .NET
dtSet1.Tables.Add(custTable)
Chapter 19 - Mapped
Dim dtSet2 Objects: Performance
As DataSet Considerations and Data Binding
= New DataSet()
Chapter
dtSet2 20 =- COM Interoperability and ADO.NET
dtSet1.Clone()
Chapter
dtSet2 21 =- Messaging
dtSet1.Copy()
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
dtSet1.Clear()
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Getting
Index Changed Rows of a DataSet
List of Figures
So far, we've talked about filling data from a DataAdapter to a DataSet and saving data back to a data
List of Tables
source using the Update method of DataSet. The Update method takes a DataSet and saves its
List of Listings
results in a data source. A DataAdapter belongs to a data provider, so we discuss it in Chapter 4.
List of Sidebars
TheGetChanges method helps you find the changed data of a DataSet so you can send only changed
data back to the data source instead of sending an entire DataSet. It helps to reduce the network traffic
and make the save process faster.

TheGetChanges method returns a DataSet, which contains all the changes made to a DataSet since
it was last loaded or the AcceptChanges method was called.

TheGetChanges method has two overloaded forms. The first form takes no parameters and returns all
the results. The second form lets you filter the results based on the row states, and it takes a parameter of
typeDataRowState.

TheHasChanges method determines if a DataSet has any changes since it was last saved or loaded.
The following snippet shows how to use the HasChanges and GetChanges methods (you'll see these
methods in moreApplied
detail in ADO.NET:
Chapter 4).Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
If Not dtSet1.HasChanges(DataRowState.Modified) Then Exit Sub
This text provides extensive coverage of ADO.NET technology
Dim dtSet2 Asincluding
DataSetADO.NET internals, namespaces, classes, and
dtSet2 = dtSet1.GetChanges(DataRowState.Modified)
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
SqlDbDataAdapter1.Update(dtSet2)
ADO.NET.

Merging a DataRow, DataTable, or a DataSet


Table of Contents
Applied ADO.NETBuilding
TheMerge method merges Data-Driven Solutions
two DataSet objects and saves the results in one DataSet. The Merge
Introduction
method has seven overloaded forms. These forms allow you to merge a DataRow, a DataTable, and a
Chapter
DataSet1 -with
ADO.NET Basics
an existing DataSet. The first parameter of the Merge method is a DataRow,
Chapter 2 - Data
DataTable, Components
or DataSet. in Visual
The secondStudio .NETparameter is a Boolean type-a True value indicates that
optional
the changes
Chapter should be
3 - ADO.NET preserved and
in Disconnected False indicates that changes won't be preserved. The third
Environments
optional4 parameter
Chapter - ADO.NETisin
a Connected
type of MissingSchemaAction,
Environments which indicates what action should be taken
when the
Chapter 5 schema of ADO.NET
- Handling added DataRow,
Events DataTable, or DataSet does not match the schema of the
current6DataSet.
Chapter The MissingSchemaAction
- Integrating XML with ADO.NET enumeration has four values: Add,AddWithKey,
Error,7and
Chapter Ignore.
- Data The
Binding Add
and value adds
Windows Formsnecessary columns
Data-Bound Controlsto complete the schema. The
AddWithKey
Chapter value adds
8 - Constraints andthe necessary
Data Relationsinformation with a primary key. The Error value generates an
error. The -Ignore
Chapter 9
value ignores the extra columns.
ADO.NET Exception Handling
Chapter
Listing 10 - Working
3-31 merges with the ODBC .NET
two DataSet Data
objects. Provider
The custTable and ordersTables are two DataSet
Chapter 11 - Stored Procedures and Views
objects created by the CreateCustomersTable() and CreateOrdersTable() methods, as
Chapter 12 -inOracle,
discussed SQLXML, The
"The DataSet: and Other
Heart .NET Data Providers
of ADO.NET" section.
Chapter 13 - Developing a Custom Data Provider
Listing14
Chapter 3-31: MergingDatabase
- Developing Two DataSetse
Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET
Dim dtSet1 Server Controls
As DataSet = New andDataSet("EmpDataSet")
Data Binding
Chapter 17 - Building
Dim dtSet2 Real-World=Web
As DataSet NewApplications
DataSet("OrdersDataSet")
Chapter 18 - Object-Relational Mapping in .NET
dtSet1.Tables.Add(custTable)
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
dtSet2.Tables.Add(ordersTable)
dtSet1.Merge(dtSet2,
Chapter False,
20 - COM Interoperability MissingSchemaAction.Add)
and ADO.NET
DataGrid1.DataSource
Chapter 21 - Messaging = dtSet1.DefaultViewManager
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
As mentioned, the Merge method also lets you merge an array of a DataRow or a DataTable object.
Appendix C - ADO.NET Frequently Asked Questions
Listing 3-32 shows you how to merge rows and a DataTable with a DataSet.
Index
List of Figures
Listing 3-32: MergingDataRow and DataTable Objects
List of Tables
List of Listings
Dim str As String = "OrderId >= 2 "
List of Sidebars
Dim rows() As DataRow = ordersTable.Select(str)
dtSet1.Tables.Add(custTable)
dtSet2.Tables.Add(ordersTable)
dtSet1.Merge(rows)
dtSet1.Merge(ordersTable, True, MissingSchemaAction.Ignore)

Other DataSet Methods


There are a few more DataSet methods left to discuss. The AcceptChanges method saves all the
changes made to a DataSet since it was loaded or the last time AcceptChanges was called. The
RejectChanges method rolls back all the changes made to a DataSet since it was created or the last
Applied
timeAcceptChanges ADO.NET:
was Building
called. These Data-Driven
methods Solutions
are discussed in more details in next chapter.
by Mahesh Chand and David Talbot ISBN:1590590732
TheReadXml andApress
ReadXmlSchema methods read an XML document and XML schema document,
2003 (928 pages)
respectively. TheThis
WriteXml and WriteXmlSchema
text provides methods
extensive coverage of ADO.NETsave DataSet data to XML document and
technology
includingrespectively.
XML schema document, ADO.NET internals, namespaces,
(We discuss classes, and
these methods in Chapter 6 in more detail.)
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
Typed and Untyped DataSets
ADO.NET.

As you may recall from earlier discussions, there are two kinds of a DataSet: typed or untyped. A typed
Table of Contents
DataSet first comes from the DataSet class and then uses an XML schema (.xsd file) to generate a
Applied ADO.NETBuilding
new class. You saw how Data-Driven Solutions
to create a typed DataSet using VS .NET in Chapter 2. An untyped DataSet
Introduction
has no built-in schema. You create an instance of a DataSet class and call its methods and properties to
Chapter 1 the
work with - ADO.NET Basics All elements of an untyped DataSet are collections. In this chapter, you'll
data sources.
work with
Chapter 2 untyped
- Data Components
DataSets.in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Both kinds-of
Chapter 4
DataSet have their own advantages and disadvantages. Typed DataSets take less time to
ADO.NET in Connected Environments
write applications but offer no flexibility. They're useful when you already know the schema of a database.
Chapter 5 - Handling ADO.NET Events
The biggest advantage of typed DataSets is the VS .NET IDE support. As you saw in Chapter 2, you can
Chapter 6 - Integrating XML with ADO.NET
drag a database table, its columns, or stored procedures to a form in your application, and the IDE
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
generates typed DataSet for you. After that, you can bind these DataSets to the controls. However,
Chapter 8 - Constraints and Data Relations
there are many occasions when you don't know the schema of a database. In those cases, the untyped
Chapter 9 - are
DataSets ADO.NET
useful.Exception Handling
The untyped DataSets also provide the flexibility of connecting with multiple data
Chapter 10 - Working with the ODBC
sources. And you can use them without .NET Data Provider
the VS .NET IDE.
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
The DataView
Chapter 13 - Developing a Custom Data Provider
Chapter
Another14powerful
- Developing Database
feature Web Applications
of ADO.NET using
is the ability ASP.NET
to create several different views of the same data. You
Chapter 15 - Using ADO.NET in Xml Web Services
can sort these views differently and filter them on different criteria. They can contain different row state
Chapter 16 - ASP.NET Server Controls and Data Binding
information.
Chapter 17 - Building Real-World Web Applications
ADataView
Chapter represents a customized
18 - Object-Relational Mapping inview
.NETof DataTable that you can bind to Windows Forms and Web
Forms 19
Chapter controls. Using
- Mapped DataView
Objects: sort and
Performance filter features,
Considerations andyou canBinding
Data also have multiple views of a single
DataTable. Using RowFilter and Sort properties, you can apply a filter on a DataView and sort its
Chapter 20 - COM Interoperability and ADO.NET
contents before binding it to a data-bound control. The AddNew method adds a new row to a DataView,
Chapter 21 - Messaging
and the Delete method deletes a row from a DataView. You can use the Find and FindRows
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
methods to search for rows based on the defined criteria.
Appendix A - Relational Databases: Some Basic Concepts
Appendix B -describes
Table 3-9 Commonly Usedof
some SQL
theStatements
DataView properties, and Table 3-10 describes some of its methods.
Appendix C - ADO.NET Frequently Asked Questions
Index
Table 3-9: The DataView Class Properties
List of Figures
List of Tables
List of Listings
List of Sidebars
PROPERTY DESCRIPTION
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
AllowDelete Indicates whether deletes are allowed.
Apress 2003 (928 pages)
AllowEdit Indicates
This text provides whethercoverage
extensive edits areofallowed.
ADO.NET technology
including ADO.NET internals, namespaces, classes, and
AddNews interfaces, Indicates
and takeswhether new rowslook
a comprehensive canatbeXML
added.
namespaces and classes, and how to integrate both with
ADO.NET. Indicates whether to use default sort. True means use the default sort
ApplyDefaultSort
andFalse means no.
Count
Table of Contents Represents the number of records in a DataView after RowFilter
andRowStateFilter
Applied ADO.NETBuilding Data-Driven Solutions have been applied.
Introduction
DataViewManager DataViewManager associated with this view.
Chapter 1 - ADO.NET Basics
Item
Chapter 2 Represents
- Data Components an item
in Visual Studio of a row.
.NET
Chapter 3 - ADO.NET in Disconnected
RowFilter Environments
Represents the expression used to filter rows to view in the DataView.
Chapter 4 - ADO.NET in Connected Environments
RowStateFilter
Chapter 5
You can use a row state filter using this property.
- Handling ADO.NET Events
Chapter
Sort6 - Integrating XML Represents
with ADO.NET
the sort column and sort order.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Table DataTable attached with this view.
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Table 3-10: The DataView Class Methods
Chapter 11 - Stored Procedures and Views
Chapter
METHOD12 - Oracle,DESCRIPTION
SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
AddNew Adds a new row to the DataView
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using Begins
BeginInit ADO.NETtheinintialization
Xml Web Services
if a data view was previously used
Chapter 16 - ASP.NET Server Controls and Data Binding
EndInit Ends the initialization started by BeginInit
Chapter 17 - Building Real-World Web Applications
Delete
Chapter Deletes a row
18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Find Finds a row in the DataView based on the specified criteria
Chapter 20 - COM Interoperability and ADO.NET
FindRows
Chapter Returns an array of rows based on the specified criteria
21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Creating
Appendix a DataView
B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
TheDataView constructor allows you to create a DataView object from a DataTable, which you can
Index
use with or without a DataSet. In Listing 3-33, the custTable and ordersTable are two DataTable
List of Figures
objects. As you can see, the code creates a DataView directly from a DataTable and creates a second
List of Tables through a DataSet.
DataView
List of Listings
Listing
List 3-33: CreatingDataView Objects from a DataTable
of Sidebars

' Creating a DataView from DataTable


Dim dtView1 As DataView = New DataView(custTable)
' Creating a DataView through DataSet
Dim dtSet1 As DataSet = New DataSet()
dtSet1.Tables.Add(ordersTable)
Dim dtView2 As DataView = New DataView(dtSet1.Tables(0))

Binding a DataView with a data-bound control is pretty easy. You set the DataSource and
DisplayMember properties of a data-bound control. For example, the following code binds two
Applied
DataView objects with a ADO.NET:
DataGridBuilding Data-Driven
and a ListBox Solutions
control.
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
DataGrid1.DataSource = dtView1
This text provides extensive coverage of ADO.NET technology
ListBox1.DataSource = dtView2
including ADO.NET internals, namespaces, classes, and
ListBox1.DisplayMember = "Name"
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Chapter 7 discusses data-bound controls in more detail.

Table of Contents
Adding, Editing, and Deleting Data Using DataView
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
TheAddNew method of a DataView, which returns a DataRowView, adds a new row to the DataView.
Chapter 1 - ADO.NET Basics
TheAddNew method is followed by a call of EndEdit. The BeginEdit and EndEdit pair edits the
Chapter
values 2of a- row.
Data The
Components in VisualAllowNew,
AllowDelete, Studio .NET and AllowEdit properties allow deleting, adding, and
Chapter 3 in- a
updating ADO.NET in Disconnected
DataView if they're setEnvironments
to True.
Chapter 4 - ADO.NET in Connected Environments
TheDelete
Chapter method ADO.NET
5 - Handling of a DataView
Events deletes a row specified by the index of the row. The Count property
returns6the- total
Chapter number
Integrating of rows
XML in a DataView.
with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Listing 3-34 uses the previously discussed methods and properties to add, delete, and edit rows of a
Chapter 8 - Constraints and Data Relations
DataView.
Chapter 9 - ADO.NET Exception Handling
Chapter
Listing10 - Working
3-34: with
Adding, the ODBCand
Updating, .NETDeleting
Data Provider
Rows of a DataView
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
' Creating a DataView from DataTable
Chapter 13 - Developing a Custom Data Provider
Dim dtView1 As DataView = New DataView(custTable)
Chapter 14 - Developing
' Creating Database
a DataView Web Applications
through DataSet using ASP.NET
Chapter 15 - Using ADO.NET in Xml
Dim dtSet1 As DataSet = New DataSet() Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
dtSet1.Tables.Add(ordersTable)
Chapter 17 - Building
Dim dtView2 As Real-World
DataViewWeb = Applications
New DataView(dtSet1.Tables(0))
Chapter
' Set18AllowDelete,
- Object-Relational Mapping inand
AllowNew .NET AllowEdit to true
Chapter 19 - Mapped Objects:=Performance
dtView1.AllowDelete True Considerations and Data Binding
dtView1.AllowNew
Chapter = True and ADO.NET
20 - COM Interoperability
dtView1.AllowEdit
Chapter 21 - Messaging = True
' Edit
Chapter 22 the
- SQLdata
Serverofandfirst row
ADO.NET: Notes on Performance
dtView1(0).BeginEdit()
Appendix A - Relational Databases: Some Basic Concepts
dtView1(0)("Name")
Appendix B - Commonly Used = SQL
"Edited Name"
Statements
dtView1(0)("Address") = "Edited
Appendix C - ADO.NET Frequently Asked Questions
Address"
dtView1(0).EndEdit()
Index
' Delete the first row from DataView
List of Figures
' the Delete method takes an index of row starting
List of Tables
' at 0
List of Listings
If dtView1.AllowDelete Then
List of Sidebars
dtView1.Delete(0)
End If
' Add a new row
Dim drv As DataRowView = dtView1.AddNew
' Change values in the DataRow.
drv("id") = 1010
drv("Name") = "New Name"
drv("Address") = "New Address"
drv.EndEdit()
' Count number of rows
MessageBox.Show(dtView1.Count.ToString())
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh
Searching and SortingChand
Dataand David Talbot
Using DataView ISBN:1590590732
Apress 2003 (928 pages)
Searching and sorting areprovides
This text two major operations
extensive that aofDataView
coverage does to filter data. The Sort property of
ADO.NET technology
DataView sorts including
rows of aADO.NET
DataView.internals, namespaces,
The Sort propertyclasses,
takes aand
string value, which contains the sort
interfaces,
criteria with one or more than and takes
one a comprehensive
DataTable columns look at XML by a comma (,) and followed by ASC
separated
namespaces and classes, and how to integrate both with
or DESC, where ASC is for sorting rows in ascending order and DESC for sorting rows in descending
ADO.NET.
order. The following code shows how to use one or more than one column with the ASC and DESC
options:
Table of Contents
Applied ADO.NETBuilding
dtView1.Sort = "Name,Data-Driven
AddressSolutions
ASC"
Introduction
dtView1.Sort = "Name DESC"
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
TheRowFilter
Chapter property
3 - ADO.NET of a DataView
in Disconnected filters the rows depending on the given criteria. The
Environments
RowFilter
Chapter takes a criteria,
4 - ADO.NET whichEnvironments
in Connected contains columns and their values. The following code returns all rows
that contain
Chapter Name column
5 - Handling values
ADO.NET of Miranda:
Events
Chapter 6 - Integrating XML with ADO.NET
dtView1.RowFilter
Chapter = "Name
7 - Data Binding and = Forms
Windows 'Miranda'"
Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Besides the Sort and RowFilter properties, the DataView class provides Find and FindRow
Chapter 10 - Working with the ODBC .NET Data Provider
methods to find rows. The Find method requires a Sort field and takes a parameter of type Object or
Chapter 11of- Object.
an array Stored Procedures and Views
The FindRow method returns an array of DataRowView, whose column matches
Chapter 12 - Oracle, SQLXML,
the specified sort key value. and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Listing 14
Chapter 3-35 uses the Find
- Developing method
Database WebtoApplications
find the value "Data
using Quest" in the column Name, which returns an
ASP.NET
index of15the
Chapter row. ADO.NET in Xml Web Services
- Using
Chapter 16 - ASP.NET Server Controls and Data Binding
Listing 3-35: Finding Rows in a DataView Using the Find Method
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
' Creating
Chapter a DataView
19 - Mapped from DataTable
Objects: Performance Considerations and Data Binding
Dim dtView1
Chapter 20 - COM As DataView and
Interoperability = New DataView(custTable)
ADO.NET
' Creating
Chapter a DataView through DataSet
21 - Messaging
Dim dtSet1 As DataSet = New DataSet()
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
dtSet1.Tables.Add(ordersTable)
Appendix A - Relational Databases: Some Basic Concepts
Dim dtView2 As DataView = New DataView(dtSet1.Tables(0))
Appendix B - Commonly Used SQL Statements
dtView2.Sort = "Name"
Appendix C - ADO.NET Frequently Asked Questions
Dim val As Object = "Data Quest"
Index
Dim pos As Integer = dtView2.Find(Val)
List of Figures
MessageBox.Show(pos.ToString())
List of Tables
List of Listings
List of Sidebars

The DataViewManager
ADataViewManager contains a collection of views of a DataSet-one view for each DataTable in the
DataSet. The DataViewManager has a DataViewSettings property that enables the user to
construct a different view for each DataTable in the DataSet. If you want to create two views on the
same DataTable, you need to create another instance of the DataViewManager and construct the
DataViewSettings for that particular view. Then you construct a DataView using the
DataViewManager. For example, in the Orders DataSet, you may want to filter out the orders with an
EmployeeID of 4 and sort the orders by the date they were shipped. You can retrieve records for
EmployeedId = 4 sorted on the date they were shipped using the sort and filter properties of the DataSet
and attaching the filtered and sorted data to a DataView or a DataViewManager.
To construct a DataViewManager, you can either
Applied ADO.NET: Building use the Solutions
Data-Driven Default constructor or pass in a DataSet
object. For example:
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
view = new DataViewManager ()
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
or, for example: namespaces and classes, and how to integrate both with
ADO.NET.

view = new DataSetView(myDataSet)


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
TheDataViewManager has a few properties you need to know about to utilize it effectively. Table 3-11
Introduction
shows the
Chapter 1 -main properties.
ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Table 3-11: DataSetView Properties
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected
PROPERTIES Environments
DESCRIPTION
Chapter 5 - Handling ADO.NET Events
DataSet- Integrating XML The
Chapter 6
DataSet being viewed of type DataSet.
with ADO.NET
Chapter 7 - Data Binding andContains
DataViewSettings WindowstheForms Data-Bound
collection Controls objects for each table in the
of TableSetting
Chapter 8 - Constraints and DataSet.
Data Relations
The TableSetting object contains sorting and filtering criteria
Chapter 9 - ADO.NET Exception
for aHandling
particular table.
Chapter 10 - Working with the ODBC .NET Data Provider
DataViewManager
Chapter contains and
11 - Stored Procedures the Views
CreateDataView method, which allows you to create a DataView
object for
Chapter 12 a particular
- Oracle, table inand
SQLXML, your DataSet.
Other You
.NET Data can construct the DataView for the table with the
Providers
DataViewManager's
Chapter a Custom Data Providerfor the particular DataTable of the DataSet. You can also
13 - DevelopingDataViewSettings
adjust settings
Chapter for the DataView
14 - Developing by assigning
Database Web filter
Applications andASP.NET
using sort properties directly in the DataView.
Chapter 15 - Using ADO.NET in Xml Web Services
You'll see DataView and DataViewManager objects in the sample applications in Chapter 4.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Summary Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
In this chapter you learned
Apress about
2003 (928 the ADO.NET model class hierarchy. ADO.NET consists of three
pages)
general namespaces and many data provider
This text provides extensive namespaces.
coverage There
of ADO.NET are two general namespaces-
technology
System.Data and System.Data.Common-and a SQL Server-specific
including ADO.NET internals, namespaces, classes, and namespace called
interfaces, and
System.Data.SqlTypes. Thetakes a comprehensive
System.Data look at XML
namespace defines classes that are the basic building
namespaces
clocks of the ADO.NET model.and classes,
The and how to integrate both
System.Data.Common with
namespace defines classes shared by all
ADO.NET.
data providers. The System.SqlTypes namespace defines classes that convert from SQL native data
types to .NET data types.
Table of Contents
You can divide ADO.NET classes into two categories: disconnected and connected. You can use
Applied ADO.NETBuilding Data-Driven Solutions
disconnected classes with or without data providers, but connected classes work with the data providers.
Introduction
Some of the disconnected classes are DataSet,DataTable,DataRow,DataColumn,DataView, and
Chapter 1 - ADO.NET Basics
DataViewManager.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET
In this chapter, in Disconnected
you saw how to work Environments
with these disconnected classes. You learned how to create in-
memory
Chapter 4 tables with the
- ADO.NET help of theEnvironments
in Connected DataTable,DataRow, and DataColumn classes. After that you
worked5with
Chapter the DataView,
- Handling Events and DataViewManager classes.
DataSet,
ADO.NET
Chapter 6 - Integrating XML with ADO.NET
Chapter 4 covers the ADO.NET data providers and working with ADO.NET in a connected environment.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Chapter Applied
4: ADO.NET in Connected
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot
Environments
ISBN:1590590732
Apress 2003 (928 pages)
Overview This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
As mentioned earlier, you can
interfaces, anddivide
takes ADO.NET functionality
a comprehensive look atinto
XMLtwo categories: connected and
disconnected. In namespaces andexamined
Chapter 3, you classes, and
thehow to integratefunctionality
disconnected both with provided by the .NET Framework
ADO.NET.
that resides in the System.Data and System.Data.Common namespaces. In disconnected
environments, the data is stored in memory.
Table of Contents
In this chapter, we explain ADO.NET functionality in connected environments, which means reading and
Applied ADO.NETBuilding Data-Driven Solutions
storing data in data sources. Specifically, you access data from a data source and save data back to the
Introduction
data source with the help of a bridge between the application and the data source; in ADO.NET this bridge
Chapter 1 provider.
is a data - ADO.NET Basics provides many data providers for different data sources to make data access
ADO.NET
Chapter 2 - Data
fast, reliable, andComponents
easy to use.inEach
Visualdata
Studio .NET has data components (classes) that let you connect to a
provider
Chapter 3 - ADO.NET
data source, in Disconnected
as well as Environments
read, write, add, delete, and update data. In this chapter, you'll learn about these
Chapter 4 - ADO.NET
components and howintoConnected
work withEnvironments
them.
Chapter 5 - Handling ADO.NET Events
So what
Chapter 6 are the ADO.NET
- Integrating XML data
with providers?
ADO.NET Technically, an ADO.NET data provider is a set of classes that
enables7 you
Chapter to connect
- Data Binding to a data
and source
Windows in order
Forms to readControls
Data-Bound and write data from a data source. A data
provider also has components that serve as conduits between the data source and the DataSet. In this
Chapter 8 - Constraints and Data Relations
way, the architecture isolates the manipulation of data from the data source.
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Understanding
Appliedthe Generic
ADO.NET: Data
Building Provider
Data-Driven Model
Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Let's examine a generic
Apress data
2003 provider
(928 pages)model before looking at specific data providers. Figure 4-1 shows a
generic class model of a data provider.
This text provides The coverage
extensive same component
of ADO.NET model applies to all of the data providers,
technology
with minor changes. All data providers implement a similar class
including ADO.NET internals, namespaces, classes, and hierarchy model, so once you're
comfortable with interfaces, and takesyou
one data provider, a comprehensive look at XML
can easily manipulate the other data providers in no time. It's
namespaces
usually just a matter and the
of changing classes,
classand how to
names and integrate both withstring.
the connection
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Figure
Chapter 13 - 4-1: A generic
Developing data provider
a Custom model
Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET
TheConnection in Xml
component of aWeb
dataServices
provider is the first component, and it communicates with a data
Chapter
source.16 - ASP.NET Serversits
A DataAdapter Controls andaData
between Binding
DataSet and the data source and passes data back and forth.
TheDataAdapter's
Chapter Fill method
17 - Building Real-World Web fills data from a data source to a DataSet, and the Update method
Applications
of the DataAdapter
Chapter saves changes
18 - Object-Relational from
Mapping in a DataSet to a data source. In Chapter 3, you saw how to use
.NET
the data
Chapter 19once you have
- Mapped it in aPerformance
Objects: DataSet through the DataTable,
Considerations DataRow, and DataColumn objects.
and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
TheCommand and CommandBuilder objects are two more important components of a data provider. A
Chapter 21 - Messaging
Command component uses SQL queries to transfer data back and forth from a data source to an
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
application and vice versa. A Command or CommandBuilder object can use a DataAdapter or a
Appendix A - Relational Databases: Some Basic Concepts
DataReader. The DataReader is a fast, read-only gateway of data.
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET
You'll begin Frequently Asked
your understanding of the Questions
ADO.NET data providers by examining the Sql data provider
defined in the System.Data.SqlClient namespace. This name-space contains the classes necessary
Index
to manipulate
List of Figures the data providers.
List of Tables
List of Listings
List of Sidebars
Importing aApplied
Namespace
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Before anything else,
Apressyou need
2003 to pages)
(928 decide what data provider you'll use in your applications. Once you've
decided, you need to include the namespace in your applications
This text provides extensive coverage so the namespace objects are available
of ADO.NET technology
to you. You include a namespace by adding an Imports directive,
including ADO.NET internals, namespaces, classes, and followed by the namespace, to your
interfaces,
project. The following and takes
lines import theaSystem.Data,
comprehensive System.Data.Common,
look at XML
namespaces and
System.Data.SqlClient, andclasses, and how to integrate
System.Data.OleDb both with of ADO.NET:
namespaces
ADO.NET.

Imports System.Data
Table of Contents
Imports System.Data.Common
Applied
ImportsADO.NETBuilding Data-Driven Solutions
System.Data.SqlClient
Introduction
Imports System.Data.OleDb
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
In later chapters,
Chapter 3
we discuss other data providers-such as Odbc and Oracle- and their related
- ADO.NET in Disconnected Environments
namespaces.
Chapter 4 - ADO.NET in Connected Environments
Chapter
You can5 also
- Handling
accessADO.NET Events objects by specifying the full namespace when you declare the
a namespace's
Chapter 6 - Integrating XML with ADO.NET
objects. For example, the following code accesses the SqlConnection class of the
Chapter 7 - Data Binding and Windows
System.Data.SqlClient namespaceForms
(inData-Bound Controls
this case, you don't need to use the Imports statement):
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET
Dim conn Exception Handling
As SqlConnection = New SqlClient.SqlConnection()
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
The previous
Chapter code SQLXML,
12 - Oracle, is equivalent
and to the .NET
Other following
Data code:
Providers
Chapter 13 - Developing a Custom Data Provider
Chapter
Imports14 -System.Data.SqlClient
Developing Database Web Applications using ASP.NET
Dim conn
Chapter As SqlConnection
15 - Using = Services
ADO.NET in Xml Web New SqlConnection()
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Exploring the System.SqlClient
Applied Namespace
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
In this chapter, we use the
Apress SQL
2003 Server
(928 pages) database as the data source to test the code samples. Hence, we
use the System.Data.SqlClient (or justcoverage
This text provides extensive SqlClient) name-space
of ADO.NET most of the time. Before going any
technology
further, let's explore the SqlClient namespace and see what it
including ADO.NET internals, namespaces, classes, and has to offer.
interfaces, and takes a comprehensive look at XML
Table 4-1 describes the SqlClient
namespaces namespace
and classes, and how classes.
to integrate both with
ADO.NET.
Table 4-1: Sql Data Provider Classes
Table of Contents
CLASS DESCRIPTION
Applied ADO.NETBuilding Data-Driven Solutions
SqlClientPermission
Introduction Makes sure the user who is accessing the data has
Chapter 1 - ADO.NET Basics permission to access a data source.
Chapter 2 - Data Components in Visual StudioSets
SqlClientPermissionAttribute .NETpermission attributes.
Chapter 3 - ADO.NET in Disconnected Environments
SqlCommand
Chapter 4 Represents a SQL statement or stored procedure.
- ADO.NET in Connected Environments
Data can be returned in a DataReader by calling its
Chapter 5 - Handling ADO.NET Events
ExecuteReader method.
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Provides
SqlCommandBuilder Data-Bound Controls
a mechanism to generate automatic
Chapter 8 - Constraints and Data Relations commands for a table.
Chapter 9 - ADO.NET Exception Handling
SqlConnection Represents a Connection object. This is first class
Chapter 10 - Working with the ODBC .NET Data Provider
you need to use to connect to a data source.
Chapter 11 - Stored Procedures and Views
SqlDataAdapter
Chapter Set ofProviders
12 - Oracle, SQLXML, and Other .NET Data commands that provides a link between
SqlConnection and DataSet.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications
SqlDataReader Reads using
dataASP.NET
in a forward-only direction through
Chapter 15 - Using ADO.NET in Xml Web Services
SqlCommand.
Chapter 16 - ASP.NET Server Controls and Data Binding
SqlError Collects error and warnings returned by the data
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
source.
Chapter 19 - Mapped Objects: Performance Considerations
SqlErrorCollection andgenerated
Collects errors Data Binding
by Sql data providers.
Chapter 20 - COM Interoperability and ADO.NET
SqlException Exception handling class designed for Sql data
Chapter 21 - Messaging
sources.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
A - Relational Databases: Some BasicProvides
SqlInfoMessageEventArgs
Appendix Conceptsdata for the InfoMessage event.
Appendix B - Commonly Used SQL Statements
SqlParameter This class represents a parameter used in
Appendix C - ADO.NET Frequently Asked Questions
SqlCommand.
Index
ListSqlParameterCollection
of Figures A collection of parameters.
ListSqlRowUpdateEventArgs
of Tables Provides data for the RowUpdated event.
List of Listings
ListSqlRowUpdatingEventArgs
of Sidebars Provides data from the RowUpdating event.
SqlTransaction A transaction that can be processed at a data source.

Like the Sql data provider, the OleDb, ODBC, and other data providers implement the same class
hierarchy model. Programmatically, the only difference is the prefix for each data provider namespace and
the connection string. Internally, each data provider uses a different native data access technology to
access the data source. For example, the OleDb provider classes begin with OleDb, the Sql Server
provider namespace classes start with Sql, and the ODBC provider classes begin with Odbc . The
System.Data classes have no provider-specific prefix, of course, because the classes defined in the
System.Data and System.Data.Common namespaces are used in memory and are shared by all the
providers.
The way ADO.NET works is that once you've connected to a database and dumped the data into the in-
Applied ADO.NET: Building Data-Driven Solutions
memory object (known as the DataSet), you can disconnect from the database and manipulate the data
until you're readybyto Mahesh Chand and David Talbot ISBN:1590590732
write it back to the database. The data provider classes serve as a bridge for moving
Apress 2003 (928 pages)
data back and forth between memory and the database. In the next section we discuss how to initially
This text
connect to a database provides
using theseextensive
providerscoverage
and howoftoADO.NET technology
implement the data provider classes to allow data
including ADO.NET internals, namespaces, classes, and
to use this efficient bridge.
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
In the sections that follow we describe how to connect to various databases with the data providers. Then
ADO.NET.
we show you how to use the data provider classes to execute queries and stored procedures. Also, you'll
learn how these powerful classes work together and how the data providers differ in certain capabilities.
Table ofalso
You'll Contents
learn how to do transaction locking, batch transactions, and transaction rollbacks.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
The Connection: Connecting
Applied ADO.NET: Building to a Data Solutions
Data-Driven Source
by Mahesh Chand and David Talbot ISBN:1590590732
As you saw in Figure 4-1,
Apress the first
2003 object that interacts with a data source is the Connection object. You
(928 pages)
can bypass a Connection object by using acoverage
This text provides extensive DataAdapter directly,
of ADO.NET but in that case a DataAdapter
technology
uses the Connection internally. The Connection class has a
including ADO.NET internals, namespaces, classes, and different name depending upon the data
interfaces, and
provider. The Connection takes
class for aOleDb,
comprehensive
Sql, and look
Odbcatare
XMLOleDbConnection, SqlConnection,
respectively. namespaces and classes, and how to integrate both with
ADO.NET.

Creating a Connection
Table of Contents
TheSqlConnection
Applied class
ADO.NETBuilding provides Solutions
Data-Driven two forms of constructors. The first doesn't take any parameters,
and the second takes a parameter of a connection string that points to the database to which you want to
Introduction
attach. 1The
Chapter connection
- ADO.NET string contains the server name, database name, user ID, password, and more.
Basics
Chapter 2 - Data Components in Visual Studio .NET
Listing 4-1 creates SqlConnection objects using both constructors.
Chapter 3 - ADO.NET in Disconnected Environments
Chapter
Listing44-1:
- ADO.NET
CreatinginSqlConnection
Connected Environments
Objects
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Dim connString As String = "user id=sa;password=password;" & _
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
"initial catalog=northwind;data source=Northwind;Connect Timeout=30"
Chapter 8 - Constraints and Data Relations
Dim conn1 As SqlConnection = New SqlConnection(connString)
Chapter 9 - ADO.NET Exception Handling
Dim conn2 As SqlConnection = New SqlConnection()
Chapter 10 - Working with the ODBC
conn2.ConnectionString .NET Data Provider
= connString
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
After creating
Chapter a connection,
14 - Developing you Web
Database call its Open method,
Applications using and when you're done with the connection, you call
ASP.NET
theClose
Chapter 15 -method.
Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Understanding the Connection Properties and Methods
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
TheConnection
Chapter 19 - Mappedclass (SqlConnection
Objects: in this case) and
Performance Considerations hasData
a connection
Binding string that contains a server and
a database
Chapter 20 - name. The connection
COM Interoperability andstring will vary depending upon the provider used. The connection
ADO.NET
string will
Chapter 21 typically contain a group of property-value pairs to describe how to connect to a database. For
- Messaging
theOleDbConnection
Chapter class,
22 - SQL Server and you have
ADO.NET: properties
Notes such as Provider and DataSource. For the
on Performance
SqlConnection class, you have the server name, initial catalog, user ID, and password. Table 4-2
Appendix A - Relational Databases: Some Basic Concepts
describes the Connection class properties. (Based on the data provider, some of these properties may
Appendix B - Commonly Used SQL Statements
not be applicable.)
Appendix C - ADO.NET Frequently Asked Questions
Index
Table 4-2: Connection Object Properties
List of Figures
List of Tables
List of Listings
List of Sidebars
PROPERTY DESCRIPTION
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
ConnectionString The connection string.
Apress 2003 (928 pages)
ConnectionTimeOut Waiting
This text provides time while
extensive establishing
coverage a connection.
of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
DataBase Name
interfaces, and of athe
takes current database.
comprehensive look at XML
namespaces and classes, and how to integrate both with
DataSource ADO.NET. Filename of the data source.
Provider Name of the OLE DB provider. This property is not available for the
Table of Contents Sql and ODBC data providers.
Applied
StateADO.NETBuilding Data-Driven Solutions
Current state of the connection of type ConnectionState.
Introduction
1 - ADO.NET Basics Size of network packets. This is only available to Sql data providers.
PacketSize
Chapter
Chapter 2 - Data ComponentsSQL
ServerVersion in Visual Studio
Server .NET This is only available to Sql data providers.
version.
Chapter 3 - ADO.NET in Disconnected Environments
WorkStationId
Chapter 4 Database
- ADO.NET in Connected client ID. This is only available to Sql data providers.
Environments
Chapter 5 - Handling ADO.NET Events
A typical connection
Chapter 6
for SQL Server may also contain the security type, the workstation ID, and even the
- Integrating XML with ADO.NET
network packet size. The following code is a connection string for a SQL Server called MCB:
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
connectionString
Chapter =
9 - ADO.NET Exception Handling
"data source=MCB;initial catalog=Northwind;"
Chapter 10 - Working with the ODBC .NET Data Provider
+ "integrated security=SSPI;persist security info=False;"
Chapter 11 - Stored Procedures and Views
+ "workstation id=MCB;packet size=4096";
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing
TheConnection Database
can have Web states
different Applications using
such as ASP.NET
open, closed, connecting, and so on. The
Chapter 15 - Using ADO.NET
ConnectionType in Xml defines
enumeration Web Services
the members of the ConnectionState.Table 4-3 describes its
Chapter 16 - ASP.NET Server Controls and Data Binding
members.
Chapter 17 - Building Real-World Web Applications
Table 4-3:
Chapter 18 - The ConnectionType
Object-Relational Enumeration
Mapping in .NET Members
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
MEMBER DESCRIPTION
Chapter 20 - COM Interoperability and ADO.NET
Chapter
Broken21 - Messaging
Connection is broken after it was opened. This may be caused by network
failure.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Closed Connection is closed.
Appendix B - Commonly Used SQL Statements
Connecting
Appendix Opening
C - ADO.NET a new
Frequently connection.
Asked Questions
Index
Executing The connection is executing a command.
List of Figures
ListFetching
of Tables Retrieving data from a data source.
ListOpen
of Listings
Connection is open and ready to use.
List of Sidebars
Table 4-4 describes the Connection class methods. You'll see some of these methods throughout this
chapter. The purpose of this table is to give you an idea of available methods.

Table 4-4: The Connection Class Members


METHOD DESCRIPTION
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
BeginTransaction Begins a database transaction.
Apress 2003 (928 pages)
ChangeDatabase Changes
This text provides databases
extensive for an
coverage of open connection.
ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Close Closes
interfaces, and takesan open connection.
a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET. Creates and returns a Command objects depends on the data provider.
CreateCommand
For example, OleDbConnection returns OleDbCommand, and
SqlConnection returns SqlCommand.
Table of Contents
Opens aSolutions
OpenADO.NETBuilding Data-Driven
Applied new connection.
Introduction
ReleaseObjectPool Represents that the connection pooling can be cleared when the
Chapter 1 - ADO.NET Basics provider is released. This is only available for OleDb data providers.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4
- ADO.NET in Connected Environments
Opening
Chapter 5
and Closing a Connection
- Handling ADO.NET Events
Chapter
As you 6can- Integrating XML with
see from Listing 4-1,ADO.NET
the code creates two Connection objects: conn1 and conn2. It creates
Chapter 7 - Data Binding and Windows
conn1 with no connection string. Forms
If you Data-Bound
create Controls
a Connection object with no connection string, you must
set its ConnectionString
Chapter property
8 - Constraints and Data before you call the Open method of the Connection object. The
Relations
code creates
Chapter the conn2
9 - ADO.NET objectHandling
Exception with a connection string as an argument. As you can see from the
connection
Chapter 10 - string,
Working it consists of a provider
with the ODBC name
.NET Data and data source.
Provider
Chapter 11 - Stored Procedures and Views
Note All the providers construct their connections in the same way. The thing that makes the
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
connection construction different from other different providers is the ConnectionString. For
Chapter 13 - Developing a Custom Data Provider
example, the SqlClient namespace doesn't need to specify a provider string because Sql
Chapter 14 - Developing Database Web Applications using ASP.NET
Server is always the database when using this class.
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET
After creating Server Controls
a Connection and
object, Data
you callBinding
its Open method to open a connection. The Open method
Chapter 17 - Building Real-World Web Applications
doesn't take any arguments. The following code opens a connection:
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
conn.Open()
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
When you're
Chapter done
22 - SQL with and
Server the connection, you on
ADO.NET: Notes callPerformance
its Close method to release the connection. The Close
method Aalso
Appendix doesn't take
- Relational any arguments.
Databases: The
Some Basic following code closes a connection:
Concepts
Appendix B - Commonly Used SQL Statements
conn.Close()
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
Listing 4-2 opens a connection with the Access 2000 Northwind database that resides in the C:\ directory.
List of Tables
As you can see, you check the connection state to see if the connection is already opened (which is
List of Listings
impossible in this code) or closed.
List of Sidebars
Listing 4-2: Connecting to an OleDb Database and Reading the OleDbConnection Properties

' Create a Connection Object


Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\\Northwind.mdb"
Dim conn As OleDbConnection = New OleDbConnection(ConnectionString)
' Open the connection
If conn.State <> ConnectionState.Open Then
conn.Open()
MessageBox.Show("Connection state: " + conn.State.ToString())
End If
' Show the connection properties
Applied ADO.NET: Building
MessageBox.Show("Connection Data-Driven
String :" + Solutions
conn.ConnectionString & _
by Mahesh Chand and David Talbot
", DataSource :" + conn.DataSource.ToString() ISBN:1590590732
& _
Apress 2003 (928 pages)
", Provider :" + conn.Provider.ToString() & _
This","
text provides extensive coverage of ADO.NET technology
+ conn.ServerVersion.ToString() & _
including ADO.NET internals, namespaces, classes, and
"," + conn.ConnectionTimeout.ToString())
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
' Close the connection
ADO.NET.
If conn.State = ConnectionState.Open Then
conn.Close()
Table of MessageBox.Show("Connection
Contents state: " + conn.State.ToString())
Applied ADO.NETBuilding
End If Data-Driven Solutions
Introduction
' Dispose the connection
Chapter
If1 (Not
- ADO.NET
conn Basics
Is Nothing) Then
Chapter conn.Dispose()
2 - Data Components in Visual Studio .NET
End
Chapter 3 If- ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Caution
Chapter 7
To utilize connection resources properly, it's always recommended to close open
- Data Binding and Windows Forms Data-Bound Controls
connections and dispose of connections explicitly if you don't intend to use them again. If you
Chapter 8 - Constraints and Data Relations
don't dispose of connections, the garbage collector will take care of it; however, the garbage
Chapter 9 - ADO.NET Exception Handling
collector waits for a certain interval of time. To force the garbage collector to dispose of
Chapter 10 - Working with the ODBC .NET Data Provider
connections, you call the Dispose method of the SqlConnection. The call to the
Chapter 11 - Stored Procedures and Views
Dispose method destroys the connection resource immediately.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter
You can13also
- Developing a Custom
use the OleDb dataData Provider
provider to connect to SQL Server or other databases if you have an
Chapter
OLE DB 14data
- Developing Databasefor
provider installed Web Applications
that database.using ASP.NET
Listing 4-3 shows the connection for a SQL Server using
the OleDb
Chapter 15 -data provider.
Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Listing17
Chapter 4-3: Connecting
- Building to a SQL
Real-World Server Using the OleDb Data Provider
Web Applications
Chapter 18 - Object-Relational Mapping in .NET
' Create
Chapter a Connection
19 - Mapped Object Considerations and Data Binding
Objects: Performance
ChapterDim
20 -ConnectionString As
COM Interoperability and String = "Provider=SQLOLEDB.1;" & _
ADO.NET
"Integrated Security=SSPI;" & _
Chapter 21 - Messaging
"Persist
Chapter 22 - SQL Server andSecurity Info=false;"
ADO.NET: Notes & _
on Performance
"Initial
Appendix A - Relational Catalog=Northwind;"
Databases: Some Basic Concepts & _
"Data Source=MCB;"
Appendix B - Commonly Used SQL Statements
Dim conn As OleDbConnection = New OleDbConnection(ConnectionString)
Appendix C - ADO.NET Frequently Asked Questions
' Open the connection
Index
If conn.State <> ConnectionState.Open Then
List of Figures
conn.Open()
List of Tables
MessageBox.Show("Connection state: " + conn.State.ToString())
List of Listings
End If
List of Sidebars

Now you'll connect to a SQL Server database using the Sql data provider. For this example, you'll use the
SQL Server 2000 Northwind database. Listing 4-4 shows the connection with the SQL Server database. As
you can see, the code first constructs a connection string with the SQL Server name MCB and the
database name Northwind. It uses Windows security in this connection, but depending on your SQL Server
setup you could alternatively use a SQL Server login and password. After constructing a connection string,
it calls the Open method to open the connection. After that it reads the connection properties and displays
them in a message box. In the end, it calls the Close method to close the connection and the Dispose
method to get rid of the connection resources.
Listing 4-4: Connecting to a SQL Server and Reading the SqlConnection Properties
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
' Create a Connection Object
Apress 2003 (928 pages)
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
This text provides extensive coverage of ADO.NET technology
"Initial
including Catalog=Northwind;Data
ADO.NET Source=MCB;"
internals, namespaces, classes, and
Dim conn Asinterfaces,
SqlConnection
and takes a =comprehensive
New SqlConnection(ConnectionString)
look at XML
' Open the namespaces
connection and classes, and how to integrate both with
ADO.NET.
If conn.State <> ConnectionState.Open Then
conn.Open()
Table ofMessageBox.Show("Connection
Contents state: " + conn.State.ToString())
End If
Applied ADO.NETBuilding Data-Driven Solutions
' Show the connection properties
Introduction
MessageBox.Show("Connection String :" + conn.ConnectionString & _
Chapter 1 - ADO.NET Basics
", Workstation Id:" + conn.WorkstationId.ToString() & _
Chapter 2 - Data Components in Visual Studio .NET
", Packet Size :" + conn.PacketSize.ToString() & _
Chapter 3 - ADO.NET in Disconnected Environments
", Server Version " + conn.ServerVersion.ToString() & _
Chapter 4 - ADO.NET ",in DataSource
Connected Environments
:" + conn.DataSource.ToString() & _
Chapter 5 - Handling",ADO.NET
Server Events
Version:" + conn.ServerVersion.ToString() & _
Chapter 6 - Integrating
", XML with ADO.NET
Connection Time Out:" + conn.ConnectionTimeout.ToString())
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints
' Close and Data Relations
the connection
Chapter 9 - ADO.NET Exception
If conn.State Handling
= ConnectionState.Open Then
Chapterconn.Close()
10 - Working with the ODBC .NET Data Provider
ChapterMessageBox.Show("Connection
11 - Stored Procedures and Views state: " + conn.State.ToString())
End12If- Oracle, SQLXML, and Other .NET Data Providers
Chapter
' Dispose
Chapter the connection
13 - Developing a Custom Data Provider
If (Not
Chapter conn IsDatabase
14 - Developing Nothing) Then
Web Applications using ASP.NET
conn.Dispose()
Chapter 15 - Using ADO.NET in Xml Web Services
End If
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter
You can 19even
- Mapped
pass aObjects:
user IDPerformance Considerations
and password and Data
in a connection Binding
string. For example, the following connection
Chapter 20 - COM Interoperability and
string uses a user ID and password: ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Dim ConnectionString As String = "user id=sa;password=pass;" & _
Appendix A - Relational"Initial
Databases: Some Basic Concepts
Catalog=Northwind;Data Source=MCB;"
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List ofNote As you can see, we used MCB as the data source name. You can use localhost as your local
Figures
server.
List of Tables
List of Listings
Understanding Connection Pooling
List of Sidebars

Connection pooling is a mechanism to utilize and share connection resources already available in a
connection pool. In general, if an application needs a connection, it creates a new connection. There's a
whole process of establishing a new connection. When the application is done using a connection, it
closes the connection. Connection pooling plays a vital role in a three-tier development. For example, in a
three-tier development, say you're using a Web server. The opening and closing of a connection
consumes time and server resources in this scenario. Now say the application executes only a SELECT
statement, which returns only a few records from the database. It may be possible that establishing a
connection with the server may take more time than executing the query. This situation gets worse when
there are hundreds of users accessing the same server. Another scenario is that a user logs into a server
and then stays connected, never logging out. So, having a connection open until a user logs out is not a
feasible solution.
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh
So how does connection Chandhelp
pooling and in
David Talbot
this scenario? In the .NET Framework, when an application
ISBN:1590590732
Apress
closes a connection, theconnection goes to a pool of available connections and stays active for a certain
2003 (928 pages)
interval of time even
Thisafter you closeextensive
text provides the connection.
coverageWhen a user technology
of ADO.NET connects, the provider compares the
connection stringincluding ADO.NET internals,
to the connection strings in namespaces, classes,
the pool. If the string and
matches, then a connection is returned
interfaces,
from the pool instead and takes
of creating and aopening
comprehensive look at XML
a new connection.
namespaces and classes, and how to integrate both with
ADO.NET.
The connection pooling mechanism works differently for different data providers. The Connection class
defines members that allow you to pool connection resources manually.
Table of Contents
OLE DB provides automatic connection pooling (also known as sessionpooling), which is handled by OLE
Applied ADO.NETBuilding Data-Driven Solutions
DB core components through its providers.
Introduction
Chapter
In ADO,1 you
- ADO.NET Basicsof the OLE DB Services parameter, which includes connection pooling as
set the value
well. This
Chapter 2 is an ADO
- Data connection
Components string Studio
in Visual passed to the Open method of an OLEDBConnection:
.NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET
Dim conn in Connected Environments
As OLEDBConnection = Nothing
Chapter 5 - Handling ADO.NET Events
Conn.Open("DSN=LocalServer;UID=sa;PWD=;OLE DB Services=-1")
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Table 4-5
Chapter 8 -describes theand
Constraints values
Datafor the OLE DB Services parameter.
Relations
Chapter 9 - ADO.NET Exception Handling
Table 4-5:
Chapter 10 - The OLEwith
Working DB the
Services Settings
ODBC .NET Data Provider
Chapter 11 - Stored
SERVICES Procedures and Views
ENABLED VALUE
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
All services
Chapter (default) a Custom Data Provider
13 - Developing "OLE DB Services = 1;"
Chapter 14 - Developing
All services Database Web Applications using ASP.NET
except pooling "OLE DB Services = 2;"
Chapter 15 - Using ADO.NET in Xml Web Services
All services
Chapter except Server
16 - ASP.NET poolingControls
and auto enlistment
and Data Binding
"OLE DB Services = 4;"
Chapter 17 - Building
All services exceptReal-World Web Applications
client cursor "OLE DB Services = 5;"
Chapter 18 - Object-Relational Mapping in .NET
All services except client cursor and pooling "OLE DB Services = 6;"
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
No services "OLE DB Services = 0;"
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
For example,
Appendix the following
A - Relational string Some
Databases: disables theConcepts
Basic connection pooling:
Appendix B - Commonly Used SQL Statements
Dim connString
Appendix C - ADO.NET As String
Frequently = "Provider
Asked Questions = SQLOLEDB OLE DB Services = -2;" & _
"Data Source = MCB; Integrated Security = SSPI "
Index
List of Figures
List of Tables
ADO .NET manages connection pooling automatically when you call the Close or Dispose method of a
List of Listings
Connection object. Once a pool is created, you can add connections to this pool until the pool reaches
List of Sidebars
its maximum size. You can define the maximum size of a connection pool using the connection string. If a
pool reaches its maximum size, the next added connection will join a queue and wait until the pool
releases an existing connection.

You create a pool when you call the Open method of a connection based on the connection string. If
you're using the same database for two Connection objects, but the connection string is different
(including spaces and single characters), each connection will be added to a different pool. For example,
Listing 4-5 creates two connections: conn1 and conn2. The ConnectionString1 and
ConnectionString2 connection strings are different for both connections. Because these connections
have different connection strings, they'll be added to two different pools.

Listing 4-5: Creating Two Connections with Different Strings


' Create a Connection Object
Applied ADO.NET: Building Data-Driven Solutions
Dim ConnectionString1 As
by Mahesh Chand and String
David = "IntegratedISBN:1590590732
Talbot Security=SSPI;" & _
"Initial Catalog=Northwind;"
Apress 2003 (928 pages) & _
"Data Source=MCB;"
This text provides extensive coverage of ADO.NET technology
Dim conn1 As including
SqlConnection = New namespaces,
ADO.NET internals, SqlConnection(ConnectionString1)
classes, and
interfaces, andObject
' Create a Connection takes a comprehensive look at XML
namespaces and
Dim ConnectionString2 As classes,
String and=how to integrate both
"Integrated with
Security=SSPI;" & _
ADO.NET.
"Initial Catalog=Pubs;" & _
"Data Source=MCB;"
Dim
Table ofconn2 As SqlConnection = New SqlConnection(ConnectionString2)
Contents
' Open connections
Applied ADO.NETBuilding Data-Driven Solutions
conn1.Open()
Introduction
conn2.Open()
Chapter 1 - ADO.NET Basics
MessageBox.Show("Connection1 " + conn1.State.ToString())
Chapter 2 - Data Components in Visual Studio .NET
MessageBox.Show("Connection2 " + conn2.State.ToString())
Chapter 3 - ADO.NET in Disconnected Environments
' some code
Chapter 4 - ADO.NET in Connected Environments
conn1.Close()
Chapter 5 - Handling ADO.NET Events
conn2.Close()
Chapter 6 - Integrating XML with ADO.NET" + conn1.State.ToString())
MessageBox.Show("Connection1
Chapter 7 - Data Binding and Windows Forms
MessageBox.Show("Connection2 " + Data-Bound Controls
conn2.State.ToString())
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Caution
Chapter You must
11 - Stored alwaysand
Procedures callViews
the Close or Dispose method of Connection to close the
connection.
Chapter 12 - Oracle, Connections
SQLXML, that are
and Other .NET Datanot explicitly closed are not added or returned to the pool.
Providers
Chapter 13 - Developing a Custom Data Provider
Even though the .NET Framework automatically manages connection pooling, you can customize the
Chapter 14 - Developing Database Web Applications using ASP.NET
connection pooling properties programmatically by passing them in a connection string or by setting them
Chapter 15 - Using ADO.NET in Xml Web Services
using the Connection properties. A typical connection string with connection pooling parameters looks
Chapter 16 - ASP.NET Server Controls and Data Binding
like the following:
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Dim ConnectionString As String = "user id=sa;password=pass;" & _
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
"Initial Catalog=Northwind;Data Source=MCB;" & _
Chapter 20 - COM Interoperability and ADO.NET
"Pooling='true';Connection Reset='false';" & _
Chapter"Connection
21 - Messaging Lifetime=5;Min Pool Size=3;Max Pool Size=20;"
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Some ofBthe
Appendix pooling settings
- Commonly Used SQLareStatements
in the form of key-value pairs (see Table 4-6).
Appendix C - ADO.NET Frequently Asked Questions
Table 4-6: Connection Pooling Settings
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
KEY DESCRIPTION
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Connection Connection creation time is compared with the current time, and if time span
Apress 2003 (928 pages)
Lifetime exceeds the Connection Lifetime value, the object pooler destroys the
This text provides extensive coverage of ADO.NET technology
connection. The default value is 0, which will give a connection the maximum
including ADO.NET internals, namespaces, classes, and
timeout.
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
Determines whether a connection is reset after it was removed from the
Connection ADO.NET.
Reset pool. The default value is True.
Max Pool
Table of Contents Maximum number of connection allowed in the pool. The default value is
Size 100.
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Min Pool Minimum number of connections allowed in the pool. The default value is 0.
Chapter
Size1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Pooling WhenTrue (the default), the connection is drawn from the pool or created if
Chapter 3 - ADO.NET in Disconnected Environments
necessary.
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
TheOleDbConnection class provides a ReleaseObjectPool method you can use to free resources
Chapter 6 - Integrating XML with ADO.NET
reserved for a connection. You call this method when this connection won't be used again. To call
Chapter 7 - Data Binding and
ReleaseObjectPool, first Windows Forms
you call the Data-Bound
Close method.Controls
Listing 4-6 shows how to use
Chapter 8 - Constraints and Data Relations
ReleaseObjectPool.
Chapter 9 - ADO.NET Exception Handling
Listing10
Chapter 4-6: Callingwith
- Working ReleaseObjectPool
the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle,and
' Connection SQLXML,
SQL and Other .NET Data Providers
strings
Chapter 13 - Developing a Custom
Dim ConnectionString As Data Provider
String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
Chapter
"Data14Source=c:\\Northwind.mdb"
- Developing Database Web Applications using ASP.NET
Chapter
Dim SQL15 -As
Using ADO.NET
String = in"SELECT
Xml Web Services
OrderID, Customer, CustomerID FROM Orders"
' Create
Chapter connection
16 - ASP.NET objectand Data Binding
Server Controls
Dim conn
Chapter As OleDbConnection
17 - Building = New OleDbConnection(ConnectionString)
Real-World Web Applications
conn.Open()
Chapter 18 - Object-Relational Mapping in .NET
' do something
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
conn.Close()
Chapter 20 - COM Interoperability and ADO.NET
OleDbConnection.ReleaseObjectPool()
Chapter 21 - Messaging
' Dispose the connection
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
If (Not conn Is Nothing) Then
Appendix A - Relational Databases: Some Basic Concepts
conn.Dispose()
Appendix B - Commonly Used SQL Statements
End If
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
Using the CreateCommand and ChangeDatabase Methods
List of Listings
List of Sidebars
TheSqlConnection class provides two more useful methods: the CreateCommand and
ChangeDatabase methods. The CreateCommand method creates a SqlCommand object, which will be
associated with the Connection object. In general, when you create a SqlCommand object, you need to
pass a SqlConnection argument to connect to a connection, but if you use the CreateCommand
method of SqlConnection, you don't need to do so.

The following code creates a SqlCommand using the CreateCommand method of an already existing
SqlConnection object:

Dim cmd As SqlCommand = conn.CreateCommand()


cmd.CommandText = "SELECT * FROM Customers"
' Do something with the command
Applied ADO.NET: Building Data-Driven Solutions
We discuss SqlCommand
by MaheshinChand
more and
detail in the
David following section.
Talbot ISBN:1590590732
Apress 2003 (928 pages)
TheChangeDatabase method of the SqlConnection object is a relief to developers who frequently
This text provides extensive coverage of ADO.NET technology
have to change the database to which they're connected. This feature was missing in previous
including ADO.NET internals, namespaces, classes, and
technologies, andinterfaces,
the only way
and out of athis
takes problem waslook
comprehensive to disconnect
at XML the connection and then reconnect
to the new database. The ChangeDatabase
namespaces and classes, and howmethod does theboth
to integrate same
withthing, but it does so internally so that
ADO.NET.
developers can continue using the same SqlConnection object. The following code opens a connection
to the Northwind database and later changes the database source using the ChangeDatabase:
Table of Contents
' Create
Applied a Connection
ADO.NETBuilding Object
Data-Driven Solutions
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
Introduction
"Initial Catalog=Northwind;Data Source=MCB;"
Chapter 1 - ADO.NET Basics
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Chapter 2 - Data Components in Visual Studio .NET
' Open the connection
Chapter 3 - ADO.NET in Disconnected Environments
conn.Open()
Chapter 4 - ADO.NET in Connected Environments
conn.ChangeDatabase("pubs")
Chapter'5 Do- Handling ADO.NET
something Events
with the command
ChapterMessageBox.Show(conn.Database.ToString())
6 - Integrating XML with ADO.NET
Chapter'7 Close
- Data the
Binding and Windows and
connection Formscall
Data-Bound Controls
Dispose
Chapter 8 conn.Close()
- Constraints and Data Relations
Chapter 9 conn.Dispose()
- ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
The Command:
AppliedExecuting SQL
ADO.NET: Building Statements
Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
TheCommand object
Apressallows you
2003 topages)
(928 directly execute SQL statements such as INSERT,SELECT,UPDATE,
andDELETE against a data source for reading,
This text provides extensive writing,ofand
coverage updating
ADO.NET a database. You can also use the
technology
Command object including
to execute stored procedures. The SqlCommand,
ADO.NET internals, namespaces, classes, and OleDbCommand, and OdbcCommand
classes representinterfaces, and takes
the Command a comprehensive
objects look and
in the Sql, OleDb, at XML
ODBC data providers, respectively.
namespaces and classes, and how to integrate both with
ADO.NET.
Table 4-7 describes some of the more important properties of the Command class for the Sql data
provider. The CommandText listed in this table can contain either a SQL statement or a stored procedure
name. The CommandType determines which one of these forms the CommandText takes.
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Table 4-7: The Command Properties
Introduction
Chapter 1 - ADO.NET Basics DESCRIPTION
PROPERTY
Chapter 2 - Data Components in Visual Studio .NET
CommandText
Chapter 3
Could be a SQL statement, a stored procedure, or a database table
- ADO.NET in Disconnected Environments
name depending on the CommandType
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NETWait
CommandTimeout Events
time before terminating the execute command
Chapter 6 - Integrating XML with ADO.NET
CommandType An enumeration of values Text,StoredProcedure, or
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
TableDirect
Chapter 8 - Constraints and Data Relations
Connection
Chapter 9 - ADO.NET ExceptionAConnection
Handling representing the ActiveConnection
Chapter 10 - Working with the ODBC .NET Data Provider
DesignTimeVisible Indicates whether the command should be visible at design-time
Chapter 11 - Stored Procedures and Views
Parameters
Chapter A collection
12 - Oracle, SQLXML, and of parameters
Other .NET (SqlParameterCollection)
Data Providers
Chapter 13 - Developing a Custom Data Provider
Transaction A transaction
Chapter 14 - Developing Database Web Applications using ASP.NET
15 - Using ADO.NET inRepresents
UpdateRowSource
Chapter how command results are applied to a DataRow when
Xml Web Services
used byand
Chapter 16 - ASP.NET Server Controls theData
Update method
Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Listing 4-7 creates a SqlCommand object, sets its properties, and then reads back its properties to display
Chapter 19 - Mapped
them. You'll see theObjects: Performance
rest of the Considerations
propertiessuch and Data Bindingthe related sections of this chapter.
as Transactionin
Chapter 20 - COM Interoperability and ADO.NET
Listing21
Chapter 4-7: Setting and Getting SqlCommand Properties
- Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational
' Connection andDatabases: Some Basic Concepts
SQL strings
Appendix
DimB -ConnectionString
Commonly Used SQL Statements
As String = "Integrated Security=SSPI;" & _
Appendix C - ADO.NET FrequentlyCatalog=Northwind;Data
"Initial Asked Questions Source=MCB;"
Index Dim SQL As String = "SELECT * FROM Orders"
List of Figures
' Create connection object
List of Tables
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
' Create command object
List of Listings
Dim cmd As SqlCommand = New SqlCommand()
List of Sidebars
cmd.Connection = conn
cmd.CommandText = SQL
cmd.CommandTimeout = 30
cmd.CommandType = CommandType.Text
' Open connection
conn.Open()
' Read Command properties
Dim str As String
str = "Connection String: " + cmd.Connection.ConnectionString.ToString()
str = str + " , SQL Statement :" + cmd.CommandText
str = str + " , Timeout :" + cmd.CommandTimeout.ToString()
str = str + " , CommandTyoe:" + cmd.CommandType.ToString()
Applied ADO.NET: Building Data-Driven Solutions
MessageBox.Show(str)
by Mahesh Chand and David Talbot
' close connection ISBN:1590590732
Apress
conn.Close() 2003 (928 pages)
'Dispose This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
conn.Dispose()
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Creating a Command Object


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
There are a number of ways to construct a Command object. You can create a Command and set its
Introduction
connection and SQL string, or you can create a Command by passing the connection string and SQL string
Chapter 1 - ADO.NET Basics
as parameters of the Command constructor.
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET
The following in Disconnected
examples Environments
show you three different ways to create a Command object. This code constructs a
Chapter 4 - and
connection ADO.NET in string:
a SQL Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating
' Connection and XML
SQLwith ADO.NET
strings
Chapter 7 - Data Binding and Windows
Dim ConnectionString As StringForms= Data-Bound Controls
"Integrated Security=SSPI;" & _
Chapter 8 - Constraints
"Initialand Data Relations
Catalog=Northwind;Data Source=MCB;"
Chapter 9 - ADO.NET
' Create Exception
connection Handling
object
Dim conn
Chapter As SqlConnection
10 - Working = New
with the ODBC .NET DataSqlConnection(ConnectionString)
Provider
Dim SQL
Chapter 11 -As String
Stored = "SELECT
Procedures and Views* FROM Orders"
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Now create a SqlCommand object using a constructor with no arguments. Later you set SqlCommand's
Chapter 14 - Developing Database Web Applications using ASP.NET
Connection and CommandText properties to connect to a Connection and set the SQL statement,
Chapter 15 - Using ADO.NET in Xml Web Services
which this command will be executing:
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
' Create command object
Chapter 18 - Object-Relational Mapping in .NET
Dim cmd1 As SqlCommand = New SqlCommand()
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
cmd1.Connection = conn
Chapter 20 - COM Interoperability and ADO.NET
cmd1.CommandText = SQL
Chapter 21 - Messaging
cmd1.CommandTimeout = 30
Chapter 22 - SQL Server and
cmd1.CommandType ADO.NET: Notes on Performance
= CommandType.Text
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
In the second
Appendix form, you
C - ADO.NET create aAsked
Frequently Questions object by directly passing a SQL query and the
SqlCommand
SqlConnection object as the first and second arguments:
Index
List of Figures
//ofCreate
List Tables command object
Dim
List cmd2 As SqlCommand = New SqlCommand(SQL, conn)
of Listings
List of Sidebars

The third way of creating a Command object is to create a command by just passing a SQL query as the
argument and setting its Connection property later:

// Create command object


Dim cmd3 As SqlCommand = New SqlCommand(SQL)
cmd3.Connection = conn

Listing 4-8 shows you how to connect to the Northwind SQL Server database, read all the records from the
Orders table, and output the first and second field's data to the console. The new things you'll notice in this
code are ExecuteReader and SqlDataReader. A SqlDataReader is a DataReader class, and
ExecuteReader fills data from a data source to the DataReader based on the SQL query. (We discuss
Applied
DataReader classes ADO.NET:
in the Building Data-Driven Solutions
next section.)
by Mahesh Chand and David Talbot ISBN:1590590732
Apress
Listing 4-8: Using 2003 (928 to
SqlCommand Read Data from a Database
pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
' Connection interfaces,
and SQLand strings
takes a comprehensive look at XML
Dim ConnectionString
namespaces Asand String = how
classes, and "Integrated Security=SSPI;"
to integrate both with & _
ADO.NET.
"Initial Catalog=Northwind;Data Source=MCB;"
Dim SQL As String = "SELECT * FROM Orders"
' Create connection object
Table of Contents
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Applied ADO.NETBuilding Data-Driven Solutions
' Create command object
Introduction
Dim cmd As SqlCommand = New SqlCommand(SQL)
Chapter 1 - ADO.NET =
cmd.Connection Basics
conn
Chapter
' Open 2 connection
- Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
conn.Open()
Chapter
' Call 4 command's
- ADO.NET in ExecuteReader
Connected Environments
Chapter 5 - Handling
Dim reader ADO.NET Events = cmd.ExecuteReader()
As SqlDataReader
Chapter
While6 reader.Read()
- Integrating XML with ADO.NET
If Not
Chapter reader.IsDBNull(0)
7 - Data ThenData-Bound Controls
Binding and Windows Forms
Chapter 8 - Constraints and Data Relations reader.GetInt32(0).ToString())
Console.Write("OrderID:" +
Console.Write("
Chapter 9 - ADO.NET Exception,") Handling
Console.WriteLine("Customer:" + reader.GetString(1).ToString())
Chapter 10 - Working with the ODBC .NET Data Provider
End If
Chapter 11 - Stored Procedures and Views
End While
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
' close reader and connection
Chapter 13 - Developing a Custom Data Provider
reader.Close()
Chapter 14 - Developing Database Web Applications using ASP.NET
conn.Close()
Chapter 15 - Using ADO.NET in Xml Web Services
'Dispose
Chapter 16 - ASP.NET Server Controls and Data Binding
conn.Dispose()
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - of
The output COM Interoperability
Listing 4-8 will list and ADO.NET
records from the Customers table to the console.
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Creating and Using OleDbCommand
Appendix A - Relational Databases: Some Basic Concepts
Like the BSqlCommand
Appendix object,
- Commonly Used SQLyou create OleDb and ODBC Command objects by using the
Statements
OleDbCommand
Appendix and Frequently
C - ADO.NET OdbcCommandAskedclasses. You can pass the same arguments as discussed previously.
Questions
The only difference is the connection string. For example, Listing 4-9 uses OleDbCommand and
Index
OleDbConnection
List of Figures to connect to a SQL Server database. As you can see, the only changes are the
class
List prefixes and the connection string. Similarly, you can use the OdbcCommand object.
of Tables
List of Listings
Listing 4-9: UsingOleCommand to Access an Access Database
List of Sidebars

' Connection and SQL strings


Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\\Northwind.mdb"
Dim SQL As String = "SELECT * FROM Orders"
' Create connection object
Dim conn As OleDbConnection = New OleDbConnection(ConnectionString)
' Create command object
Dim cmd As OleDbCommand = New OleDbCommand(SQL)
cmd.Connection = conn
' Open connection
conn.Open()
Applied ADO.NET:
' Call command's Building Data-Driven Solutions
ExecuteReader
by Mahesh Chand
Dim reader As OleDbDataReader and David Talbot ISBN:1590590732
= cmd.ExecuteReader()
Apress
While reader.Read() 2003 (928 pages)
This text provides extensive+coverage
Console.Write("OrderID:" of ADO.NET technology
reader.GetInt32(0).ToString())
including ADO.NET
Console.Write(" ,") internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Console.WriteLine("Customer:"
namespaces and classes, and how + to reader.GetString(1).ToString())
integrate both with
End WhileADO.NET.
' close reader and connection
reader.Close()
Table of Contents
conn.Close()
Applied ADO.NETBuilding
conn.Dispose() Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3
- ADO.NET in Disconnected Environments
Using the
Chapter 4
CommandType Enumeration
- ADO.NET in Connected Environments
Chapter 5 - Handlingenumeration
TheCommandType ADO.NET Events
decides what type of object a command will be executed as. The
Chapter 6 - Integrating
CommandType enumerationXML with
canADO.NET
have any of the three values defined in Table 4-8.
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - The
Table 4-8: Constraints and DataEnumeration
CommandType Relations Members
Chapter 9 - ADO.NET Exception Handling
MEMBERS DESCRIPTION
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures
StoredProcedure The and
nameViews
of the stored procedure.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
TableDirect TheCommandText property should be set to the table name, and all
Chapter 13 - Developing a Custom Data Provider
rows and columns in the table will be returned.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter
Text15 - Using ADO.NETAinSQL
Xmltext
Webcommand.
Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
As you can see from Table 4-8, you can call a stored procedure, use TableDirect, or execute a SQL
Chapter 18 - Object-Relational Mapping in .NET
command. We present these options individually in the following sections.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Calling a Stored Procedure
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Calling stored procedures using the Command object is similar to executing a SQL query. This section
Appendix A - Relational Databases: Some Basic Concepts
gives you a quick overview of how to execute stored procedures. (We cover stored procedures in more
Appendix B - Commonly Used SQL Statements
detail in Chapter 11.)
Appendix C - ADO.NET Frequently Asked Questions
Index
You need to set the CommandType property of a Command object before calling a stored procedure. By
List of Figures
default, the CommandType property is Text. If you want to call a stored procedure, you need to set the
List of Tables
CommandType to StoredProcedure and the CommandText to the stored procedure name. After that
youofcan
List call the ExecuteReader method or other methods. You can also pass parameters to the
Listings
procedure
List by setting parameter values in the Command and then calling ExecuteReader on the
of Sidebars
Command object. Alternatively, you can pass a procedure name as a string when creating a Command
object.Listing 4-10 shows the settings of the CommandType and CommandText properties of
SqlCommand. As you can see, it calls an existing SQL Server Northwind database stored procedure,
Sales By Year.

Listing 4-10: Calling a Stored Procedure Using SqlCommand

' Create a SqlCommand with stored procedure as string


Dim cmd As SqlCommand = New SqlCommand("Sales By Year", conn)
' set Command's CommandType as StoredProcedure
cmd.CommandType = CommandType.StoredProcedure
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Note Executing stored procedures can be helpful in improving the performance of an application in
Apress 2003 (928 pages)
multiuser and Web applications because a stored procedure executes on the server itself.
This text provides extensive coverage of ADO.NET technology
includinginADO.NET
The Northwind database internals,
SQL Server namespaces,
contains classes,
a few stored and
procedures. One is called Sales By Year
interfaces, and takes a comprehensive look at XML
(see Listing 4-11).namespaces and classes, and how to integrate both with
ADO.NET.
Listing 4-11: The Sales By Year Stored Procedure in the Northwind Database

Table of Contents
SELECT Orders.ShippedDate, Orders.OrderID, "Order Subtotals".Subtotal,
Applied ADO.NETBuilding Data-Driven Solutions
DATENAME(yy,ShippedDate) AS Year
Introduction
FROM Orders INNER JOIN "Order Subtotals" ON Orders.OrderID =
Chapter 1 -Subtotals".OrderID
"Order ADO.NET Basics WHERE Orders.ShippedDate
Chapter 2 - Data
BETWEEN Components in Visual
@Beginning_Date ANDStudio .NET
@Ending_Date
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
This stored
Chapter procedure takes
6 - Integrating twoADO.NET
XML with parameters, Beginning_Date and Ending_Date. The procedure
selects7all -ofData
Chapter the Binding
orders between theseForms
and Windows two dates. It alsoControls
Data-Bound performs a join with the Order Subtotals
from the
Chapter 8 Order Subtotaland
- Constraints view, which
Data calculates the subtotals of each. If you want to execute this stored
Relations
procedure-inADO.NET
Chapter 9
ADO.NET, you just create a Command object of type StoredProcedure and then call the
Exception Handling
Command object's ExecuteReader method. You then cycle through the results in the reader that you're
Chapter 10 - Working with the ODBC .NET Data Provider
looking for from your stored procedure. Listing 4-12 executes a stored procedure that selects all the orders
Chapter 11 - Stored Procedures and Views
in July and displays their order IDs.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter
Listing13 - Developing
4-12: Executinga Custom Data Provider
and Reading the Results of a Stored Procedure
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
Chapter 16 - ASP.NET Server Controls and Data Binding
"Initial Catalog=Northwind;Data Source=MCB;"
Chapter 17 - Building Real-World Web Applications
Dim SQL As String = "SELECT * FROM Orders"
Chapter'18 - Object-Relational
Create connection Mapping in .NET
object
ChapterDim19 -conn
Mapped AsObjects: Performance Considerations
SqlConnection and Data Binding
= New SqlConnection(ConnectionString)
Chapter'20 - COM Interoperability
Create a SqlCommand and with
ADO.NETstored procedure as string
ChapterDim21 -cmd
Messaging
As SqlCommand = New SqlCommand("Sales By Year", conn)
Chaptercmd.Connection
22 - SQL Server and=ADO.NET:
conn Notes on Performance
Appendix' Aset- Relational
Command's Databases: Some Basic as
CommandType Concepts
StoredProcedure
cmd.CommandType = CommandType.StoredProcedure
Appendix B - Commonly Used SQL Statements
Appendix' C Create
- ADO.NETa SqlParameter and add a parameter
Frequently Asked Questions
Index Dim parm1 As SqlParameter = cmd.Parameters.Add("@Beginning_Date", _
SqlDbType.DateTime,
List of Figures 20)
parm1.Value = "7/1/1996"
List of Tables
Dim parm2 As SqlParameter = cmd.Parameters.Add("@Ending_Date", _
List of Listings
SqlDbType.DateTime, 20)
List of Sidebars
parm2.Value = "7/31/1996"
' Open connection
conn.Open()
' Call ExecuteReader to execute the stored procedure
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim orderlist As String = ""
' Read data from the reader
While reader.Read()
Dim result As String = reader("OrderID").ToString()
orderlist += result + " "
End While
' close the connection and reader
reader.Close()
Applied ADO.NET: Building Data-Driven Solutions
conn.Close()
by Mahesh Chand and David Talbot
conn.Dispose() ISBN:1590590732
Apress 2003 (928
' Print data on the console pages)
This text provides extensive
Console.WriteLine("Orders incoverage
July")of ADO.NET technology
Console.WriteLine("===============") classes, and
including ADO.NET internals, namespaces,
interfaces, and takes a comprehensive look at XML
Console.WriteLine(orderlist)
namespaces and classes, and how to integrate both with
ADO.NET.

Table
If youofwanted
Contents
to look at the subtotals along with the orders, you'd just add a DataReader index for
Applied ADO.NETBuilding
dereferencing the subtotalData-Driven Solutions
and concatenate with the order ID. Listing 4-13 shows the new DataReader
Introduction
loop.
Chapter 1 - ADO.NET Basics
Listing24-13:
Chapter Adding
- Data the Subtotal
Components Listing
in Visual Studioto the Output of the Stored Procedure Results
.NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter
While4 reader.Read()
- ADO.NET in Connected Environments
Dim nextID
Chapter As String
5 - Handling = reader("OrderID").ToString()
ADO.NET Events
Dim nextSubtotal
Chapter Aswith
6 - Integrating XML String = reader("Subtotal").ToString()
ADO.NET
orderlist
Chapter +=
7 - Data nextID
Binding + ", " Forms
and Windows + nextSubtotal + ", "
Data-Bound Controls
End While
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
The result of replacing this line of code in Listing 4-13 gives output that returns order IDs and subtotals in
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
the month of July in the Northwind database.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Using TableDirect
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
You can also use the TableDirect CommandType to read information directly from a table. You need
Chapter
to make17two
- Building
changes Real-World Web Applications
in the example to execute a table by setting TableDirect. First, you need to set
Chapter 18 - CommandText
Command's Object-Relationalproperty
Mappingtointhe
.NET
table name; second, you need to set the CommandType
Chapter 19to- CommandType.TableDirect.
property Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
The following
Chapter code reads the Customers table and sets the CommandType property to
21 - Messaging
CommandType.TableDirect:
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
cmd.CommandText
Appendix B - Commonly = "Customers"
Used SQL Statements
cmd.CommandType
Appendix = CommandType.TableDirect
C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
Listing 4-14 reads information from the Customers table by setting the TableDirect method and
List of Listings
displaying it on the console.
List of Sidebars
Listing 4-14: UsingTableDirect to Read a Table

' Create a Connection Object


Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\\Northwind.mdb"
Dim conn As OleDbConnection = New OleDbConnection(ConnectionString)
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "Customers"
cmd.CommandType = CommandType.TableDirect
conn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader()
Applied ADO.NET: Building
Console.WriteLine("Customer Data-Driven
Id, ContactSolutions
Name, Company Name")
by Mahesh Chand and David Talbot
Console.WriteLine("=======================================") ISBN:1590590732
Apress
While reader.Read() 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
Console.Write(reader("CustomerID").ToString())
including ADO.NET
Console.Write(", " +internals, namespaces, classes, and
reader("ContactName").ToString())
interfaces, and takes a comprehensive look at XML
Console.WriteLine(",
namespaces and classes, " +andreader("CompanyName").ToString())
how to integrate both with
End WhileADO.NET.
' release objects
reader.Close()
Table of Contents
conn.Close()
Applied ADO.NETBuilding
conn.Dispose() Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3
- ADO.NET in Disconnected Environments
Executing
Chapter 4
a Command
- ADO.NET in Connected Environments
Chapter 5 saw
You just - Handling ADO.NET Eventsmethod, which reads data from a data source and fills the
the ExecuteReader
Chapter 6 - Integrating XML with ADO.NET
DataReader object depending on the data provider. Besides ExecuteReader, the Command object
Chapter 7 - Data Binding
defines three more execute and methods.
Windows Forms
TheseData-Bound
methods areControls
ExecuteNonQuery,ExecuteScalar, and
Chapter 8 - Constraints .and
ExecuteXmlReader TheData Relations
ExecuteReader method produces a DataReader, which is the solution for
streaming
Chapter 9 -data through
ADO.NET ADO.NET.
Exception (We discuss the DataReader in more details in "The DataReader:
Handling
Walking
Chapter 10through thewith
- Working Data.")
the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
TheExecuteNonQuery method allows you to execute a SQL statement or a Command object with the
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
CommandText property and a SQL statement without using a DataSet. This method doesn't take any
Chapter 13 - Developing a Custom Data Provider
parameters and returns the number of rows affected by the execute operation.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using
For example, you ADO.NET in Xml
could have Web Services
an UPDATE, INSERT, or DELETE statement in your CommandText and then
callExecuteNonQuery
Chapter to execute
16 - ASP.NET Server Controls itand
directly
Data on your database.
Binding
Chapter 17 - Building Real-World Web Applications
Listing 4-15 shows how to insert a row into the Northwind database using the ExecuteNonQuery
Chapter 18 - Object-Relational Mapping in .NET
method. You can even use UPDATE and DELETE SQL queries to update and delete data from a database.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
(We use these statements in later examples.) In this example, you create an INSERT query and call
Chapter 20 - COM Interoperability and ADO.NET
ExecuteNonQuery.
Chapter 21 - Messaging
Chapter
Listing22 - SQL
4-15: ServerRecords
Adding and ADO.NET: Notes Using
to a Table on Performance
the INSERT SQL Statement
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
' Create a Connection Object
Appendix C - ADO.NET Frequently Asked Questions
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
Index
"Initial Catalog=Northwind;Data Source=MCB;"
List
Dim of Figures
conn As SqlConnection = New SqlConnection(ConnectionString)
Listopen
of Tables
an existing Connection to the Database and Create a
List
' of Listings Object with it:
Command
List of Sidebars
conn.Open()
Dim cmd As SqlCommand = New SqlCommand("Customers", conn)
' Assign the SQL Insert statement we want to execute to the CommandText
cmd.CommandText = "INSERT INTO Customers" & _
"(Address, City, CompanyName, ContactName, CustomerID)" & _
"VALUES ('111 Broad St.', 'NY', 'Xerox', 'Fred Biggles', 1400)"
' Call ExecuteNonQuery on the Command Object to execute insert
Dim res As Integer
res = cmd.ExecuteNonQuery()
Console.WriteLine("Affected Rows :" + res.ToString())
' release objects
conn.Close()
conn.Dispose()
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Note If you'reThis text provides
deleting, extensive
inserting, coverage
and updating of ADO.NET
data with knowntechnology
parameters, using Execute methods
including ADO.NET internals, namespaces,
is faster than using DataAdapter'sUpdate method. classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
is a handy method for using a SQL statement that retrieves a single value. A good
ExecuteScalarADO.NET.
example of this is retrieving the number of rows from a database. Listing 4-16 retrieves the total number of
rows from the Customers table. Then you assign the SQL command for getting the row count in
Customers
Table to the Command object, and you call ExecuteScalar to retrieve the counter.
of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Listing 4-16: Using the ExecuteScalar Method to Retrieve a Single Value
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data a
' Create Components in Visual
Connection Studio .NET
Object
Dim ConnectionString
Chapter As String
3 - ADO.NET in Disconnected = "Integrated Security=SSPI;" & _
Environments
Chapter 4 "Initial
- ADO.NET Catalog=Northwind;Data
in Connected Environments Source=MCB;"
ChapterDim
5 -conn AsADO.NET
Handling SqlConnection
Events = New SqlConnection(ConnectionString)
Chapter'6 open an existing
- Integrating XML with Connection
ADO.NET to the Database and Create a
Chapter'7 Command Object
- Data Binding with it:
and Windows Forms Data-Bound Controls
conn.Open()
Chapter 8 - Constraints and Data Relations
Dim cmd As SqlCommand = New SqlCommand("Customers", conn)
Chapter 9 - ADO.NET Exception Handling
' Assign the SQL Insert statement we want to execute to the CommandText
Chapter 10 - Working with the ODBC .NET Data Provider
cmd.CommandText = "SELECT Count(*) FROM Customers"
Chapter 11 - Stored Procedures and Views
' Call ExecuteNonQuery on the Command Object to execute insert
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Dim res As Integer
Chapterres
13 -=Developing a Custom Data Provider
cmd.ExecuteScalar()
ChapterConsole.WriteLine("Total
14 - Developing Database Web Applications
Rows :"using ASP.NET
+ res.ToString())
Chapter'15 - Using ADO.NET
release objects in Xml Web Services
Chapterconn.Close()
16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
conn.Dispose()
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
If you run
Chapter 21 Listings 4-17, it will display only the total row number on the console.
- Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Listing 4-17: DataReader Reads Data from a SQL Server Database
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
' Create
Appendix a Connection
C - ADO.NET FrequentlyObject
Asked Questions
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
Index
"Initial Catalog=Northwind;Data Source=MCB;"
List of Figures
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
List of Tables
Dim SQL As String = "SELECT * FROM Customers"
List of Listings
' open a connection
List of Sidebars
conn.Open()
Dim cmd As SqlCommand = New SqlCommand(SQL, conn)
' Call ExecuteNonQuery on the Command Object to execute insert
Dim res As Integer
' Call ExecuteReader to return a DataReader
Dim reader As SqlDataReader = cmd.ExecuteReader()
Console.WriteLine("Customer ID, Contact Name," & _
"Contact Title, Address")
Console.WriteLine("===================================")
While reader.Read()
If Not reader.IsDBNull(0) Then
Console.Write(reader("CustomerID").ToString() + ", ")
Console.Write(reader("ContactName").ToString() + ", ")
Applied ADO.NET: Building Data-Driven Solutions+ ", ")
Console.Write(reader("ContactTitle").ToString()
by Mahesh Chand and David Talbot
Console.WriteLine(reader("Address").ToString() ISBN:1590590732
+ ", ")
End If Apress 2003 (928 pages)
End While This text provides extensive coverage of ADO.NET technology
including ADO.NET internals,
Console.WriteLine("Affected namespaces,
Records: " & _classes, and
interfaces, and takes a comprehensive look at XML
'reader.RecordsAffected.ToString())
namespaces and classes, and how to integrate both with
' release objects
ADO.NET.
conn.Close()
conn.Dispose()
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Using Other Command Methods
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET
TheCancel method in
of Disconnected
a SqlCommand Environments
object tries to cancel an execute operation. If there's no execute
Chapter 4 - ADO.NET in Connected Environments
operation or the Cancel method fails to cancel the execution, nothing happens.
Chapter 5 - Handling ADO.NET Events
TheCreateParameter
Chapter method
6 - Integrating XML creates a SqlParameter object.
with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
ThePrepare method creates a prepared version of the command. It doesn't affect the operation if
Chapter 8 - Constraints and Data Relations
CommandType is TableDirect. Before calling this method, you need to specify the data type of each
Chapter 9 - ADO.NET Exception Handling
parameter in the statement to be prepared.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and
TheResetCommandTimeout Viewsresets the command timeout value to the default value, which is
method
Chapter 12 -
30 seconds. Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
We use14these
Chapter methodsDatabase
- Developing in later examples.
Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
The DataReader: Walking
Applied ADO.NET: through
Building the Data
Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
As discussed earlier in this
Apress chapter,
2003 there are two common ways to fetch data from a data source: using a
(928 pages)
DataSet and using a DataReader. A DataReader
This text provides extensive coverage ofprovides
ADO.NET antechnology
easy way for the programmer to read
data from a database as if it were coming from a stream. The
including ADO.NET internals, namespaces, classes, and DataReader is the solution for forward-
interfaces,
streaming data through and takes
ADO.NET. a comprehensive
The DataReader look at XML
is also called a firehose cursor or forward, read-
namespaces
onlycursor because it moves and classes,
forward and how
through to integrate
the data. both with
The DataReader not only allows you to move
ADO.NET.
forward through each record of a database, but it also enables you to parse the data from each column.

Similar to other data components, each data provider has a DataReader class. For example,
Table of Contents
OleDbDataReader is the DataReader class for OleDb data providers. Similarly, SqlDataReader and
Applied ADO.NETBuilding Data-Driven Solutions
ODBC DataReader are DataReader classes for the Sql and ODBC data providers, respectively.
Introduction
Chapter 1 - ADO.NET Basics
Initializing
Chapter 2 - Data a DataReader
Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
As you saw previously, you call the ExecuteReader method of the Command object, which returns an
Chapter 4 - ADO.NET in Connected Environments
instance of the DataReader. For example, you use the following code:
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Dim cmd As SqlCommand = New SqlCommand(SQL, conn)
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
' Call ExecuteReader to return a DataReader
Chapter 8 - Constraints and Data Relations
Dim reader As SqlDataReader = cmd.ExecuteReader()
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored
Once you're Procedures
done with and Views call the Close method to close a DataReader:
a DataReader,
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
reader.Close()
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Understanding DataReader Properties and Methods
Chapter 17 - Building Real-World Web Applications
Chapter 18 -describes
Table 4-9 Object-Relational Mapping inproperties,
the DataReader .NET and Table 4-10 describes the DataReader methods.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - The
Table 4-9: COMDataReader
Interoperability and ADO.NET
Properties
Chapter 21 - Messaging
PROPERTY DESCRIPTION
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix
DepthA - Relational Databases: Some
Indicates the Basic
depthConcepts
of nesting for a row
Appendix B - Commonly Used SQL Statements
FieldCount Returns
Appendix C - ADO.NET Frequently number
Asked of columns in a row
Questions
Index
IsClosed Indicates whether a DataReader is closed
List of Figures
Item
List of Tables
Gets the value of a column in native format
ListRecordsAffected
of Listings Number of rows affected after a transaction
List of Sidebars

Table 4-10: The DataReader Methods


METHOD DESCRIPTION
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Close Closes a DataReader object.
Apress 2003 (928 pages)
IsDBNull Represents
This whether
text provides a column
extensive contains
coverage null values.
of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
Read Reads the
interfaces, next
and record
takes in the DataReader.
a comprehensive look at XML
namespaces and classes, and how to integrate both with
Advances the DataReader to the next result during batch transactions.
NextResult ADO.NET.
GetXXX There are dozens of GetXXX methods. These methods read a specific data
Table of Contents type value from a column. For example, GetChar will return a column value as
a character
Applied ADO.NETBuilding and GetString
Data-Driven Solutions as a string.
Introduction
Chapter 1 - ADO.NET Basics
Reading
Chapter 2 with
- Data the DataReader
Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Once a4DataReader
Chapter - ADO.NET inisConnected
initialized,Environments
you can utilize its various methods to read your data records. Foremost,
you can use
Chapter 5
the Read method, which, when called repeatedly, continues to read each row of data into the
- Handling ADO.NET Events
DataReader object. The DataReader also provides a simple indexer that enables you to pull each
Chapter 6 - Integrating XML with ADO.NET
column of data from the row. Listing 4-17 is an example of using the DataReader in the Northwind
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
database for the Customers table and displaying data on the console.
Chapter 8 - Constraints and Data Relations
Chapter
As you 9can- ADO.NET Exception
see, you use similarHandling
steps as in previous examples. Initially, you create a Connection object,
Chapter 10Command
create a - Workingobject,
with the ODBC
call .NET Data Provider method, call the DataReader'sRead method until
the ExecuteReader
the end11
Chapter of -the data,Procedures
Stored and then display the data. At the end, you release the DataReader and
and Views
Connection
Chapter objects.
12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Figure 4-2 shows the output of Listing 4-17.
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Figure 4-2: Output of the Customers table from the DataReader
Index
List of Figures
SqlDataReader provides many Read methods, which allow you to get the value of a column as a
List of Tables
specific type. For instance, you could rewrite this line from the previous example:
List of Listings
List of Sidebars
Dim str As String = reader("CustomerID").ToString()

as this:

Dim str As String = reader.GetString(0)

With the GetString method of CustomerID, you don't need to do any conversion, but you do have to
know the zero-based column number of CustomerID (which, in this case, is zero).
InterpretingApplied
Batches of Building
ADO.NET: Queries Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
TheDataReader also has methods that enable you to read data from a batch of SQL queries. The
Apress 2003 (928 pages)
following is an example of a batch transaction on the Customers and Orders tables. The NextResult
This text provides extensive coverage of ADO.NET technology
method allows you to obtain each query result from the batch of queries performed on both tables. In this
including ADO.NET internals, namespaces, classes, and
example, after creating a Connection
interfaces, object, you setlook
and takes a comprehensive up your Command object to do a batch query on the
at XML
Customers and the Orders tables:
namespaces and classes, and how to integrate both with
ADO.NET.

Dim cmd as SqlCommand = new SqlCommand(


Table"SELECT * FROM Customers;SELECT * FROM Orders",
of Contents
conn)
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter
Now you1 can- ADO.NET Basics
create the Reader through the Command object. You then use a result flag as an indicator to
Chapter
check if2 you've
- Datagone
Components
through in
all Visual StudioThen
the results. .NET you loop through each stream of results and read the
Chapter 3 a-string
data into ADO.NET
untilinit Disconnected Environments
reads 10 records. After that, you show results in a message box (see Listing 4-18).
Chapter 4 - ADO.NET in Connected Environments
Listing54-18:
Chapter Executing
- Handling Batches
ADO.NET Using DataReader
Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data
' Create Binding and Windows
a Connection ObjectForms Data-Bound Controls
Chapter 8 - Constraints and Data
Dim ConnectionString As Relations
String = "Integrated Security=SSPI;" & _
Chapter "Initial
9 - ADO.NET Exception Handling
Catalog=Northwind;Data Source=MCB;"
Chapter 10 - Working
Dim conn with the ODBC .NET
As SqlConnection DataSqlConnection(ConnectionString)
= New Provider
Dim SQL
Chapter 11 -As String
Stored = "SELECT
Procedures and Views* FROM Customers; SELECT * FROM Orders"
' open
Chapter 12 a- Oracle,
connection
SQLXML, and Other .NET Data Providers
conn.Open()
Chapter 13 - Developing a Custom Data Provider
Dim cmd
Chapter 14 -As SqlCommand
Developing = Web
Database NewApplications
SqlCommand(SQL, conn)
using ASP.NET
' Call
Chapter 15 ExecuteNonQuery onWeb
- Using ADO.NET in Xml the Command Object to execute insert
Services
Dim res As Integer
Chapter 16 - ASP.NET Server Controls and Data Binding
' Call ExecuteReader to return a DataReader
Chapter 17 - Building Real-World Web Applications
Dim reader As SqlDataReader = cmd.ExecuteReader()
Chapter 18 - Object-Relational Mapping in .NET
Dim str As String = ""
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Dim counter As Integer
Chapter 20 - COM Interoperability and ADO.NET
Dim bNextResult As Boolean = True
Chapter
While21bNextResult
- Messaging = True
Chapter
While -reader.Read()
22 SQL Server and ADO.NET: Notes on Performance
Appendix
strA +=- Relational Databases: Some Basic Concepts + " ,"
reader.GetValue(0).ToString()
Appendix B - Commonly
counter = counter Used SQL
+ 1Statements
Appendix C - ADO.NET
If counter = 10Frequently
Then Asked Questions
Index Exit While
End
List of If
Figures
End
List While
of Tables
ListMessageBox.Show(str)
of Listings
bNextResult
List of Sidebars = reader.NextResult()
End While
' release objects
conn.Close()
conn.Dispose()

After that you call the NextResult method, which gets the next query result in the batch. The result is
processed again in the Read method loop. The code displays data from both tables.
Tip One of theApplied
commonlyADO.NET:
askedBuilding
questionsData-Driven Solutionsdiscussion forums and newsgroups is
on ADO.NET-related
about whenby Mahesh Chand and David
to use DataReader vs. Talbot
when to use a DataSet. Here is a simple answer: You use a
ISBN:1590590732
DataReaderApressinstead of apages)
2003 (928 DataSet when you need read-only data and you don't need to alter
the data. This
Seetext
the provides
"Using aextensive coverage
DataSet vs. Using of ADO.NET technology
a DataReader" section for a more in-depth answer.
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Understanding Command Behaviors
namespaces and classes, and how to integrate both with
ADO.NET.
TheExecuteReader method of a Command object such as SqlCommand has two over-loaded forms, as
shown:
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Overloads Public Function ExecuteReader() As SqlDataReader
Introduction
Overloads Public Function ExecuteReader(CommandBehavior) As SqlDataReader
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 form
The first - ADO.NET
doesn'tintake
Disconnected Environments
any arguments, but the second form takes an argument of type
Chapter 4 - ADO.NET inThe
CommandBehavior. Connected
Command Environments
behavior can control the action and return data as a result of
executing
Chapter 5 a - Handling
Command. ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
The default
Chapter 7
value passed in the ExecuteReader is CommandBehavior.Default, which returned the
- Data Binding and Windows Forms Data-Bound Controls
same results as ExecuteReader with no arguments.
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
WhenCommandBehavior.CloseConnection is passed in ExecuteReader, the associated
Chapter 10 - Working
Connection objectwith the ODBC
is closed when.NET
the Data Provider is closed. In this case, you don't have to call
DataReader
Chapter 11 - Stored Procedures
SqlConnection.Close and Views
explicitly.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
You can
Chapter 13fetch only a single
- Developing row from
a Custom Data aProvider
database by executing a SqlCommand. The
CommandBehavior.SingleRow
Chapter value
14 - Developing Database Web passed in
Applications ExecuteReader
using ASP.NET returns the single row from a table
instead15
Chapter of -the result
Using set. in Xml Web Services
ADO.NET
Chapter 16 - ASP.NET Server Controls and Data provides
CommandBehavior.SequentialAccess Binding a way for the DataReader to handle rows that
Chapter 17 - Building Real-World Web Applications
contain large binary values (BLOB data) such as images or memo fields. SequentialAccess values
Chapter 18 - Object-Relational
enableDataReader to load Mapping
data as ainstream
.NET of bytes or characters.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
By setting
Chapter 20 -the Interoperability and ADO.NET value, the DataReader doesn't return data. It returns the
CommandBehavior.KeyInfo
COM
table column
Chapter and primary key information instead. This is useful when you're collecting metadata.
21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
TheCommandBehavior.SchemaOnly value returns column information only.
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
The DataAdapter: Adapting
Applied ADO.NET: toData-Driven
Building Your Environment
Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
As you saw in Figure 4-1,
Apress a DataAdapter
2003 (928 pages) plays a vital role in the ADO.NET architecture. It sits between a
data source and a DataSet, and it passes data
This text provides extensive coveragefrom of
the data source
ADO.NET to the DataSet, and vice versa, with
technology
or without using commands.
including ADO.NET internals, namespaces, classes, and as DataSet, DataTable,
Now you'll use disconnected classes such
interfaces, and takes atocomprehensive
DataView, and DataViewManager look
write interactive at XML applications based on Windows Forms
database
and Web Forms.namespaces and classes, and how to integrate both with
ADO.NET.
TheDataAdapter enables you to connect to a database and specify SQL strings for retrieving data from or
writing data to a DataSet. As you know, a DataSet represents in-memory cached data. An in-memory
Table of Contents
object frees you from the confines of the specifics of a database and allows you to deal with the data in
Applied ADO.NETBuilding Data-Driven Solutions
memory. The DataAdapter serves as an intermediary between the database and the DataSet.
Introduction
Chapter 1 - ADO.NET Basics
Constructing
Chapter a DataAdapter
2 - Data Components in Visual Studio Object
.NET
Chapter 3 - ADO.NET in Disconnected Environments
TheDataAdapter constructor has many overloaded forms. You can create a DataAdapter using a
Chapter 4 - ADO.NET in Connected Environments
constructor with no arguments, pass a Command object, pass a Command object with Connection object as
Chapter 5 - Handling ADO.NET Events
arguments, or use a combination of these. You can also specify a SQL statement as a string for querying a
Chapter 6 table
particular - Integrating
or moreXML thanwith
oneADO.NET
table. You can also specify the connection string or a Connection object
Chapter 7 - Data Binding and Windows
to connect to the database. Listing 4-19 Forms Data-Bound
creates Controls
four SqlDataAdapter instances with different approaches.
Chapter
As you 8can- Constraints
see, the code andfirst
Data Relations
creates and opens SqlConnection and SqlCommand objects. Later you use
Chapter 9 -objects
these two ADO.NET Exception
to create Handling
DataAdapters. The first DataAdapter,da1, takes only one parameter of type
Chapter 10 - Working
SqlCommand. with SqlCommand
Because the ODBC .NETalready
Data Provider
has a SqlConnection, there's no need to specify a
connection
Chapter 11 - explicitly with the DataAdapter.
Stored Procedures and Views The second DataAdapter,da2, is created using no
argument,
Chapter 12 -and laterSQLXML,
Oracle, its SelectCommand
and Other .NET property is set to a SqlCommand. The third DataAdapter,da3,
Data Providers
takes a string argument as a SQL statement
Chapter 13 - Developing a Custom Data Provider and a second argument as SqlConnection. The last
DataAdapter takes both string arguments, one containing
Chapter 14 - Developing Database Web Applications using ASP.NET the SQL statement and the second containing
the connection string.
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Listing 4-19: CreatingSqlDataAdapter Instances
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
' Create
Chapter a Connection
19 - Mapped Object Considerations and Data Binding
Objects: Performance
Dim ConnectionString
Chapter As and
20 - COM Interoperability String = "Integrated Security=SSPI;" & _
ADO.NET
"Initial Catalog=Northwind;Data Source=MCB;"
Chapter 21 - Messaging
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Dim SQL As String = "SELECT * FROM Customers"
Appendix A - Relational Databases: Some Basic Concepts
' open a connection
Appendix B - Commonly Used SQL Statements
conn.Open()
Appendix C - ADO.NET Frequently Asked Questions
Dim cmd As SqlCommand = New SqlCommand(SQL, conn)
Index
' Creating SqlDataAdapter using different constructors
List
Dim of Figures
da1 As SqlDataAdapter = New SqlDataAdapter(cmd)
List
Dim Tables
of da2 As SqlDataAdapter = New SqlDataAdapter()
List of Listings
da2.SelectCommand = cmd
List
Dim of Sidebars
da3 As SqlDataAdapter = New SqlDataAdapter(SQL, conn)
Dim da4 As SqlDataAdapter = New SqlDataAdapter(SQL, ConnectionString)

As discussed, there's no difference between creating OleDb, Sql, and Odbc DataAdapters. The only
difference is the connection string. For example, Listing 4-20 shows you how to create an
OleDbDataAdapter object. Listing 4-20 uses the Access 2000 Northwind database and accesses all
records of the Orders table by using a SELECT * SQL query.

Listing 4-20: Executing a SELECT * Statement Using OleDbDataAdapter


' Create a Connection Object
Applied ADO.NET:
Dim ConnectionString As String Building Data-Driven Solutions
= "Provider=Microsoft.Jet.OLEDB.4.0;" & _
by Mahesh Chand
"Data Source=c:\\Northwind.mdb" and David Talbot ISBN:1590590732
Apress =2003
Dim SQL As String (928 pages)* FROM Orders"
"SELECT
This text provides
Dim conn As OleDbConnection extensive
= New coverage of ADO.NET technology
OleDbConnection(ConnectionString)
including
' Open the connection ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
conn.Open() namespaces and classes, and how to integrate both with
' Create an OleDbDataAdapter
ADO.NET. object
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand(SQL, conn)
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
You can
Chapter 1 also use a DataAdapter's
- ADO.NET Basics Command properties by using the Command object with
OleDbDataAdaper.
Chapter For example,
2 - Data Components theStudio
in Visual following
.NETcode uses OleDbCommand to set the SelectCommand
property
Chapter 3 of- the DataAdapter.
ADO.NET You can
in Disconnected see that OleDbDataAdapter has no arguments as its constructor:
Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling
' Create ADO.NET Events
an OleDbDataAdapter object
Dim adapter
Chapter As OleDbDataAdapter
6 - Integrating XML with ADO.NET = New OleDbDataAdapter()
adapter.SelectCommand
Chapter = New Forms
7 - Data Binding and Windows OleDbCommand(SQL,
Data-Bound Controlsconn)
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Understanding DataAdapter Properties
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
As you 12
Chapter start workingSQLXML,
- Oracle, with DataAdapters,
and Other .NETyou
Dataneed take a quick look at the DataAdapter properties and
Providers
methods.
Chapter 13 The DataAdapter
- Developing has
a Custom fourProvider
Data properties that are Command objects; they represent the ways it can
query, insert,
Chapter delete, and
14 - Developing updateWeb
Database the database.
Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Table 4-11 describes SqlDbDataAdapter class properties, and Table 4-12 shows the
Chapter 16 - ASP.NET Server
OleDbDataAdapter Controls and Data Binding
properties.
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational
Table 4-11: Mapping Class
The SqlDataAdpater in .NETProperties
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
PROPERTY
Chapter DESCRIPTION
20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
AcceptChangesDuringFill TheAcceptChanges method of a DataRow saves the changes
Chapter 22 - SQL Server and ADO.NET:made
Notes toonaPerformance
DataRow. The True value of this property makes a
Appendix A - Relational Databases: Some Basic Concepts
call to the DataRow'sAcceptChanges property after it has
Appendix B - Commonly Used SQL Statements
been added to the DataTable. The False value doesn't. The
Appendix C - ADO.NET Frequently Askeddefault
Questions
value is True.
Index
ContinueUpdateOnError TheTrue value of this property keeps the update process
List of Figures
continuing even if an error occurred in a DataRow. The False
List of Tables
value generates an exception. The default value is False.
List of Listings
ListDeleteCommand
of Sidebars Represents a DELETE statement or stored procedure for
deleting records from the data source.
InsertCommand Represents an INSERT statement or stored procedure for
inserting a new record to the data source.
MissingSchemaAction Determines
Applied ADO.NET: Building the action Solutions
Data-Driven to be taken when incoming data doesn't
have a matching
by Mahesh Chand and David Talbot schema. This is a type of
ISBN:1590590732
MissingMappingAction
Apress 2003 (928 pages) enumeration, which has three
values:Error,Ignore, and Passthrough. The Error value
This text provides extensive coverage of ADO.NET technology
generates
including ADO.NET internals, an exception,
namespaces, the Ignore
classes, and value maps the unmatched
interfaces, and takes columns or tables,
a comprehensive andatthe
look XML Passthrough value adds the
namespaces and classes, andcolumn
source how to or
integrate
sourceboth
tablewith
created to the DataSet using its
ADO.NET.
original name.
MissingActionSchema Determines the action to be taken when a DataSetschema is
Table of Contents missing. This is a type of MissingSchemaAction
Applied ADO.NETBuilding Data-Driven enumeration,
Solutions which has three values: AddWithKey,Error, and
Introduction Ignore.AddWithKey adds the necessary columns and
Chapter 1 - ADO.NET Basics primary key information, Error generates an exception, and
Chapter 2 - Data Components in VisualIgnore ignores the extra columns.
Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
SelectCommand Represents a SELECT statement or stored procedure for
Chapter 4 - ADO.NET in Connected Environments
selecting records from a data source.
Chapter 5 - Handling ADO.NET Events
UpdateCommand
Chapter Represents an UPDATE statement or stored procedure for
6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windowsupdating records in Controls
Forms Data-Bound a data source.
Chapter 8 - Constraints and Data Relations
TableMappings Represents a collection of mappings between an actual data
Chapter 9 - ADO.NET Exception Handling
source table and a DataTable object.
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle,
Table 4-12: SQLXML, and Other
OleDbDataAdapter .NET DataProperties
Command Providers with Examples
Chapter 13 - Developing a Custom Data Provider
PROPERTY
Chapter EXAMPLEWeb Applications using ASP.NET
14 - Developing Database
Chapter 15 - Using ADO.NET
SelectCommand in Xml Web Services
cmd.SelectCommand.CommandText = "SELECT * FROM Orders
Chapter 16 - ASP.NET Server
ORDER Controls and Data Binding
BY Price";
Chapter 17 - Building Real-World Web Applications
DeleteCommand cmd.DeleteCommand.CommandText = "DELETE FROM Orders WHERE
Chapter 18 - Object-Relational Mapping in .NET
LastName ='Smith'";
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability
InsertCommand and ADO.NET
cmd.InsertCommand.CommandText = "INSERT INTO Orders
Chapter 21 - Messaging VALUES (25,'Widget1','Smith')";
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
UpdateCommand cmd.UpdateCommand.CommandText = "UPDATE Orders SET
Appendix A - Relational Databases: Some Basic Concepts
ZipCode='34956' WHERE OrderNum =14";
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Listing 4-21 sets some of the SqlDataAdapter properties.
Index
List of Figures
Listing 4-21: UsingSqlDataAdapter Properties
List of Tables
List of Listings
' Create an OleDbDataAdapter object
List ofDim
Sidebars
adapter As SqlDataAdapter = New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand(SQL, conn)
adapter.AcceptChangesDuringFill = True
adapter.ContinueUpdateOnError = True
adapter.MissingSchemaAction = MissingSchemaAction.Error
adapter.MissingMappingAction = MissingMappingAction.Error

Understanding DataAdapter Methods


TheDataAdapter class provides many useful methods. For instance, the Fill method of the
DataAdapter fills data from a DataAdapter to the DataSet object, and the Update method stores data
from a DataSetApplied
object toADO.NET: Building Data-Driven Solutions
the data source.
by Mahesh Chand and David Talbot ISBN:1590590732
Table 4-13 describes some
Apress of(928
2003 thepages)
OleDbDataAdapter methods.
This text provides extensive coverage of ADO.NET technology
including ADO.NET
Table 4-13: The DataAdapter internals, namespaces, classes, and
Methods
interfaces, and takes a comprehensive look at XML
METHOD namespaces DESCRIPTION
and classes, and how to integrate both with
ADO.NET.
Fill Fills data records from a DataAdapter to a DataSet object depending
on the selection query.
Table of Contents
FillSchema
Applied This method
ADO.NETBuilding Data-Driven adds a DataTable to a DataSet and configures the
Solutions
Introduction schema to match that in the data source.
Chapter 1 - ADO.NET Basics This method retrieves parameters that are used when a SELECT
GetFillParameters
Chapter 2 - Data Componentsstatement
in Visual Studio .NET
is executed.
Chapter 3 - ADO.NET in Disconnected Environments
This method
Update - ADO.NET in Connected
Chapter 4
stores data from a DataSet to the data source.
Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Filling -the
Chapter 7 Data DataSet
Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
TheFill method
Chapter 9
is the primary method for bringing data into a DataSet from a data source. This
- ADO.NET Exception Handling
command uses the SelectCommand SQL statement to fill a DataSet memory structure consisting of
Chapter 10 - Working with the ODBC .NET Data Provider
DataTables, DataRows, DataColumns, and DataRelations. The Fill method has eight overloaded
Chapter 11 - Stored Procedures and Views
forms. Using these methods, you can fill a DataSet from a DataAdapter by selecting a particular table,
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
selecting a range of rows, and selecting rows based on the CommandBehavior. The following shows
Chapter 13 - Developing a Custom Data Provider
different overloaded forms of the Fill method defined in the DbDataAdapter class:
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Overloads Overrides Public Function Fill(DataSet) As Integer _
Chapter 16 - ASP.NET Server Controls and Data Binding
Implements IDataAdapter.Fill
Chapter 17 - Building Real-World Web Applications
Overloads Public Function Fill(DataTable) As Integer
Chapter 18 - Object-Relational Mapping in .NET
Overloads Public Function Fill(DataSet, String) As Integer
Chapter
Overloads19 - Mapped Objects: Performance
Overridable Protected Considerations
Function and Data Binding
Fill(DataTable, IDataReader) _
Chapter 20 -
As Integer COM Interoperability and ADO.NET
Chapter
Overloads21 - Messaging
Overridable Protected Function Fill(DataTable, _
Chapter 22 - SQL Server
IDbCommand, and ADO.NET: Notes
CommandBehavior) As on Performance
Integer
Appendix A - Relational Databases: Some Basic Concepts
Overloads Public Function Fill(DataSet, Integer, Integer, String) _
Appendix B - Commonly Used SQL Statements
As IntegerOverloads
Overridable
Appendix Protected
C - ADO.NET FrequentlyFunction Fill(DataSet, String, IDataReader, _
Asked Questions
Integer, Integer) As IntegerOverloads
Index
Overridable
List of Figures Protected Function Fill(DataSet, Integer, Integer, _
ListString,
of Tables IDbCommand, CommandBehavior) As Integer
List of Listings
List of Sidebars
The following form of the Fill method selects or refreshes rows in a specified range in the DataSet:

Overloads Public Function Fill(_


ByVal dataSet As DataSet, _
ByVal startRecord As Integer, _
ByVal maxRecords As Integer, _
ByVal srcTable As String _
) As Integer

In this example, dataSet is a DataSet,startRecord is the zero-based record number to start with,
maxRecords is the maximum number of records to retrieve, and srcTable is the name of the source table
to use for table mapping. The following code calls the same overloaded method to get or refresh rows from
the 9th position toApplied
the 15th ADO.NET:
position of Building Data-Driven
the Orders Solutions
table. In other words, it fills the DataSet with seven rows if
found in the table:by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
adapter.Fill(ds, 9, 15, " Orders")
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
You can use a DataSet
ADO.NET.directly or a DataTable to fill or refresh the data of a DataSet. Listing 4-22 calls
theFill method to fill data to a DataSet, which later can bind with data-bound controls.

Table of Contents
Listing 4-22: Calling a DataAdapter'sFill Method
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
' Create an OleDbDataAdapter object
Chapter 1 - ADO.NET Basics
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
Chapter 2 - Data Components in Visual
adapter.SelectCommand Studio
= New .NET
SqlCommand(SQL, conn)
Chapter 3 - ADO.NET
' Create in Disconnected
DataSet Object Environments
Chapter
Dim4 ds- ADO.NET in Connected
As DataSet = NewEnvironments
DataSet("Orders")
Chapter 5 - Handling
' Call ADO.NET Events
DataAdapter's Fill method to fill data from the
Chapter 6 - Integratingto
' DataAdapter XMLthe
with DataSet
ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
adapter.Fill(ds)
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Adding a DataTable to a DataSet
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
TheFillSchema
Chapter method
13 - Developing adds aData
a Custom Provider to a DataSet. The FillSchema method has two
DataTable
arguments;
Chapter the first is DataTable,
14 - Developing Database Weband the second
Applications is SchemaType.
using ASP.NET The SchemaType argument defines
the handling
Chapter of existing
15 - Using ADO.NETschema.
in XmlItWeb
has Services
two values: Mapped and Source. The SchemaType.Mapped value
means16
Chapter you- can applyServer
ASP.NET any existing table
Controls and mapping to the incoming schema and configure the DataSet with
Data Binding
the transformed
Chapter schema.
17 - Building The SchemaType.Source
Real-World Web Applications value means you can ignore any table mappings on
theDataAdapter
Chapter and configure
18 - Object-Relational the DataSet
Mapping in .NET using the incoming schema without applying any
transformations.
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter
Listing 20 - COM
4-23 usesInteroperability
the FillSchema and method
ADO.NETto add a DataTable to a DataSet.
Chapter 21 - Messaging
Listing22
Chapter 4-23: Using
- SQL theand
Server Method
ADO.NET: Notes
FillSchema of SqlDataAdapter
on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix
' CreateB - Commonly Used SQL Statements
an OleDbDataAdapter object
Appendix C - ADO.NET
Dim adapter Frequently Asked Questions
As SqlDataAdapter = New SqlDataAdapter()
Index
adapter.SelectCommand = New SqlCommand(SQL, conn)
List
' of Figures DataSet Object
Create
List of Tables
Dim ds As DataSet = New DataSet("Orders")
List
' of Listings
Call DataAdapter's Fill method to fill data from the
' of
List DataAdapter
Sidebars to the DataSet
adapter.Fill(ds)
' Adding a DataTable to the DataSet
Dim table As DataTable() = _
adapter.FillSchema(ds, SchemaType.Mapped, "Categories")

Looking at a DataAdapter Example


Now you'll create your first sample using DataAdapters. In this example, you'll learn how to create
DataAdapters using the Sql and OleDb data providers and fill data from a DataAdapter to a DataGrid
control.
Applied ADO.NET: Building Data-Driven Solutions
First, create a Windows
by Maheshapplication
Chand and using Visual
David Basic ProjectsISBN:1590590732
Talbot and add two Button controls and a
DataGrid control to the form
Apress by pages)
2003 (928 dragging the controls from the Toolbox. Second, set both buttons' Name
property; use OleDbDataAdapter and SqlDataAdapter.
This text provides extensive coverage of ADO.NETNext, set the Text properties to
technology
OleDbDataAdapter including
andADO.NET internals, namespaces,
SqlDataAdapter. classes,
After setting theseandproperties, the form will look like Figure 4-
3. As you can see,interfaces,
there areandtwotakes a comprehensive
buttons, look at XML
shown as OleDbDataAdapter and SqlDataAdapter.
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Figure 4-3: Creating a Windows Forms application and adding controls to the form
Chapter 9 - ADO.NET Exception Handling
Chapter
Now add10 button
- Working
clickwith thehandlers
event ODBC .NET
for Data
both Provider
the OleDbDataAdapter and SqlDataAdapter buttons. You can
Chapter 11 - Stored Procedures and Views
add a button click event handler either by double-clicking the button or by using the Events tab of the
Chapter 12 -window.
Properties Oracle, SQLXML, and Other .NET Data
On the OleDbDataAdapter Providers
button click event handler, you'll write code to read data from
an OleDb data source and fill data to the DataGrid
Chapter 13 - Developing a Custom Data Provider control. On the SqlDataAdapter button click event
handler,
Chapter 14you'll write code
- Developing to readWeb
Database dataApplications
from a SQLusing
Server data source and fill data to the DataGrid.
ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Listing 4-24 shows the source code for the OleDbDataAdapter button click, and Listing 4-25 shows the
Chapter 16 - ASP.NET Server Controls and Data Binding
source code for the SqlDataAdapter button click. As you can see, you follow the same steps as before.
Chapter 17 - Building Real-World Web Applications
Open a connection, create a DataAdapter object with a SELECT string, create a DataSet object, call a
Chapter 18 - Object-Relational Mapping in .NET
DataAdapter'sFill method to fill the DataSet, and bind the DataSet to the DataGrid control using
Chapter 19 - Mapped Objects: Performance
theDataGrid.DataSouce Considerations and Data Binding
property of DataSet.DefaultViewManager, which represents the default
Chapter 20 - COM Interoperability
view of a DataSet object. and ADO.NET
Chapter 21 - Messaging
Listing22
Chapter 4-24: Displaying
- SQL Server andthe OrdersNotes
ADO.NET: TableonData in a DataGrid Using OleDbDataAdapter
Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix
Private B -Sub
Commonly Used SQL Statements
OleDbDataAdapter_Click(ByVal sender As System.Object, _
Appendix
ByValC e- ADO.NET Frequently Asked Questions
As System.EventArgs) Handles OleDbDataAdapter.Click
Index' Create a Connection Object
List ofDim
Figures
ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
List of"Data
Tables Source=c:\\Northwind.mdb"
List ofDim SQL As String = "SELECT * FROM Orders"
Listings
List ofDim conn As OleDbConnection = New OleDbConnection(ConnectionString)
Sidebars
' Open the connection
conn.Open()
' Create an OleDbDataAdapter object
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand(SQL, conn)
' Create DataSet Object
Dim ds As DataSet = New DataSet("Orders")
' Call DataAdapter's Fill method to fill data from the
' DataAdapter to the DataSet
adapter.Fill(ds)
' Bind data set to a DataGrid control
DataGrid1.DataSource = ds.DefaultViewManager
End Sub Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET
Listing 4-25: Displaying internals,
the Customers namespaces,
Table's Data inclasses, and
a DataGrid Using SqlDataAdapter
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.
Private Sub SqlDataAdapter_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SqlDataAdapter.Click
' Create a Connection Object
Table of Contents
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
Applied ADO.NETBuilding Data-Driven Solutions
"Initial Catalog=Northwind;Data Source=MCB;"
Introduction
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Chapter 1 - ADO.NET Basics
Dim SQL As String = "SELECT * FROM Customers"
Chapter 2 - Data Components in Visual Studio .NET
' Open the connection
Chapter 3 - ADO.NET in Disconnected Environments
conn.Open()
Chapter 4 - ADO.NET
' Create in Connected Environments
an OleDbDataAdapter object
Chapter 5 - Handling ADO.NET Events
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
Chapter 6 - Integrating XML with ADO.NET
adapter.SelectCommand = New SqlCommand(SQL, conn)
Chapter 7 - Data DataSet
' Create Binding andObject
Windows Forms Data-Bound Controls
Chapter 8 ds
Dim - Constraints
As DataSetand Data Relations
= New DataSet("Orders")
' Call
Chapter DataAdapter's
9 - ADO.NET Fill method to fill data from the
Exception Handling
' DataAdapter
Chapter 10 - Working withtothethe
ODBCDataSet
.NET Data Provider
adapter.Fill(ds)
Chapter 11 - Stored Procedures and Views
' Adding
Chapter a SQLXML,
12 - Oracle, DataTable to the
and Other .NETDataSet
Data Providers
Dim table As DataTable() = _
Chapter 13 - Developing a Custom Data Provider
adapter.FillSchema(ds, SchemaType.Mapped, "Categories")
Chapter 14 - Developing Database Web Applications using ASP.NET
' Bind data set to a DataGrid control
Chapter 15 - Using ADO.NET in Xml Web Services
DataGrid1.DataSource = ds.DefaultViewManager
Chapter 16 - ASP.NET Server Controls and Data Binding
End Sub
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - of
The output COM Interoperability
Listing 4-24 looks and ADO.NET
like Figure 4-4.
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars

Figure 4-4: Filling data from an Access database to a DataGrid control using OleDbDataAdapter

The output of Listing 4-25 looks like Figure 4-5.


Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Figure
Chapter 1 - 4-5: Filling
ADO.NET data from a SQL Server database to a DataGrid control using SqlDataAdapter
Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Filling -the
Chapter 4 DataAdapter
ADO.NET from a Recordset
in Connected Environments
Chapter 5 - Handling ADO.NET Events
Because
Chapter 6 Microsoft realized
- Integrating it would
XML with be difficult for some developers to let go of their ADO recordsets (or
ADO.NET
perhaps they
Chapter 7
have some legacy applications or components that would be difficult to convert), the
- Data Binding and Windows Forms Data-Bound Controls
DataAdapter'sFill method also allows you to fill a DataSet with an ADO recordset. You can't, however,
Chapter 8 - Constraints and Data Relations
go the other way and fill a recordset with a Dataset. The Fill method appends rows from the recordset to
Chapter 9 - ADO.NET Exception Handling
the existing DataSet'sDataTable. If a primary key exists for the DataRows in the DataSet, then the Fill
Chapter 10 - Working with the ODBC .NET Data Provider
method will attempt to update rows from the recordset with a matching primary key. You can call the
Chapter 11 - Stored Procedures and Views
recordset with the |following code:
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
OleDbDataAdapter1.Fill(ds, anADORecordset, SourceTableName)
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Updating the Database Using the Update Method
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
The architecture of a DataAdapter has been designed so that you can make any changes you want to the
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
data in a filled DataSet, without affecting the database until you call the Update method. When Update is
Chapter
called, 20
the-DataAdapter
COM Interoperability and ADO.NET
will attempt to execute each query (UPDATE,INSERT,DELETE) on every row of
Chapter 21 - Messaging
theDataSet that has been updated, inserted, and deleted. For example, if you call Delete on a row in the
Chapter
DataSet,22 -then
SQL when
ServerUpdate
and ADO.NET: Notes
is called on Performance
on the DataAdapter, the DeleteCommand of the DataAdapter
Appendix A - Relational Databases: Some Basic Concepts
will be called using the particular row in the DataSet.
Appendix B - Commonly Used SQL Statements
NoteC Keep
Appendix in mind
- ADO.NET that this Asked
Frequently Update is different from a SQL UPDATE statement.
Questions
Index
To delete a row, you create an SqlCommand object with a DELETE statement and set the DeleteCommand
List of Figures
property of the DataAdapter.Listing 4-26 creates a SqlCommand object with a DELETE statement where
List of Tables
EmployeeId = 10.
List of Listings
List of Sidebars
Listing 4-26: Setting the DeleteCommand Property of DataAdapter

' Set DeleteCommand property


adapter.DeleteCommand = New SqlCommand(
"DELETE from Employees where EmployeeID = 10", conn)

Also, you need to create a parameter for the command that maps to the EmployeeID in the database. (We
discuss parameters in more depth in the "Staying within the Parameters" section of this chapter.)

Listing 4-27 shows an example that creates and sets parameter properties for the Sql data provider.
Listing 4-27: Creating a SqlParameter
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Dim workParamApress
As SqlParameter
2003 (928 pages) = Nothing
adapter.DeleteCommand = New SqlCommand(_
This text provides extensive coverage of ADO.NET technology
"DELETE from including
EmployeesADO.NETwhere EmployeeID
internals, = classes,
namespaces, 10", conn)
and
workParam = interfaces, and takes a comprehensive look at XML
adapter.DeleteCommand.Parameters.Add(_
namespaces
"@EmployeeID", and classes, and how to integrate both with
OleDbType.Integer)
ADO.NET.
workParam.SourceColumn = "EmployeeID"
workParam.SourceVersion = DataRowVersion.Original
Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Now you create a DataSet and fill it with the employees from the Employees table in the Northwind
Chapter 1 - ADO.NET Basics
database, as shown in Listing 4-28.
Chapter 2 - Data Components in Visual Studio .NET
Chapter
Listing34-28:
- ADO.NET
Callinginthe
Disconnected Environments
Fill Method of a DataAdapter
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Dim ds As DataSet = New DataSet("EmployeeSet")
Chapter 6
- Integrating XML with ADO.NET
adapter.Fill(ds, "Employees")
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working
Now you're preparedwith the ODBC
to delete data.NET
fromData Provider
a table using a DataTable. By calling Delete on the last row in
theRows
Chapter 11collection, you mark and
- Stored Procedures the last row as deleted. To cause the actual deletion to take place in the
Views
Northwind
Chapter 12 -database, you need
Oracle, SQLXML, andtoOther
call Update onProviders
.NET Data the adapter. This causes the adapter to go through each
changed
Chapter 13row of the DataSet
- Developing and
a Custom seeProvider
Data which command needs to be called on that row. In the case of the last
row in the
Chapter 14 -Employees
Developingtable, you Web
Database call the DeleteCommand
Applications (see Listing 4-29).
using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Listing 4-29: Removing a Row and Calling the Update Method of a DataAdapter
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Dim Dt
Chapter 18 As DataTable =Mapping
- Object-Relational ds.Tables("Employees")
in .NET
Dim lastRow
Chapter As Objects:
19 - Mapped Integer = Dt.Rows.Count
Performance - and
Considerations 1 Data Binding
Dim firstName
Chapter As String and
20 - COM Interoperability = Dt.Rows(lastRow)("FirstName").ToString()
ADO.NET
Dim lastName As String = Dt.Rows(lastRow)("LastName").ToString()
Chapter 21 - Messaging
Dt.Rows(Dt.Rows.Count - 1).Delete()
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
adapter.Update(ds, "Employees")
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
Listing 4-30 shows the code that calls the Update method to delete a row from a DataSet and updates it
List
backof Figures
to the database using DataApdater'sUpdate method.
List of Tables
Listing
List 4-30: Calling a DataAdapter's Update Method
of Listings
List of Sidebars
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
"Initial Catalog=Northwind;Data Source=MCB;"
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
Dim workParam As SqlParameter = Nothing
adapter.DeleteCommand = New SqlCommand(_
"DELETE from Employees where EmployeeID = 10", conn)
workParam = adapter.DeleteCommand.Parameters.Add(_
"@EmployeeID", OleDbType.Integer)
workParam.SourceColumn = "EmployeeID"
workParam.SourceVersion = DataRowVersion.Original
adapter.SelectCommand = New SqlCommand("SELECT * FROM Employees", conn)
Dim ds As Applied
DataSet ADO.NET:
= New Building Data-Driven Solutions
DataSet("EmployeeSet")
by Mahesh Chand
adapter.Fill(ds, "Employees") and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
Dim Dt As DataTable = ds.Tables("Employees")
Dim lastRowThisAs
textInteger
provides extensive coverage of ADO.NET
= Dt.Rows.Count - 1 technology
including ADO.NET internals, namespaces, classes, and
Dim firstName As String = Dt.Rows(lastRow)("FirstName").ToString()
interfaces, and takes a comprehensive look at XML
Dim lastName As String
namespaces = Dt.Rows(lastRow)("LastName").ToString()
and classes, and how to integrate both with
Dt.Rows(Dt.Rows.Count
ADO.NET. - 1).Delete()
adapter.Update(ds, "Employees")
DataGrid1.DataSource = ds.DefaultViewManager
Table 'of release
Contents objects
Applied ADO.NETBuilding
conn.Close() Data-Driven Solutions
Introduction
conn.Dispose()
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4
- ADO.NET in Connected Environments
Table and
Chapter 5
Column Mapping
- Handling ADO.NET Events
Chapter
One of 6the- important
Integratingproperties
XML with of
ADO.NET
the DataAdapter is the TableMappings property. This property
Chapter 7 - Data Binding and Windows
contains a collection of DataTableMappingForms Data-Bound Controls
objects (from the System.Data.Common namespace). The
Chapter uses theand
8 - Constraints
DataAdapter Data Relations
DataTableMapping object to map the table name of the data source to the
DataTable
Chapter name ofException
9 - ADO.NET the DataSet. In general, the names for both sources can be the same.
Handling
Chapter 10 - Working with the ODBC .NET Data Provider
For example, Listing 4-31 constructs the Northwind database's Order Table Mapping and adds it to the
Chapter 11 - Stored Procedures and Views
DataAdapter.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter
Listing13 - Developing
4-31: a Custom Data Provider
UsingDataTableMapping to Map the Orders Table of Northwind
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
' Create a Connection Object
Chapter 16 - ASP.NET Server Controls and Data Binding
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
Chapter 17 - Building Real-World Web Applications
"Initial Catalog=Northwind; Data Source=MCB;"
Chapter 18 - Object-Relational Mapping in .NET
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Chapter'19 - Mapped
Open the Objects: Performance Considerations and Data Binding
connection
Chapterconn.Open()
20 - COM Interoperability and ADO.NET
Chapter'21 - Messaging
Create a DataTableMapping object
ChapterDim22 -dtMapping
SQL Server and AsADO.NET: Notes on Performance
DataTableMapping =
Appendix A New- Relational Databases: Some
DataTableMapping("Orders",Basic Concepts"mapOrders")
AppendixDimB -adapter
Commonly As Used SQL Statements
SqlDataAdapter =
Appendix C -New SqlDataAdapter("Select
ADO.NET Frequently Asked Questions * From Orders", conn)
Index ' Call DataAdapter's TableMappings.Add method
adapter.TableMappings.Add(dtMapping)
List of Figures
' Create a DataSet Object and call DataAdapter's Fill method
List of Tables
' Make sure you use new name od DataTableMapping i.e., MayOrders
List of Listings
Dim ds As DataSet = New DataSet()
List of Sidebars
adapter.Fill(ds, "mapOrders")
DataGrid1.DataSource = ds.DefaultViewManager
'Dispose
conn.Close()
conn.Dispose()

The default mapping for a DataTable is the Table alias. If you use this mapping name, then you don't
need to mention the table in the Fill method. Listing 4-32 shows an example using DataTableMapping
with the Table option.
Listing 4-32: UsingDataTableMapping with the Table Option
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
' Create a Connection Object
Apress 2003 (928 pages)
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
This text provides extensive coverage of ADO.NET technology
"Initial including
Catalog=Northwind;
ADO.NET internals,Data Source=MCB;"
namespaces, classes, and
Dim conn interfaces,
As SqlConnection = New SqlConnection(ConnectionString)
and takes a comprehensive look at XML
' Open the namespaces and classes, and how to integrate both with
connection
conn.Open() ADO.NET.
' Create a DataTableMapping object
Dim dtMapping
Table of Contents As DataTableMapping =
New DataTableMapping("Table",
Applied ADO.NETBuilding Data-Driven Solutions
"mapOrders")
Dim adapter As SqlDataAdapter =
Introduction
New SqlDataAdapter("Select * From Orders", conn)
Chapter 1 - ADO.NET Basics
' Call DataAdapter's TableMappings.Add method
Chapter 2 - Data Components in Visual Studio .NET
adapter.TableMappings.Add(dtMapping)
Chapter 3 - ADO.NET in Disconnected Environments
' Create a DataSet Object and call DataAdapter's Fill method
Chapter'4 Make
- ADO.NET
sure inyou
Connected
use newEnvironments
name od DataTableMapping i.e., MayOrders
ChapterDim
5 -ds Handling ADO.NET Events
As DataSet = New DataSet()
Chapteradapter.Fill(ds)
6 - Integrating XML with ADO.NET
ChapterDataGrid1.DataSource
7 - Data Binding and Windows Forms Data-Bound Controls
= ds.DefaultViewManager
Chapter'Dispose
8 - Constraints and Data Relations
Chapterconn.Close()
9 - ADO.NET Exception Handling
Chapterconn.Dispose()
10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
DataTables are not the only things aliased in ADO.NET. You can also alias the DataColumns using
Chapter 14 - Developing Database Web Applications using ASP.NET
DataColumnMapping objects. Why do you want column mapping? Let's say you have a table that has
Chapter 15 - Using ADO.NET in Xml Web Services
column names such as a1, a2, and so on, but when a user views the data in data-bound controls, you want
Chapter 16 - ASP.NET Server Controls and Data Binding
to display some meaningful names such as CustomerName and so on.
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational
Data column mapping allowsMapping in .NET
you to customize a DataSet's column names, which internally are mapped
Chapter 19 - Mapped
with a table columns.Objects:
You can Performance Considerations
achieve this by using and Data Binding
DataAdapter.DataTableMapping.ColumnMapping.
Chapter 20 - COM Interoperability and ADO.NET The DataColumnMapping property of
21 - Messaging is a collection of column mappings. You use the Add method of
DataTableMapping
Chapter
DataColumnCollection,
Chapter which adds
22 - SQL Server and ADO.NET: a column
Notes mapping. The Add method takes a column name of the
on Performance
source table
Appendix and a DataSet
A - Relational column
Databases: Somename. Listing 4-33 maps the OrderId,ShipName,ShipCity, and
Basic Concepts
OrderDate
Appendix columns of
B - Commonly theSQL
Used Orders table to mapID,mapName,mapCity, and mapDate, respectively.
Statements
Appendix C - ADO.NET Frequently Asked Questions
Listing 4-33: UsingDataColumnMapping
Index
List of Figures
' of
List Create
Tables a Connection Object
Dim ConnectionString As String = "Integrated Security=SSPI;" & _
List of Listings
"Initial Catalog=Northwind; Data Source=MCB;"
List of Sidebars
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
' Open the connection
conn.Open()
' Create a DataTableMapping object
Dim dtMapping As DataTableMapping = New DataTableMapping("Table", "Orders")
Dim adapter As SqlDataAdapter = New SqlDataAdapter _
("SELECT OrderID, ShipName, ShipCity, OrderDate FROM Orders", conn)
' Call DataAdapter's TableMappings.Add method
adapter.TableMappings.Add(dtMapping)
dtMapping.ColumnMappings.Add(New DataColumnMapping("OrderID", "mapID"))
dtMapping.ColumnMappings.Add(New DataColumnMapping("ShipName", "mapName"))
dtMapping.ColumnMappings.Add(New DataColumnMapping("ShipCity", "mapCity"))
dtMapping.ColumnMappings.Add(New DataColumnMapping("OrderDate", "mapDate"))
' Create Applied
a DataSetADO.NET:
ObjectBuilding
and Data-Driven Solutions
call DataAdapter's Fill method
by Mahesh Chand and David Talbot
' Make sure you use new name od DataTableMapping ISBN:1590590732
i.e., MayOrders
Dim ds AsApress 2003 (928
DataSet pages)DataSet()
= New
This text provides extensive coverage of ADO.NET technology
adapter.Fill(ds)
including ADO.NET=internals,
DataGrid1.DataSource namespaces, classes, and
ds.DefaultViewManager
interfaces, and takes a comprehensive look at XML
'Dispose namespaces and classes, and how to integrate both with
conn.Close()
ADO.NET.
conn.Dispose()

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Figure 4-6 displays the results of Listing 4-33.
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Figure 4-6: Column mapping
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter
The IDE16automatically
- ASP.NET Server Controls
generates and of
much Data
theBinding
mappings, so you don't have to worry about them. But,
Chapter 17 - Building Real-World Web Applications
occasionally, you may want to choose your own schema names for your DataSet that map back to the data
Chapter
source.18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
CommandBuilder: Easing
Applied ADO.NET: the Data-Driven
Building Work of Solutions
Programmers
by Mahesh Chand and David Talbot ISBN:1590590732
Sometimes creating SQL
Apress statements
2003 can be a lengthy job when dealing with many columns in a table. A
(928 pages)
CommandBuilder object reduces the burden
This text provides extensive of creating
coverage SQL statement
of ADO.NET for you. In other words, the
technology
CommandBuilder object helps you generate UPDATE, DELETE,
including ADO.NET internals, namespaces, classes, and and INSERT commands on a single
interfaces,
database table for and takes The
a DataAdapter. a comprehensive look at XML
OleDbCommandBuilder, SqlCommandBuilder, and
namespaces
OdbcCommandBuilder and classes,
classes representand how
the to integrate both with
CommandBuilder object in the OleDb, Sql, and Odbc
ADO.NET.
data providers (respectively).

Creating a CommandBuilder Object


Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Creating a CommandBuilder object is pretty simple. You pass a DataAdapter as an argument of the
Introduction
1 - ADO.NETcontructor.
CommandBuilder
Chapter Basics For example:
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET
// Create in Disconnected
a command builderEnvironments
object
Chapter 4 - ADO.NET in Connected
SqlCommandBuilder builderEnvironments
= new SqlCommandBuilder(adapter);
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET

Using SqlCommandBuilder
- Data Binding and Windows FormsMembers
Chapter 7 Data-Bound Controls
Chapter 8 - Constraints and Data Relations
TheDataAdapter
Chapter 9 - ADO.NETproperty of Handling
Exception a CommandBuilder represents the DataProvider attached to a
10 - Working object
CommandBuilder
Chapter forODBC
with the which.NET
automatic SQL statements are generated. The GetDeleteCommand,
Data Provider
GetUpdateCommand,
Chapter and GetInsertCommand
11 - Stored Procedures and Views methods return the Delete,Update, and Insert
commands
Chapter in the form
12 - Oracle, of a Command
SQLXML, and Other object. TheProviders
.NET Data RefreshSchema method refreshes the database
schema.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Using SqlCommandBuilder
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Now you'll
Chapter 17 -see how to
Building use the SqlCommandBuilder
Real-World Web Applications in an application. You can use the
OleDbCommandBuilder and OdbcCommandBuilder classes in the same way.
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
As usual, you create a connection to the database and use it to create the adapter object. The adapter is
Chapter 20 - COM
constructed with Interoperability
the initial queryand ADO.NET
for the Employees table as well as with the database connection.
Chapter 21 - Messaging
Next, you
Chapter 22 construct the and
- SQL Server CommandBuilder by passing
ADO.NET: Notes on the DataAdapter into its constructor. The act of
Performance
creating Athe
Appendix Databases: automatically
CommandBuilder
- Relational causes the UPDATE,INSERT, and DELETE commands to
Some Basic Concepts
be generated
Appendix for the adapter.
B - Commonly Used SQLThe following code creates two SqlCommandBuilder objects:
Statements
Appendix C - ADO.NET Frequently Asked Questions
Dim adapter As SqlDataAdapter = New SqlDataAdapter(SQL, conn)
Index
Dim builder1 As SqlCommandBuilder = New SqlCommandBuilder()
List of Figures
builder1.DataAdapter = adapter
List of Tables
Dim builder2 As SqlCommandBuilder = _
List of Listings
New SqlCommandBuilder(adapter)
List of Sidebars

Next, fill the DataSet using the adapter and create an instance of the Employees DataTable from the
DataSet:

Dim ds As DataSet = New DataSet("EmployeeSet")


adapter.Fill(ds, "Employees")

Now, insert a new DataRow into the DataTable in memory and populate a row with your desired values
using the DataTable'sAddNew method. After that you call the DataRowCollection.Add method to
add the row to the DataTable:
Applied ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
' Create a data table object and add a new row
Apress 2003 (928 pages)
Dim EmployeeTable As DataTable = ds.Tables("Employees")
This text provides extensive coverage of ADO.NET technology
Dim row As DataRow = EmployeeTable.NewRow()
including ADO.NET internals, namespaces, classes, and
row("FirstName") = takes
interfaces, and "Rodney"
a comprehensive look at XML
namespaces= and
row("LastName") classes, and how to integrate both with
"DangerField"
ADO.NET.
row("Title") = "Manager "
EmployeeTable.Rows.Add(row)
Table of Contents
Applied
Finally,ADO.NETBuilding Data-Driven
you call DataAdapter's Solutions
Update method to update the DataTable changes to the data source:
Introduction
Chapter 1 - ADO.NET Basics
// Update data adapter
Chapter 2 - Data Components in Visual Studio .NET
adapter.Update(ds, "Employees");
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Listing 54-34
Chapter shows the
- Handling full source
ADO.NET code for creating and using a CommandBuilder object.
Events
Chapter 6 - Integrating XML with ADO.NET
Listing 4-34:
Chapter 7
Creating and Using the SqlCommandBuilder Class
- Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
ChapterDim
9 -ConnectionString As String = "Integrated Security=SSPI;" & _
ADO.NET Exception Handling
"Initial
Chapter 10 - Working with Catalog=Northwind;Data
the ODBC .NET Data Provider Source=MCB;"
Dim conn
Chapter As SqlConnection
11 - Stored = New SqlConnection(ConnectionString)
Procedures and Views
ChapterDim
12 -SQL AsSQLXML,
Oracle, Stringand
= Other
"SELECT * FROM
.NET Data Employees"
Providers
Chapter'13
open a connection
- Developing a Custom Data Provider
conn.Open()
Chapter 14 - Developing Database Web Applications using ASP.NET
Dim adapter As SqlDataAdapter = New SqlDataAdapter(SQL, conn)
Chapter 15 - Using ADO.NET in Xml Web Services
Dim builder1 As SqlCommandBuilder = New SqlCommandBuilder()
Chapter 16 - ASP.NET Server Controls and Data Binding
builder1.DataAdapter = adapter
Chapter 17 - Building Real-World Web Applications
Dim builder2 As SqlCommandBuilder = _
Chapter 18 - Object-Relational Mapping in .NET
New SqlCommandBuilder(adapter)
Chapter'19 - Mapped
Create a Objects:
command Performance
builderConsiderations
object and Data Binding
ChapterDim20 -builder
COM Interoperability and ADO.NET
As SqlCommandBuilder = New SqlCommandBuilder(adapter)
Chapter'21 - Messaging
Create a dataset object
ChapterDim22 -dsSQLAsServer and ADO.NET:
DataSet = NewNotes on Performance
DataSet("EmployeeSet")
Appendix A - Relational Databases:
adapter.Fill(ds, "Employees") Some Basic Concepts
Appendix' BCreate
- Commonly Used table
a data SQL Statements
object and add a new row
AppendixDim C -EmployeeTable
ADO.NET FrequentlyAs DataTable
Asked Questions = ds.Tables("Employees")
Index Dim row As DataRow = EmployeeTable.NewRow()
row("FirstName") = "Bill"
List of Figures
row("LastName") = "Harvey"
List of Tables
row("Title") = "President"
List of Listings
EmployeeTable.Rows.Add(row)
List of Sidebars
' Update data adapter
adapter.Update(ds, "Employees")
' Dispose
conn.Close()
conn.Dispose()

As you can see from Listing 4-34, you didn't have to figure out how to create InsertCommand for the
Employees table because the CommandBuilder did it for you. All you had to do was add a row to the
DataSet and invoke an Update on the DataAdapter. You may argue that the InsertCommand is
automatically generated by VS .NET with the DataAdapter configurer, but the CommandBuilder works
with the SelectCommand you choose for the adapter, so you can change the SelectCommand on the fly
Applied ADO.NET:
and reuse the CommandBuilder atBuilding
runtime. Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
Note that you should call the
Apress 2003RefreshSchema
(928 pages) method of the CommandBuilder if the SelectCommand
of the associatedThis text provideschanges.
DataAdapter extensiveThe RefreshSchema
coverage rebuilds the other command structures
of ADO.NET technology
(InsertCommand, including ADO.NET internals,
DeleteCommand, namespaces, of
UpdateCommand) classes, and
the DataAdapter.
interfaces, and takes a comprehensive look at XML
namespaces and classes, and how to integrate both with
ADO.NET.

Table of Contents
Applied ADO.NETBuilding Data-Driven Solutions
Introduction
Chapter 1 - ADO.NET Basics
Chapter 2 - Data Components in Visual Studio .NET
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
Chapter 5 - Handling ADO.NET Events
Chapter 6 - Integrating XML with ADO.NET
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
Chapter 9 - ADO.NET Exception Handling
Chapter 10 - Working with the ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database Web Applications using ASP.NET
Chapter 15 - Using ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World Web Applications
Chapter 18 - Object-Relational Mapping in .NET
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Chapter 21 - Messaging
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
Appendix C - ADO.NET Frequently Asked Questions
Index
List of Figures
List of Tables
List of Listings
List of Sidebars
Staying within the
Applied Parameters
ADO.NET: Building Data-Driven Solutions
by Mahesh Chand and David Talbot ISBN:1590590732
ADO.NET wraps Apress
a classaround
2003 (928the parameters used for each column of the database. You can use the
pages)
parameters in conjunction with SelectCommand
This text provides extensive coverageto help you to select
of ADO.NET data for the DataSet. You also
technology
use it in conjunction with the other commands of the CommandDataSet
including ADO.NET internals, namespaces, classes, and (InsertCommand,
UpdateCommand, interfaces, and takes atocomprehensive
DeleteCommand) place data intolook
theatDataSet.
XML These are generated automatically
when you insert anamespaces and classes, component
OleDbDataAdapter and how to integrate
from the both with
Toolbox.
ADO.NET.
TheOleDbType describes the type information for the parameter. It consists of everything from strings to
Global Unique Identifiers (GUIDs).
Table of Contents
Applied
The SqlADO.NETBuilding
data provider hasData-Driven Solutions
a SqlDbType, and the Odbc data provider has an OdbcType. These type
Introduction
names and definitions differ, depending upon the provider you're using; for example, the Money type is the
Chapter
same in1 Odbc
- ADO.NET Basics
and Sql data providers, but it's called Currency in OleDb data providers.
Chapter 2 - Data Components in Visual Studio .NET
Not only
Chapter 3 does a parameter
- ADO.NET have a DbType
in Disconnected property, but a parameter has a Direction (input, output),
Environments
Size, and
Chapter 4 - even a Value.
ADO.NET Table 4-14
in Connected describes the SqlParameter properties.
Environments
Chapter 5 - Handling ADO.NET Events
Table 4-14:
Chapter 6
The SqlParameter Class Properties
- Integrating XML with ADO.NET
Chapter 7 - Data BindingDESCRIPTION
PROPERTY and Windows Forms Data-Bound Controls
Chapter 8 - Constraints and Data Relations
DbType
Chapter Represents
9 - ADO.NET Exception the DbType of the parameter.
Handling
Chapter 10 - Working with
Direction the ODBC .NET
Represents Data Provider
the direction of a parameter. A parameter can be input-only,
Chapter 11 - Stored Procedures and Views
output-only, bi-directional, or a stored procedure.
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
IsNullable Represents whether a parameter accepts null values.
Chapter 13 - Developing a Custom Data Provider
Chapter 14 - Developing Database
OleDbType Web
Represents Applications
the OleDbType using ASP.NET
of the parameter.
Chapter 15 - Using ADO.NET in Xml Web Services
Offset Represents the offset to the Value property.
Chapter 16 - ASP.NET Server Controls and Data Binding
Chapter 17 - Building Real-World
ParameterName Web the
Represents Applications
name of the parameter.
Chapter 18 - Object-Relational Mapping in .NET
Precision Represents the maximum number of digits used to represent the Value
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
property.
Chapter 20 - COM Interoperability and ADO.NET
Scale
Chapter 21 - Messaging Represents the decimal places to which Value is resolved.
Chapter 22 - SQL Server and ADO.NET: Notes on Performance
Size Represents the maximum size in bytes a column can store.
Appendix A - Relational Databases: Some Basic Concepts
SourceColumn
Appendix Represents
B - Commonly Used the source column mapped to the DataSet.
SQL Statements
Appendix C - ADO.NET Frequently
SourceVersion Asked
Represents theQuestions
DataRow version.
Index
ListSqlDbType
of Figures Represents the SqlDbType of the parameter
ListValue
of Tables Represents the value of the parameter.
List of Listings
List of Sidebars
Now you're going to see the construction of parameters using the Integrated Development Environment
(IDE) as well as manually. Listing 4-35 shows the construction of an OleDbParameter generated by the
IDE for the Northwind database. All commands have a collection of parameters; in this example, the
parameter ContactName is being added to a command used for deleting from the database.

Listing 4-35: Creating a Parameter

Me.oleDbDeleteCommand2.Parameters.Add(New
System.Data.OleDb.OleDbParameter("ContactName",
System.Data.OleDb.OleDbType.Char, 30,
System.Data.ParameterDirection.Input, False,
((System.Byte)(0)), ((System.Byte)(0)),
Applied
"ContactName", ADO.NET: Building Data-Driven Solutions
System.Data.DataRowVersion.Original,
Nothing)) by Mahesh Chand and David Talbot ISBN:1590590732
Apress 2003 (928 pages)
This text provides extensive coverage of ADO.NET technology
including ADO.NET internals, namespaces, classes, and
interfaces, and takes a comprehensive look at XML
Luckily, the IDE automatically generates the parameters for you; as you can see, this is a lot of code to
namespaces and classes, and how to integrate both with
write for just one ADO.NET.
parameter. Imagine if you had to manually deal with a database table of 50 parameters!

You need to create and add parameters to the command for each parameter reference that appears in
the SQL
Table command. If the SQL command only describes a single row insert or update, then you don't have
of Contents
parameters.
Applied But, more often
ADO.NETBuilding than not,Solutions
Data-Driven when you're using DataSets, DataTables, and DataRows, you'll
need parameters because these in-memory structures operate on several rows.
Introduction
Chapter 1 - ADO.NET Basics
Parameters appear in a SQL Server INSERT command proceeded by an @ sign, as shown in the
Chapter 2 - Data Components in Visual Studio .NET
following code:
Chapter 3 - ADO.NET in Disconnected Environments
Chapter 4 - ADO.NET in Connected Environments
sqlInsertCommand1.CommandText =
Chapter 5 - Handling ADO.NET Events
@"INSERT INTO Customers(CustomerID, CompanyName, ContactName)"+
Chapter 6 - Integrating XML with ADO.NET
" VALUES (@CustomerID, @CompanyName, @ContactName)"
Chapter 7 - Data Binding and Windows Forms Data-Bound Controls
In OleDb, parameters appear as question marks such as:
Chapter 8 - Constraints and Data Relations
oleDbInsertCommand2.CommandText =
Chapter
"INSERT9 -INTO
ADO.NET Exception Handling
Customers(Address, City, CompanyName, ContactName)"+
Chapter 10 - Working
" VALUES (?, ?,with
?,the
?)"ODBC .NET Data Provider
Chapter 11 - Stored Procedures and Views
Chapter 12 - Oracle, SQLXML, and Other .NET Data Providers
To add13
Chapter the- parameter
Developing @CustomerID to the InsertCommand of the SqlDataAdapter, simply call Add
a Custom Data Provider
on the command's
Chapter ParameterCollection.
14 - Developing This
Database Web Applications willASP.NET
using return a parameter in which you can further assign
properties,
Chapter 15 - such
Usingas:
ADO.NET in Xml Web Services
Chapter 16 - ASP.NET Server Controls and Data Binding
SqlParameter
Chapter 17 - BuildingworkParam = Applications
Real-World Web theSqlServerAdapter.InsertCommand.
Parameters.Add("@CustomerID",
Chapter 18 - Object-Relational Mapping in .NET SqlDbType.Int)
Chapter 19 - Mapped Objects: Performance Considerations and Data Binding
Chapter 20 - COM Interoperability and ADO.NET
Two other crucial properties are the name of the column that the parameter is mapping to and the
Chapter 21 - Messaging
RowVersion. Typically, it's good to give the parameter the same name as the column of the database to
Chapter 22 confusion:
avoid the - SQL Server and ADO.NET: Notes on Performance
Appendix A - Relational Databases: Some Basic Concepts
Appendix B - Commonly Used SQL Statements
workParam.SourceColumn = "CustomerID"
Appendix C - ADO.NET Frequently Asked Questions
workParam.SourceVersion = DataRowVersion.Original
Index
List of Figures
The<