
- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary

924 Chapter 20 DATABASES: ARCHITECTURE AND BASIC CONCEPTS
We’ll use this stored procedure in the following chapter in a short application that demonstrates how to execute a stored procedure from within your VB code and how to get the results back.
Summary
It’s been a long chapter but certainly interesting. You’ve learned how data are stored in databases, how to break the information into smaller pieces and store it into tables, and how relationships between tables allow you to quickly locate the information you’re interested in.
All actions against databases are performed with SQL statements and stored procedure. SQL is a universal language for accessing data in databases. SQL is not a traditional language, in that it describes the actions you want to perform to the database but not how these actions will be carried out. ADO.NET is based on SQL (SQL statements and stored procedures), and all the information in this chapter will be used in the following chapters to build database applications.
So far, you’ve learned how to extract data from a database. In the following chapter, you’ll learn how to move this information from the server (the computer running SQL Server) to the client (the machine on which your application is running), how to store the information and process it on the client, and how to update the database.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

Chapter 21
Building Database Applications with ADO.NET
In this chapter, we’re going to explore the basics of database applications. The database applications you build with VB.NET are client-server applications. The data resides in a database, which is installed on one of the computers on the network. To test the examples of this section, you will most likely use a copy of SQL Server installed on the same machine you use for development. In an actual production environment, your application will be installed on many clients, and all of the clients will be accessing the same database, installed on the server. The server knows how to access the data very efficiently, and that’s all it does. Presenting the data to the user and/or processing the data is your application’s responsibility. The server is the machine on which SQL Server is running. The machines on which the application is running are the clients.
The client-server model is a very efficient one because it allows you to share the computational load among multiple computers and each computer does what it can do best. The client computer gets data, presents them to the user, optionally processes them, and sends them back to the server. The server can focus on moving data out of and into the database. The type of client described here is called rich client, because it can use advanced controls to present the data to the user. It’s a workstation running Windows applications, which can process the data in many ways. For example, it can convert the data read from the database into elaborate graphical representations. There’s absolutely no reason to pass the task of processing the data to the server.
The client-server architecture a two-tier architecture. The programs running on the client make up the presentation tier; the database server is the data tier. Of course, you’ve all read that ADO.NET is a great tool for three-tier, or multitier, applications. So, what’s the third tier? Let’s consider for a moment an application that runs on the Web—an online store, for example. The client is the browser. This is the program that runs on each client and interacts with the user. Unlike a Windows application, the browser can’t deploy an elaborate user interface (you can’t place any of the Windows controls on a Web page); you’re limited to the HTML controls. This type of client is called thin client, because it has limited processing capabilities. The browser must receive HTML pages and render them on the monitor. The HTML page may contain scripts too, but even so you can’t duplicate the functionality of a Windows form on a Web page. In other words, you can’t pull data from the database, download them to the client, and process
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

926 Chapter 21 BUILDING DATABASE APPLICATIONS WITH ADO.NET
them there. If you want to convert the numeric data retrieved from the database into charts, you must create the pages with the graphs and download them to the client, where they can be rendered by the browser.
The processing of the data takes place on the Web server. The clients on the Web don’t see the database server directly. Instead, they contact a program running on the Web server, which in turns contacts the database to retrieve the requested data, format them in a way the browser can understand them, and then send the HTML page to the client. The programs running on the Web server that service the requests made by the clients form the third tier, and a Web application is the most familiar example of the three-tier architecture. The three-tier architecture is the evolution of the client-server model.
You can also introduce additional tiers to your applications. Let’s say your application records orders made over the Web. When the user places an order, the application running on the Web server may have to contact the server of a shipping company to get a quote on the shipping cost and add it to the total cost of the order. The programs on the shipper’s server form yet another tier of the application.
What makes VB.NET a great tool for multitier applications is that it uses XML to pass data between layers. XML is a text-based protocol that can describe any type of data, even images. By using the XML format, data are moved easily and reliably between layers, even between different operating systems and databases. Since you’re probably wondering about XML, what it can do for your applications and, especially, how well you must understand XML, let me simplify the picture a little. You can write database applications without ever seeing a line of XML code. ADO.NET uses XML to format the data and move them between the database and the client application (or any of the other tiers of the application). As you work with the objects of ADO.NET, you access data in their native format. XML is totally transparent to you.
The Architecture of ADO.NET
You already know how to retrieve data from a database with SQL statements and stored procedures. Now you’ll learn where the data are stored on the client computer and how to process them. We’ll get to the first few examples of database applications shortly, but first I would like to overview the architecture of ADO.NET. Once you have a good idea of the big picture and the motivation behind the design of ADO.NET, you’ll be able to better understand the objects of ADO.NET and how to use them.
ADO.NET is considered to be the evolution of ADO, but it doesn’t even resemble ADO. ADO was designed on the assumption that the client could maintain a connection to the database. When the Web wasn’t an issue, you could set up a connection to the database and request data over this connection, or update the database through the same connection. The client application was connected to the database at all times, and it could access any table at any time. In fact, this is how most database applications that run on local area networks are written today.
As programmers were looking for ways to access their databases over the Web, they realized that they couldn’t write applications that maintained their own connection to the database. They still used ADO, but they were using ADO in a disconnected mode: they wrote code to read data from the database, move them to the client, and close the connection to the database. The processing of the data took place on the client. To update the database, programmers had to establish a new connection, send
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

THE ARCHITECTURE OF ADO.NET 927
the modified data back to the server to update the tables of the database, and then close the connection again. Microsoft kept adding features to ADO to enhance the support of disconnected sets of data.
Clearly, there was a need for a different programming paradigm, one that would completely decouple the server from the client. This new paradigm was implemented with ADO.NET, which uses a disconnected architecture. The client application requests the data, which are downloaded to the client computer and stored locally. The client is your application that uses a database over the company’s network, or a Web application that connects to a Web server through any connection, from dial-up to T1. Even if you have a fast connection, ADO.NET doesn’t allow you to maintain a connection to the database and access it directly. Instead, you’re required to retrieve the data you need to the client and work with them there.
You know how to request the data you’re interested in. All you need is a structure that will hold the data. This structure is an object called a DataSet. The DataSet is a cache for your data, and it looks just like the database. You can edit the data in the DataSet, add new data, even combine the data you retrieved from your database with data from another database. It is possible (but certainly not recommended) to download all the tables to the client and work with a complete copy of the database. Everything you do the DataSet is local to the client and doesn’t affect the tables in the database. When you’re ready to update the underlying tables in the database, you establish a new connection, send the modified data, and close the connection again.
The DataSet is at the core of ADO.NET, because everything takes place in the DataSet. With ADO, there was a similar object, the Recordset object, where you could store a table or the result of a query and process it locally. The DataSet is far more than a Recordset: it’s a miniature database, made up of tables and relations between them. Let’s say you want to work with the invoices issued last month. The data you need are some rows of the Customers table (the customers that placed one or more orders in this period), some rows of the Orders table (the orders placed in the same period), and some rows of the Order Details table (the details that correspond to the
selected orders). Chances are you don’t need all the columns of these tables either. So you can specify, with SQL statements or stored procedures, the data you need and bring them into a DataSet on the client. The data will end up in three different tables in the DataSet, and you can establish relations between tables. In essence, you’re working with a subset of the database. You can review the data, edit them, do anything you would do if you were working directly against the database. The DataSet can impose the same constraints and enforce referential integrity between its tables. After you’re done processing the data, the DataSet knows how to move the changes back to the data- base—after all, they have the same structure.
While the data is in the DataSet, other users can add new rows to the tables of the database, even edit some of the rows you have copied into the DataSet. Unfortunately, there’s no way for you to know in advance that one of the customer rows in a local DataSet is no longer the same as the original row in the database. You will only find out when you attempt to update the database. As a result, this architecture is not ideal for applications like flight reservations. If you need this type of immediate access to the database, you should request data as you need them and update the database immediately. These types of applications are quite uncommon. ADO.NET is a great architecture for typical applications most of us are faced with on a daily basis, and it’s especially well suited for the Web. However, the current version of ADO.NET doesn’t address all the issues. It’s very likely that the next version of ADO.NET will also support connected DataSets. If not, there will be applications that ADO.NET can’t handle.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |