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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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
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()
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!
ASP.NET and ADO.NET - 5 Sonata Software Limited
Categories Table in NorthWind Access Database
ASP.NET and ADO.NET - 6 Sonata Software Limited
CategoryList.aspx
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.
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)
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 - 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>
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
ASP.NET and ADO.NET - 15 Sonata Software Limited
CategoryList3.aspx
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)
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> " & dr.GetInt32(0)) table.Append("</td><td> " & dr.GetString(1) & "</td></tr>") End While
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>
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=" & _
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()
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
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")
& 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>