Datasheet
Now when you look at the WHERE clause section you see two tables joined together as in Figure 8-17:
Figure 8-17
The
WHERE Clause Builder can also be used to filter data so that only selected rows are shown; we'll look
at that later. For now though, let's look at the code the wizard created for us (it may look slightly
different in your page – we've wrapped it so it's easier to read):
System.Data.DataSet GetProductsDataSet() {
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Ole DB Services=-4; Data Source=C:\\BegASPNET11\\" +
"data\\Northwind.mdb";
System.Data.IDbConnection dbConnection =
new System.Data.OleDb.OleDbConnection(connectionString);
string queryString = "SELECT [Products].[ProductName], " +
"[Categories].[CategoryName] FROM [Products], [Categories] " +
"WHERE ([Categories].[CategoryID] = [Products].[CategoryID])";
System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDbDataAdapter dataAdapter =
new System.Data.OleDb.OleDbDataAdapter();
dataAdapter.SelectCommand = dbCommand;
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
Let's tackle this systematically. First, we have the function declaration:
System.Data.DataSet GetProductsDataSet() {
This is defined as type System.Data.DataSet, which means it's going to return a DataSet (we'll look
at this in detail in the next chapter). You'll notice that the declaration has the
System.Data namespace
before it. This is done because, while declaring variables or functions, ASP.NET needs to know where the
type is stored.
Normally we use the
<%@ import Namespace="..." %> page directive to indicate the namespaces being
used in a page, and thus we don't have to specify the namespace when declaring variables. The wizard
isn't sure what namespaces have been set at the top of the page, so it includes the full namespace
just-in-case, ensuring that the code will compile under all situations.
267
Reading from Databases
57084_08.qxp 30/01/2004 8:03 PM Page 267