Webmaster''''s Guide to the Wireless Internet part 45 pot

10 122 0
Webmaster''''s Guide to the Wireless Internet part 45 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

412 Chapter 9 • Microsoft Mobile Internet Toolkit Data 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. www.syngress.com Figure 9.47 ADO.NET Architecture Transaction Connection Database DataReader SelectCommand InsertCommand UpdateCommand DeleteCommand DataAdapter Parameters Command .NET Data Provider Data Set XML DataRelationCollection DataTableCollection DataTable DataRowCollection DataColumnCollection ConstraintCollection 159_wg_wi_09 10/22/01 5:29 PM Page 412 Microsoft Mobile Internet Toolkit • Chapter 9 413 Table 9.3 Supported OLE DB Providers Driver Provider 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 NOTE 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. www.syngress.com Figure 9.48 Comparing SQLData Provider and OLEDB Data Provider Application OLE Provider DataReader DataSet OLEDB Data Provider SQL Data Provider Non-SQL Server Databases SQL Server Databases 159_wg_wi_09 10/22/01 5:29 PM Page 413 414 Chapter 9 • Microsoft Mobile Internet Toolkit ADO.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. Figure 9.49 DataTitles.aspx <%@ 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 it www.syngress.com Continued 159_wg_wi_09 10/22/01 5:29 PM Page 414 Microsoft Mobile Internet Toolkit • Chapter 9 415 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 www.syngress.com Figure 9.49 Continued Figure 9.50 Accessing Records Using the DataReader Object 159_wg_wi_09 10/22/01 5:29 PM Page 415 416 Chapter 9 • Microsoft Mobile Internet Toolkit SQLCommand 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 www.syngress.com 159_wg_wi_09 10/22/01 5:29 PM Page 416 Microsoft Mobile Internet Toolkit • Chapter 9 417 NOTE 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. www.syngress.com Figure 9.51 Relationships between the Three Tables Authors Titles TitleAuthor 159_wg_wi_09 10/22/01 5:29 PM Page 417 418 Chapter 9 • Microsoft Mobile Internet Toolkit We 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 Dim titleID_titleauthor_column As DataColumn www.syngress.com 159_wg_wi_09 10/22/01 5:29 PM Page 418 Microsoft Mobile Internet Toolkit • Chapter 9 419 ' 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 www.syngress.com 159_wg_wi_09 10/22/01 5:29 PM Page 419 420 Chapter 9 • Microsoft Mobile Internet Toolkit End 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. www.syngress.com 159_wg_wi_09 10/22/01 5:29 PM Page 420 Microsoft Mobile Internet Toolkit • Chapter 9 421 ' 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. www.syngress.com Figure 9.52 Relationship between the Two Tables 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 159_wg_wi_09 10/22/01 5:29 PM Page 421 . 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. 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. 416 Microsoft Mobile Internet Toolkit • Chapter 9 417 NOTE 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

Ngày đăng: 04/07/2014, 02:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan