Datasheet

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.
Next we have the connection string that simply points to our existing database:
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Ole DB Services=-4; Data Source=C:\BegASPNET11\" & _
"data\Northwind.mdb"
Now we have the connection object:
Dim dbConnection As System.Data.IDbConnection = _
New System.Data.OleDb.OleDbConnection(connectionString)
One thing that's immediately obvious is that this example is using the IDbConnection and not the
OleDbConnection to define the connection to the database If this seems confusing, refer to the
discussion of interfaces in the previous chapter, where we talked about generic routines.
IDbConnection is an interface that defines what the Connection class must do, and since the wizard is
building a generic routine, it uses this interface. This is because the wizard allows you to connect to
different database types. – tThhis is on the first screen and is the same as the
Data Explorer allowing you
to pick either Access or SQL Server database. To make the wizard simpler, it uses the generic
interface as the type rather than having to use the type for a specific database.
The Interface simply enforces the correct signature on a class implementing the interface. There's no
actual requirement for the implementation to do anything. You could have a class that implements the
Open method but that actually does something else instead of opening a connection. It would be dumb,
but it could be done.
Next we have the SQL string, as built up by the wizard:
Dim queryString As String = "SELECT [Products].[ProductName], " & _
"[Categories].[CategoryName] FROM [Products], "[Categories] " & _
"WHERE ([Categories].[CategoryID] = [Products].[CategoryID])"
Now we have the definition of the command object. In previous examples we passed the command text
directly into the
OleDbDataAdapter. Underneath, ASP.NET actually creates another object – a Command
object. But you don't see that Command object, as it is used internally. The wizard creates the Command
object directly, by making use of the CommandText property to store the SQL command, and the
Connection property to store the database connection. As with the connection, which used the
interface as its type, the command is also defined as an interface type (
IDbCommand).
Dim dbCommand As System.Data.IDbCommand = _
New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Now we have the definition of the data adapter, and as with the connection, the type of the variable is
the interface type.
273
Reading From Databases
57076_Ch 8 SAN.qxd 01/12/2003 6:43 PM Page 273