asp2 ado datareader

35
ASP.NET and ADO.NET - 1 Sonata Software Limited Database Applications Customer Management Name: John Doe Address: 101 Dalmation Street Salary: $43,000 Age: 34 PF1 - Help PF2 - Add PF3 - Update F ile E dit W indow H elp Customer Management Name: Address: Salary: John Doe 101 Dalmation St. $48,000 Cancel L oan History U pdate Age: 34 CUI client GUI client Database Server Disks & Files Enter details and press PF3 to update Networks Browser client Application Server Web Server Inter ne/ Intra net LAN / WAN

Upload: sharktale2828

Post on 28-Nov-2015

16 views

Category:

Documents


1 download

DESCRIPTION

ASP2 ADO DataReader

TRANSCRIPT

Page 1: ASP2 ADO DataReader

ASP.NET and ADO.NET - 1 Sonata Software Limited

Database Applications

Customer Management

Name: John DoeAddress: 101 Dalmation Street Salary: $43,000 Age: 34

PF1 - Help PF2 - Add PF3 - Update

File Edit Window HelpCustomer Management

Name:

Address:

Salary:

John Doe

101 Dalmation St.

$48,000

CancelLoan History Update

Age: 34

CUI client

GUI client

Database Server

Disks & Files

Enter details and press PF3 to update

Networks

Browser client

Application Server

Web Server

Interne/

Intranet

Interne/

Intranet

LAN / WAN

LAN / WAN

Page 2: ASP2 ADO DataReader

ASP.NET and ADO.NET - 2 Sonata Software Limited

DataReader: Direct Access to Database

Connection

Command DataReader

Command

•Select •Stored procedure

•Insert•Update•Delete •Stored procedure

Client Application

Database

Read

Write

Windows Form

Web Form

Page 3: ASP2 ADO DataReader

ASP.NET and ADO.NET - 3 Sonata Software Limited

Cursor of a "Record Set"

100 Java 200.00

200 HTML 250.00

300 C 300.00

400 XML 240.00

Record set

Initial position of cursor

Current cursor position

dr.read() moves the cursor to the next row and return TrueTrue if the next row exists.

If current cursor position is here, then the

execution of dr.read() returns FalseFalse.

Num Name Price0 1 2

Column index

Column name

Dim dr As OleDbDataReader dr = catCMD.ExecuteReader()

Page 4: ASP2 ADO DataReader

ASP.NET and ADO.NET - 4 Sonata Software Limited

Accessing Column Values

• 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. 

By Zero-based Column Type Safe or Ordinal Reference

By Column Name String Reference

Returned Value

dr.getInt32(0) or dr(0) dr("Num") 200

dr.getString(1) or dr(1) dr("Name") HTML

dr.getDecimal(2) or dr(2) dr("Price") 250.00

dr.getDecimal("Price") is illegal! dr.getDecimal("Price") is illegal!

Page 5: ASP2 ADO DataReader

ASP.NET and ADO.NET - 5 Sonata Software Limited

Categories Table in NorthWind Access Database

Page 6: ASP2 ADO DataReader

ASP.NET and ADO.NET - 6 Sonata Software Limited

CategoryList.aspx

Page 7: ASP2 ADO DataReader

ASP.NET and ADO.NET - 7 Sonata Software Limited

Using DataReader

Categorytable

NorthWind

100

ASP.NET Code

100

Record set

SQL statementFormat result result set rows in set rows in HTML formatHTML format

Dynamically generated Dynamically generated web pageweb page

12

34

DataReader

5

Retrieve result set,result set,one row at a time.one row at a time.

Page 8: ASP2 ADO DataReader

ASP.NET and ADO.NET - 8 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")

Page 9: ASP2 ADO DataReader

ASP.NET and ADO.NET - 9 Sonata Software Limited

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>

Page 10: ASP2 ADO DataReader

ASP.NET and ADO.NET - 10 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>

Page 11: ASP2 ADO DataReader

ASP.NET and ADO.NET - 11 Sonata Software Limited

Commonly Used DataReader Properties and Methods

Page 12: ASP2 ADO DataReader

ASP.NET and ADO.NET - 12 Sonata Software Limited

Using Code-Behind

Page 13: ASP2 ADO DataReader

ASP.NET and ADO.NET - 13 Sonata Software Limited

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>

Page 14: ASP2 ADO DataReader

ASP.NET and ADO.NET - 14 Sonata Software Limited

CategoryList2.aspx.vbImports System.Data.OleDBImports 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 SubEnd Class

Page 15: ASP2 ADO DataReader

ASP.NET and ADO.NET - 15 Sonata Software Limited

CategoryList3.aspx

Page 16: ASP2 ADO DataReader

ASP.NET and ADO.NET - 16 Sonata Software Limited

Using DataReader

Categorytable

NorthWind

100

ASP.NET Code

100

Record set

SQL statementFormat result result set rows in set rows in HTML formatHTML format

Dynamically generated Dynamically generated web pageweb page

1

2

34

DataReader

5

<form action="xxx.aspx" method="post"> (from a static HTML page)

<a href="xxx.aspx?cid=7&name=Produce"> Product</a>

Retrieve result set,result set,one row at a time.one row at a time.

Invocation of ASP.NET programs

Page 17: ASP2 ADO DataReader

ASP.NET and ADO.NET - 17 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>

</BODY></HTML>

Page 18: ASP2 ADO DataReader

ASP.NET and ADO.NET - 18 Sonata Software Limited

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

Page 19: ASP2 ADO DataReader

ASP.NET and ADO.NET - 19 Sonata Software Limited

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>

CategoryList3.aspx - Continued

Page 20: ASP2 ADO DataReader

ASP.NET and ADO.NET - 20 Sonata Software Limited

The Data Model of the NorthWind Database

Page 21: ASP2 ADO DataReader

ASP.NET and ADO.NET - 21 Sonata Software Limited

Drill Down Data Navigation

http://localhost:3459/tutor/ProductBycategory.aspx?cid=1&cname=Beverages

Page 22: ASP2 ADO DataReader

ASP.NET and ADO.NET - 22 Sonata Software Limited

CategoryList4.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 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

http://localhost:3459/tutor/ProductBycategory.aspx?cid=1&cname=Beverages

Page 23: ASP2 ADO DataReader

ASP.NET and ADO.NET - 23 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>

Page 24: ASP2 ADO DataReader

ASP.NET and ADO.NET - 24 Sonata Software Limited

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

Page 25: ASP2 ADO DataReader

ASP.NET and ADO.NET - 25 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>

Page 26: ASP2 ADO DataReader

ASP.NET and ADO.NET - 26 Sonata Software Limited

Generating HTML Dynamically

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

"<tr><td>" & 1 & "</td><td>" & Beverages & "</td></tr>"

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

Separate static text from dynamic ones

Replace dynamic data with actual DataReader methods

Page 27: ASP2 ADO DataReader

ASP.NET and ADO.NET - 27 Sonata Software Limited

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>"

<tr><td> 1 </td><td><a href='ProductsByCategory.aspx?cid=11&cname=BeveragesBeverages'>Beverages</a></td></tr>

"<tr><td>" & 11 & "</td><td><a href='ProductsByCategory.aspx?cid=" & 11 & "&cname=" & BeveragesBeverages & "'>" & Beverages Beverages & "</a></td></tr>"

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

Page 28: ASP2 ADO DataReader

ASP.NET and ADO.NET - 28 Sonata Software Limited

Design View

Page 29: ASP2 ADO DataReader

ASP.NET and ADO.NET - 29 Sonata Software Limited

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()

Page 30: ASP2 ADO DataReader

ASP.NET and ADO.NET - 30 Sonata Software Limited

SQL Statement - Join

SELECT ProductID, ProductName, UnitPrice, Products.CategoryID, CategoryName, Discontinued FROM Products, Categories

Where ProductID=11 AND Products.CategoryID = Categories.categoryID

Page 31: ASP2 ADO DataReader

ASP.NET and ADO.NET - 31 Sonata Software Limited

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")

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</script>

continued

Page 32: ASP2 ADO DataReader

ASP.NET and ADO.NET - 32 Sonata Software Limited

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>

Page 33: ASP2 ADO DataReader

ASP.NET and ADO.NET - 33 Sonata Software Limited

Page 34: ASP2 ADO DataReader

ASP.NET and ADO.NET - 34 Sonata Software Limited

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

Page 35: ASP2 ADO DataReader

ASP.NET and ADO.NET - 35 Sonata Software Limited

• 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