Vous êtes sur la page 1sur 35

Database Applications

Customer Management

Networks

CUI client
File Edit

Name: John Doe Address: 101 Dalmation Street Salary: $43,000 Age: 34 PF1 - Help PF2 - Add PF3 Enter details and press PF3 to update Update

Database Server

Customer Management Window Help John Doe 101 Dalmation St. $48,000 34 Update Cancel

LAN / WAN

GUI client

Name: Address: Salary: Age: Loan History

Disks & Files


Application Server

Browser client

Interne / Intrane t

Web Server

Sonata Software Limited

ASP.NET and ADO.NET - 1

DataReader: Direct Access to Database


Select Stored procedure

Client Application

Read
DataReader

Command

Windows Form
Connection

Write
Command

Insert Update Delete Stored procedure

Web Form

Database
Sonata Software Limited

ASP.NET and ADO.NET - 2

Dim dr As OleDbDataReader dr = catCMD.ExecuteReader()


Initial position of cursor

Cursor of a "Record Set"


Column index 0 Num 100 1 Name Java 2 Price 200.00 Column name

Current cursor position dr.read() moves the cursor to the next row and return True if the next row exists.

200 300 400

HTML 250.00 C XML 300.00 240.00

If current cursor position is here, then the execution of dr.read() returns False.

Record set

Sonata Software Limited

ASP.NET and ADO.NET - 3

Accessing Column Values


By Zero-based Column
Type Safe or Ordinal Reference

By Column Name String Reference dr("Num") dr("Name") dr("Price")

Returned Value 200 HTML 250.00

dr.getInt32(0) or dr(0) dr.getString(1) or dr(1) dr.getDecimal(2) or dr(2)

Accessing the value of a column from the current record in the record set, e.g., dr, you can use either dr(i) where i is an ordinal reference (0, 1, 2, ...) or dr("ColumnName"). It will return an object and when it is used for output, it is implicitly converted to string data type. If you want to use Type Safe Reference, you can use dr.GetXXX(i) where i is an ordinal reference (0, 1, 2, ...) and XXX is the appropriate data type of the column such as dr.GetDecimal(2). Type Safe Reference is more convenient when you want to format the retrieved data or to use it in a calculation. You cannot use column names with Type Safe Reference.

dr.getDecimal("Price") is illegal!

Sonata Software Limited

ASP.NET and ADO.NET - 4

Categories Table in NorthWind Access Database

Sonata Software Limited

ASP.NET and ADO.NET - 5

CategoryList.aspx

Sonata Software Limited

ASP.NET and ADO.NET - 6

Using DataReader

Dynamically generated web page 2 SQL statement

Format result

1 5

set rows in HTML format

ASP.NET Code
DataReader
Record set
100

NorthWind
Category table

100

Retrieve result set,

one row at a time.


4 3 ASP.NET and ADO.NET - 7

Sonata Software Limited

CategoryList.aspx
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data.OleDB" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> Protected Sub Page_Load (ByVal sender As Object, ByVal e As System.EventArgs) Dim conn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader conn = New _ OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Northwind.mdb")

Sonata Software Limited

ASP.NET and ADO.NET - 8

CategoryList.aspx - continued
If Not IsPostBack Then Try conn.Open() cmd = New OleDbCommand("select * from categories", conn)
cmd.CommandType = CommandType.Text
' require Namespace="System.Data"

dr = cmd.ExecuteReader() While dr.Read() Label1.Text &= dr.GetInt32(0) & "--" _ & dr.GetString(1) & "--" _ & dr.GetString(2) & "<br>" End While Catch ex As Exception Label1.Text = "Database error!" & "<br>" & ex.Message Finally dr.Close() conn.Close() End Try End If End Sub </script> ASP.NET and ADO.NET Sonata Software Limited

CategoryList.aspx - continued
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ADO.NET Example - 1</title> </head> <body> <H1>List of categories</H1> <form id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server"></asp:Label> </div> </form> </body> </html>

Sonata Software Limited

ASP.NET and ADO.NET - 10

Commonly Used DataReader Properties and Methods

Sonata Software Limited

ASP.NET and ADO.NET - 11

Using Code-Behind

Sonata Software Limited

ASP.NET and ADO.NET - 12

CategoryList2.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CategoryList2.aspx.vb" Inherits="CategoryList2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <H1>List of categories</H1> <form id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server"></asp:Label> </div> </form> </body> </html>

Sonata Software Limited

ASP.NET and ADO.NET - 13

CategoryList2.aspx.vb
Imports System.Data.OleDB Imports System.Data Partial Class CategoryList2 Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim conn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader conn = New _ OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Northwind.mdb")) If Not IsPostBack Then Try conn.Open() cmd = New OleDbCommand("select * from categories", conn) cmd.CommandType = CommandType.Text ' require Namespace="System.Data" dr = cmd.ExecuteReader() While dr.Read() Label1.Text &= dr.GetInt32(0) & "--" _ & dr.GetString(1) & "--" _ & dr.GetString(2) & "<br>" End While Catch ex As Exception Label1.Text = "Database error!" & "<br>" & ex.Message Finally conn.Close() End Try End If

End Sub End Class ASP.NET and ADO.NET - 14

Sonata Software Limited

CategoryList3.aspx

Sonata Software Limited

ASP.NET and ADO.NET - 15

Using DataReader
<form action="xxx.aspx" method="post"> (from a static HTML page)
<a href="xxx.aspx?cid=7&name=Produce"> Product</a>
Invocation of ASP.NET programs
1 Dynamically generated web page 5
Format result

2 SQL statement

set rows in HTML format

ASP.NET Code
DataReader
Record set
100

NorthWind
Category table

100

Retrieve result set,

one row at a time.


4 3 ASP.NET and ADO.NET - 16

Sonata Software Limited

Generated HTML Source Code


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Category Listing</TITLE></HEAD><BODY> <H1>Category List</H1>

<table border=4> <tr> <th>ID</th> <th>ID</th> </tr> <tr> <td>1</td> <td>Beverages</td> </tr> <tr> <td>2</td> <td>Condiments</td> </tr> <tr> <td>3</td> <td>Confections</td> </tr> . </table>
Sonata Software Limited

</BODY> </HTML>

ASP.NET and ADO.NET - 17

CategoryList3.aspx
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data.OleDB" %> <%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Dim table As System.Text.StringBuilder table = New System.Text.StringBuilder("")


If Not IsPostBack Then
Dim conn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader

conn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Northwind.mdb"))

Try conn.Open() cmd = New OleDbCommand("select * from categories", conn) cmd.CommandType = CommandType.Text dr = cmd.ExecuteReader()

table.Append("<table><table border='1'><tr>") table.Append("<th>ID</th><th>Name</th></tr>") While dr.Read() table.Append("<tr><td>&nbsp;" & dr.GetInt32(0)) table.Append("</td><td>&nbsp;" & dr.GetString(1) & "</td></tr>") End While

Sonata Software Limited

ASP.NET and ADO.NET - 18

CategoryList3.aspx - Continued
Catch ex As Exception Label1.Text = "Database error!" & "<br>" & ex.Message Finally

conn.Close() table.Append("</table>") Label1.Text = table.ToString()


End Try End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Category List</title> </head> <body> <H1>Category List</H1> <form id="form1" runat="server"> <div>
<asp:Label ID="Label1" runat="server"></asp:Label>

</div> </form> </body> </html>


Sonata Software Limited

ASP.NET and ADO.NET - 19

The Data Model of the NorthWind Database

Sonata Software Limited

ASP.NET and ADO.NET - 20

Drill Down Data Navigation

http://localhost:3459/tutor/ProductBycategory.aspx?cid=1&cname=Beverages ASP.NET and ADO.NET - 21 Sonata Software Limited

CategoryList4.aspx
http://localhost:3459/tutor/ProductBycategory.aspx?cid=1&cname=Beverages
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data.OleDB" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim nwindConn As OleDbConnection Dim catCMD As OleDbCommand Dim myReader As OleDbDataReader nwindConn = _ New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Northwind.mdb")) Try catCMD = nwindConn.CreateCommand() catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories" nwindConn.Open() myReader = catCMD.ExecuteReader() Label1.Text &= "<table border=4><tr><th>ID</th><th>Name</th></tr>" Do While myReader.Read() Label1.Text &= "<tr><td>" & myReader("CategoryID") & _ "</td><td> <a href='ProductsBycategory.aspx?cid=" & _ myReader("CategoryID") & "&cname=" & _ Server.UrlEncode(myReader("CategoryName")) & "'>" & _ myReader("CategoryName") & "</a> </td></tr>" Loop ASP.NET and ADO.NET - 22 Sonata Software Limited

CategoryList4.aspx - Continued
Catch ex As Exception Label1.Text = "Database error!" & "<br>" & ex.Message Finally nwindConn.Close() nwindConn = Nothing Label1.Text &= "</table>" End Try End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Category List</title> </head> <body> <H1>Category List</H1> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html>
Sonata Software Limited

ASP.NET and ADO.NET - 23

ProductsByCategory.aspx
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data.OleDB" %> <%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim conn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader

LabelTitle.Text = Products from & Request.QueryString("cname")


conn = _ New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Northwind.mdb"))

Try cmd = conn.CreateCommand()


cmd.CommandText = "SELECT ProductID, ProductName, UnitPrice FROM Products" & _

" Where CategoryID=" & Request.QueryString("cid") conn.Open() dr = cmd.ExecuteReader() Label1.Text &= "<table border=4><tr><th>ID</th><th>Name</th><th>Price</th></tr>" Do While dr.Read() Label1.Text &= "<tr><td>" & dr("ProductID") & _ "</td><td> <a href='ProductDetail.aspx?pid=" & _

dr("ProductID") & "'>" & _


dr("ProductName") & "</a> </td><td align='right'>" & _ Format(dr("UnitPrice"), "$#,##0.00") & "</td></tr>"

Loop
ASP.NET and ADO.NET - 24

Sonata Software Limited

ProductsByCategory.aspx - Continued
Catch ex As Exception Label1.Text = "Database error!" & "<br>" & ex.Message Finally conn.Close() conn = Nothing Label1.Text &= "</table>" End Try End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Category List</title> </head> <body> <H1>Products from

<asp:Label ID="LabelTitle" runat="server" Text="Label"></asp:Label>


</H1> <form id="form1" runat="server"> <div>

<asp:Label ID="Label1" runat="server"></asp:Label>


</div> </form> </body> </html>
Sonata Software Limited

ASP.NET and ADO.NET - 25

Generating HTML Dynamically


<tr><td> 1 </td><td> Beverages </td></tr>

Separate static text from dynamic ones

"<tr><td>" &

& "</td><td>" &

Beverages

& "</td></tr>"

Replace dynamic data with actual DataReader methods

"<tr><td>" & dr(0) & "</td><td>" &

dr(1)

& "</td></tr>"

Sonata Software Limited

ASP.NET and ADO.NET - 26

<tr><td> 1 </td><td> <a href='ProductsByCategory.aspx?cid=1&cname= Beverages'>Beverages</a></td></tr> "<tr><td>" & 1 & "</td><td><a href='ProductsByCategory.aspx?cid=" & 1 & "&cname=" & Beverages & "'>" & Beverages & "</a></td></tr>" "<tr><td>" & dr(0) & "</td><td><a href='ProductsByCategory.aspx?cid=" & dr(0) & "&cname=" & dr(1) & "'>" & dr(1) & "</a></td></tr>"

Label1.Text &= "<tr><td>" & myReader("CategoryID") & _ "</td><td> <a href='ProductsBycategory.aspx?cid=" & _ myReader("CategoryID") & "&cname=" & _ Server.UrlEncode(myReader("CategoryName")) & "'>" & _ myReader("CategoryName") & "</a> </td></tr>"

Sonata Software Limited

ASP.NET and ADO.NET - 27

Design View

Sonata Software Limited

ASP.NET and ADO.NET - 28

ProductDetail.aspx
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data.OleDB" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim nwindConn As OleDbConnection = _ New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Northwind.mdb")) Dim prodCMD As OleDbCommand = nwindConn.CreateCommand()

Sonata Software Limited

ASP.NET and ADO.NET - 29

SQL Statement - Join


SELECT ProductID, ProductName, UnitPrice, Products.CategoryID, CategoryName, Discontinued FROM Products, Categories Where ProductID=11 AND Products.CategoryID = Categories.categoryID

Sonata Software Limited

ASP.NET and ADO.NET - 30

prodCMD.CommandText = _

"SELECT ProductID, ProductName, UnitPrice, Products.CategoryID as x, " & _ " CategoryName, Discontinued FROM Products, Categories" & _

" Where ProductID=" & Request.QueryString("pid") & _ " AND Products.CategoryID = Categories.categoryID "
Dim dr As OleDbDataReader Try

nwindConn.Open() dr = prodCMD.ExecuteReader() If dr.Read() Then LabelProductID.Text = Request.Params("pid")


LabelProductName.Text = dr("ProductName") LabelCategoryID.Text = dr("CategoryID") LabelCategoryName.Text = dr("CategoryName") LabelUnitPrice.Text = dr("UnitPrice")

continued

CheckBoxDiscontinued.Checked = dr("Discontinued") HyperLinkProductList.NavigateUrl = _ "ProductsByCategory.aspx?cid=" _ & dr("CategoryID") & cname= & dr(CategoryName)
Else LabelErrorMsg.Text = "No product found!" End If Catch ex As Exception LabelErrorMsg.Text = "Database error!" & "<br>" & ex.Message Finally nwindConn.Close() End Try End Sub ASP.NET and ADO.NET - 31 Sonata Software Limited </script>

Continued
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Product Detail</title> </head> <body> <form id="form1" runat="server"> <div> <P><STRONG><FONT size="4">Product Detailed Information<BR> </FONT></STRONG>

<asp:label id="LabelErrorMsg" runat="server"></asp:label><BR> Product ID: <asp:label id="LabelProductID" runat="server"></asp:label><BR> Product Name: <asp:label id="LabelProductName" runat="server"></asp:label><BR> Category ID: <asp:label id="LabelCategoryID" runat="server"></asp:label><BR> Category Name: <asp:label id="LabelCategoryName" runat="server"></asp:label><BR> Unit Price: <asp:label id="LabelUnitPrice" runat="server"></asp:label><BR> <asp:checkbox id="CheckBoxDiscontinued" runat="server" Text="Discontinued"></asp:checkbox></P><P>

<asp:hyperlink id="HyperLinkProductList" runat="server"> Back to product list of the same Category</asp:hyperlink><BR>

<a href="CategoryList4.aspx">Show all product category. </A>


</div> </form> </body> </html>

Sonata Software Limited

ASP.NET and ADO.NET - 32

Sonata Software Limited

ASP.NET and ADO.NET - 33

Domain Name: 63642.hostmyapplications.com Technical Contact Username: aspne3mz Technical Contact Password: xxxxxxx URL used to access your site: http://63642.hostmyapplications.com

Sonata Software Limited

ASP.NET and ADO.NET - 34

Highlight Source (Local) Web Site files Click the right pointing arrow to upload

URL used to access your page at: http://63642.hostmyapplications.com/CategoryList.aspx


Sonata Software Limited

ASP.NET and ADO.NET - 35

Vous aimerez peut-être aussi