ADO.NET contains two Data providers: ■ SQL Data provider ■ OLEDB Data provider If your backend database is SQL Server, you should use the SQL Data provider as it talks natively using TDS
Trang 1Data Providers
In ADO, communication with the data source is through the OLE DB providers.
In ADO.NET, the communication is through Data providers ADO.NET contains two Data providers:
■ SQL Data provider
■ OLEDB Data provider
If your backend database is SQL Server, you should use the SQL Data provider
as it talks natively (using TDS) to SQL server.This results in immediate perfor-mance gains, as there is no need to go through the OLE DB layer However, if you are not using SQL Server, you should use the OLEDB Data provider.
The OLEDB Data provider is engineered to work with most OLE DB providers.The providers listed in Table 9.3 have been tested and are known to work with ADO.NET.
Transaction Connection
Database
DataReader
SelectCommand InsertCommand UpdateCommand DeleteCommand
DataAdapter
Parameters Command
.NET Data Provider Data Set
XML
DataRelationCollection
DataTableCollection DataTable DataRowCollection DataColumnCollection ConstraintCollection
Trang 2Table 9.3 Supported OLE DB Providers
SQLOLEDB Microsoft OLE DB Provider for SQL Server MSDAORA Microsoft OLE DB Provider for Oracle Microsoft.Jet.OLEDB.4.0 OLE DB Provider for Microsoft Jet
It is possible to use the OLEDB Data provider even if you are using SQL server In this case, you are foregoing the benefits of talking directly to SQL server by going through additional layers by first going to the OLEDB Data provider and then going through the OLEDB provider.
Figure 9.48 summarizes the discussion so far.
Application
OLE Provider
DataReader DataSet
OLEDB Data Provider
SQL Data Provider
Non-SQL Server Databases
SQL Server Databases
Trang 3ADO.NET DataReader
A huge portion of our database access is on retrieving records and simply dis-playing them on the client side For this reason, ADO.NET provides the
DataReader.The DataReader is a read-only, forward-only stream returned from the database In order to prevent storing a huge number of records in memory (resulting from multiple users performing the data retrieval at the same time, typ-ical of Web access patterns), the DataReader stores only a single record in
memory at any one time.The example in Figure 9.49 illustrates the use of the DataReader.
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage"
Language="VB" %>
<%@ Register TagPrefix="Mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SQLClient" %>
<script language="vb" runat=server>
Sub Page_load (sender as Object, e as EventArgs) Dim connStr As String = "server=localhost; User ID=sa;
password=;database=Pubs"
Dim conn As New SQLConnection(connstr) Dim comm As New SQLCommand("SELECT * FROM Titles", conn) Dim dataReader As SQLDataReader
Try conn.Open() dataReader = comm.ExecuteReader ' -reads the record one by one and add to list -While datareader.Read
titles.Items.add (datareader("Title")) End While
Catch any_exception As Exception ' -catching any exception and displaying
Trang 4it -errorMessage.Text = "Error!"
Finally ' -close the connection and frees the datareader -conn.Close()
datareader = Nothing End Try
End Sub
</script>
<Mobile:Form runat="server" paginate="true">
<Mobile:Label runat="server" StyleReference="title" Text="Books in the Pubs table" />
<Mobile:List runat="server" id="titles"/>
<Mobile:Label runat="server" id="errorMessage"/>
</Mobile:Form>
When this code is run, the screen shown in Figure 9.50 will appear.
Dissecting the Codes The first thing to note when using ADO.NET is that we need to import two namespaces:
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SQLClient" %>
We next create three objects—conn, comm, and dataReader.The SQLConnection object is for making a connection to the database and the
Trang 5SQLCommand object is used for setting a command.The SQLDataReader object is used for reading records in a read-only, forward-only fashion.
Dim connStr As String = "server=localhost; User ID=sa;
password=;database=Pubs"
Dim conn As New SQLConnection(connstr) Dim comm As New SQLCommand("SELECT * FROM Titles", conn) Dim dataReader As SQLDataReader
We next have a Try, Catch, and Finally block:
Try
Catch any_exception As Exception
Finally
End Try Within the Try block, we open a connection to the database and execute the
command.To read the individual record, we use the Read() method of the
DataReader and then add it to the list control.
conn.Open() dataReader = comm.ExecuteReader ' -reads the record one by one and add to list -While datareader.Read
titles.Items.add (datareader("Title")) End While
Within the Catch block, we want to display any exception that occurs as a result of executing the codes in the Try block.
errorMessage.Text = "Error!"
And in the Finally block, we close the connection and free the DataReader
object:
' -close the connection and frees the datareader -conn.Close()
datareader = Nothing
Trang 6N OTE
Note that in VB.NET, the Set keyword is no longer in use Also, notice the ability to pass parameters to a class at the moment of instantiation.
ADO.NET Dataset From the last section you can see that the DataReader object provides a quick and easy way to retrieve records from a table In this section, we will look at the more powerful and flexible Dataset object.
Let’s take a look at the following example and see how we can use a Dataset object to access three different tables in the database.We will use the Pubs database that comes installed with Microsoft SQL Server 2000.We will specifi-cally use the following three tables:
■ Titles
■ Authors
■ TitleAuthor The relationship between the three tables is shown in Figure 9.51.
We want to print out all the titles stored in the Title table as well as the associated authors.
TitleAuthor
Trang 7We have the following code:
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage"
Language="VB" %>
<%@ Register TagPrefix="Mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SQLClient" %>
<script language="vb" runat=server>
Sub Page_load (sender as Object, e as EventArgs) ' -using
dataset -Dim connStr As String = "server=localhost; User ID=sa;
password=;database=Pubs"
Dim conn As New SQLConnection(connstr) Dim comm As New SQLCommand("SELECT * FROM Titles", conn) Dim sql As String = "SELECT * FROM TitleAuthor INNER JOIN Authors ON TitleAuthor.au_id=Authors.au_id"
Dim adapter as New SQLDataAdapter (comm) Dim ds As New DataSet("Pubs")
Dim titleToAdd as String Dim count as short
Try ' -using the dataset command to fill a table from a dataset -
adapter.Fill(ds, "Titles_table") ' -setting the dataset command to another Command object -comm.CommandText = sql
' -filling another table in the dataset -adapter.Fill(ds, "TitleAuthor_table")
' -set a relationship between the two tables -Dim titleID_titles_Column As DataColumn
Trang 8' -relationship between the titles and titleauthors tables-
titleID_titles_Column = ds.Tables("titles_table").Columns("title_id")
titleID_titleauthor_column = ds.Tables("titleauthor_table").Columns("title_id")
Dim TitleToAuthor As New DataRelation("TtoA", titleID_titles_Column, titleID_titleauthor_column)
Dim childrow As DataRow Dim row As DataRow
' -add a relation to the dataset -ds.Relations.Add(TitleToAuthor)
For Each row In ds.Tables("titles_table").Rows titleToAdd = row("title") & " by "
count = 0 For Each childrow In row.GetChildRows("TtoA") count += 1
if count>1 then titleToAdd += ", "
end if titleToAdd += childrow("au_fname") & " " &
childrow("au_lname")
Next titles.items.Add (titleToAdd) Next
Catch any_exception As Exception ' -catching any exception and displaying
it -errorMessage.Text = "Error!"
Finally ' -close the connection and frees the dataset -conn.Close()
ds = Nothing
Trang 9End Try
End Sub
</script>
<Mobile:Form runat="server" Paginate="true">
<Mobile:Label runat="server" StyleReference="title" Text="Titles and Authors" />
<Mobile:List runat="server" id="titles"/>
<Mobile:Label runat="server" id="errorMessage"/>
</Mobile:Form>
Now let’s take a closer look at the preceding code.We first create an SQLConnection object to connect to our database:
Dim connStr As String = "server=localhost; User ID=sa;
password=;database=Pubs"
Dim conn As New SQLConnection(connstr) Next we create a SQLCommand object:
Dim comm As New SQLCommand("SELECT * FROM Titles", conn) This SQLCommand object simply retrieves all the titles in the Title table.The
second SQL statement performs an INNER JOIN between the TitleAuthor table
and the Authors table.The joining field is “au_id”.This join is performed to retrieve the names of authors.
Dim sql As String = "SELECT * FROM TitleAuthor INNER JOIN Authors ON TitleAuthor.au_id=Authors.au_id"
We next create an SQLDataAdapter object for executing the command.The SqlDataAdapter serves as a bridge between a Dataset and SQL Server for
retrieving and saving data.We also create a Dataset object:
Dim adapter as New SQLDataAdapter (comm) Dim ds As New DataSet("Pubs")
Within the Try block, we first fill the Dataset’s Tables collection with records from the Titles table using the SQLDataAdapter object and named it Titles_table.
We next fill another table within the Tables collection and name it
TitleAuthor_table.This table contains records from the TitleAuthor table.
Trang 10' -using the dataset command to fill a table from a dataset - adapter.Fill(ds, "Titles_table")
' -setting the dataset command to another Command object -comm.CommandText = sql
' -filling another table in the dataset -adapter.Fill(ds, "TitleAuthor_table") Since there is a relationship between the Titles_table and TitleAuthor_table tables, we proceed to create a relationship between them.
' -set a relationship between the two tables -
Dim titleID_titles_Column As DataColumn Dim titleID_titleauthor_column As DataColumn
' -relationship between the titles and titleauthors tables -titleID_titles_Column = ds.Tables("titles_table").Columns("title_id") titleID_titleauthor_column =
ds.Tables("titleauthor_table").Columns("title_id")
Dim TitleToAuthor As New DataRelation("TtoA", titleID_titles_Column,
titleID_titleauthor_column) Figure 9.52 shows the relationship between the tables.
You can view the Titles_table as the parent and the TitleAuthor_table as a child Each title in Titles_table has some (or no) rows in TitleAuthor_table Using this parent-child relationship, we add the relationship into the dataset and then print out all the titles and authors name.
title_id
3 2
au_id
Title
title_id
1
type
B3 B4 B3 A2 A1
3 3 2 1 1
au_fname au_lname
TitleAuthor_table Titles_table