- What Is a Database?
- Getting Data Out of Databases
- Kinds of Databases
- ODBC
- Database Structure
- Using ADO.NET
- Adding Rows to Database Tables Using ADO.NET
- Building the Façade Classes
- Making the ADO.NET Façade
- Creating Classes for Each Table
- Building the Price Table
- Loading the Database Tables
- The Final Application
Building the Façade Classes
This description is the beginning of the new Façade we are developing to handle creating, connecting to, and using databases. In order to carry out the rest, let's consider Table 18-4, grocery prices at three local stores.
It would be nice if we had this information in a database so we could easily answer the question, "Which store has the lowest prices for oranges?" Such a database should contain three tables: the supermarkets, the foods, and the prices. We also need to keep the relations among the three tables. One simple way to handle this is to create a Stores table with StoreName and StoreKey, a Foods table with a FoodName and a FoodKey, and a Price table with a PriceKey, a Price, and references to the StoreKey and Foodkey.
In our Façade, we will make each of these three tables its own class and have it take care of creating the actual tables. Since these three tables are so similar, we'll derive them all from the basic DBTable class.
Building the Price Query
For every food name, we'd like to get a report of which stores have the cheapest prices. This means writing a simple SQL query against the database. We can do
Table 18-4. Grocery Pricing Data
Store |
Product |
Price |
Stop and Shop |
Apples |
0.27 |
Stop and Shop |
Oranges |
0.36 |
Stop and Shop |
Hamburger |
1.98 |
Stop and Shop |
Butter |
2.39 |
Stop and Shop |
Milk |
1.98 |
Stop and Shop |
Cola |
2.65 |
Stop and Shop |
Green beans |
2.29 |
Village Market |
Apples |
0.29 |
Village Market |
Oranges |
0.29 |
Village Market |
Hamburger |
2.45 |
Village Market |
Butter |
2.99 |
Village Market |
Milk |
1.79 |
Village Market |
Cola |
3.79 |
Village Market |
Green beans |
2.19 |
Waldbaum's |
Apples |
0.33 |
Waldbaum's |
Oranges |
0.47 |
Waldbaum's |
Hamburger |
2.29 |
Waldbaum's |
Butter |
3.29 |
Waldbaum's |
Milk |
1.89 |
Waldbaum's |
Cola |
2.99 |
Waldbaum's |
Green beans |
1.99 |
this within the Price class and have it return a Dataset with the store names and prices.
The final application simply fills one list box with the food names and fills the other list box with prices when you click on a food name, as shown in Figure 18-1.
Figure 18-1. The grocery program using a Façade pattern