Top Banner
Chapter 7 ADO.NET
24

Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Jan 03, 2016

Download

Documents

Dustin Boyd
Welcome message from author
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
Page 1: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Chapter 7 ADO.NET

Page 2: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

關聯式資料庫

Database

Tables

Records

Fields

Definition:

Page 3: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

Select statement

Select field_1[, field_2, …, fiels_N] From DataTable

ex: Select UserID, UserPwd From Members Select * From Members

Page 4: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

Select … Where statement

Select field_1[, …, fields_N] From DataTable Where conditions

ex: Select * From Members Where UserID = ‘tina’ Select * From Members Where UserID=‘tina’ And UserPwd=‘1234’ Select * From Members Where UserID In (‘tina’, ‘jacky’) Select * From Members Where UserAdd Like ‘ 台北市 %’

Page 5: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

Select … Order By statement

Select field_1[, …, fields_N] From DataTable [Where conditions] [Order By statement [Asc/Desc]]

ex: Select * From Members Order By UserID Asc

Page 6: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

多資料表 Select statement

Select distributors.distri_num, company, phone,order_num, order_date from distributors, orderswhere distributors.distri_num = orders.distri_num order by 1

Page 7: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

集總函數 - Count, Sum, Avg, Max, Min

Select supp_code supply_ID, avg(unit_price) Average_Priceform products group by supp_code

ex: Select sum(total_price) from items where supp_code=‘HHT’

Page 8: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

Insert Statement

Insert Into dataTable [(field_1,…,field_N)] Values(field_1,…, field_N)

field_Name

ex: Insert Into Members Values (‘elvira’, ‘wxyz’)

Page 9: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

Update statement

Update DataTable Set field=statement [,…] [Where Conditions]

ex: Update Member Set UserPwd=‘zyxw’ Where UserID=‘elvira’

Page 10: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

SQL 結構化查詢語言

Delete Statement

Delete DataTable [Where conditions]

ex: Delete Members Where UserID=‘Jacken’

Page 11: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Sequential/Random File

JET/DAO (MS Access – ISAM Format)

Indexed sequential access method

Data access object

ODBC (Open database connectivity)DBMS Suppliers -> follow spec. to write driverClient - > ODBC API (complex!!)

Not efficiency

RDO (Remote Data Object) + ODBC (No local ISAM)

DAO + ODBC (ODBCDirect) OLE DB/ADO

遠端關聯式資料庫

Database Access Historical

Page 12: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

OLE DB/ADO – 物件導向

OLE DB – 系統層次 資料區分為 提供者與使用者 提供四種類別 Data Source Session Command Rowset

ADO – 應用層次 六種物件 (DAO 14 種 , RDO 11 種 ) Connection Command Field Recordset Parameter Error

Page 13: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

ADO

OLE DB

ODBC

關聯式與ISAM 資料庫

非關聯式及自訂格式資料

大型主機專屬資料庫

應用程式 / 瀏覽器

自訂 OLE DB Provider

No OLE DB providerWith ODBC

Page 14: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

An Overview of ADO.NET

.NET framework contains several namespaces* withdozens of classes devoted to database access. First, we concern only two of the namespaces.

System.Data.SqlClient

System.Data.OleDb

Note: A namespace is a logical grouping of classes. The namespaces are organized into a hierarchy (a logical tree). The root of the tree is the system namespace

Page 15: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

An Overview of ADO.NET

•System.Data.SqlClient – three classes

SqlConnectionSqlCommandSqlDataReader

If you plan to build your ASP.NET application withMicrosoft SQL Server (7.0 or higher), you will usethese classes most often. These classes enable you to execute SQL statements and quickly retrieve datafrom a database query.

works only with Microsoft SQL server

Note:

Page 16: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

An Overview of ADO.NET

•System.Data.OleDb – three classes

OleDbConnectionOleDbCommandOleDbDataReader

Use OLE DB providers toconnect to a database

Note: The SQL classes communicate with MS SQL directly on the level of Tabular Data Stream (TDS) protocol.

Page 17: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Step 1: Import namespace

<%@ Import Namespace=“System.Data.SqlClient” %>

<%@ Import Namespace=“System.Data.OleDb” %>

or

Page 18: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Step 2: Create and open a database connection

<%@ Import Namespace=“System.Data.SqlClient” %><Script Language=“VB” Runat=“Server”>Sub Page_Load Dim conPubs As SqlConnection conPubs = New SqlConnection( _ “Server=localhost;uid=sa;pwd=secret;database=pubs”) conPubs.Open()End Sub</Script>

Connection String

Page 19: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Step 2: Create and open a database connection

<%@ Import Namespace=“System.Data.OleDb” %><Script Language=“VB” Runat=“Server”>Sub Page_Load(s as Object, e as Eventargs) Dim conAuthors As OleDbConnection conAuthors = New OleDbConnection( “PROVIDER= Microsoft.Jet.OLEDB.4.0;DATA Source=C:\Authors.mdb”) conAuthors.Open()End Sub</Script>

Note: Oracle dataase -> use the MSDAORA provider

Page 20: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Step 2: Create and open a database connection

By default when you call the open() method, the connection is given 15 seconds to open before timing out. You can override this default behavior by supplying a Connect Timeout attribute in theconnection string.

conPubs = New SqlConnection(“Server=localhost;uid=sa;

pwd=secret;database=pubs; Connect Timeout=90”)

Page 21: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Step 3: Retrieving Records – From a Database Table<%@ Import Namespace=“System.Data.SqlClient” %><Script Language=“VB” Runat=“Server”>Sub Page_Load Dim conPubs As SqlConnection Dim cmdAuthors As SqlCommand Dim dtrAuthors As SqlDataReader conPubs = New SqlConnection(“Server=localhost;uid=sa;pwd=secret;database=pubs”) conPubs.Open() cmdAuthors = New SqlCommand(“Select au_lname From Authors”, conPubs) dtrAuthors=cmdAuthors.ExecuteReader() While dtrAuthors.Read() Response.Write(“<Li>”) Response.Write(dtrAuthors(“au_lname”)) End While dtrAuthors.Close() conPubs.Close()End Sub</Script>

Page 22: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

DataReader 物件常用屬性

FieldCount - 多少欄位HasMoreRows - True/FalseItem - 傳回欄位值

DataReader 物件常用方法

CloseGetName - 取得欄位名稱GetOrdinal - 取得欄位所在位址GetValues - 傳回紀錄中所有欄位的值IsDBNull - Is Null

Page 23: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Example:

while myReader.Read() If myReader.IsDBNull(0) Then Response.Write(“ 不詳” ) Else For I = 0 to myReader.FieldCount-1 Response.Write(myReader.GetName(I) & “:”) Response.Write(myReader.Item(I)) Response.Write(“<BR>”) Next End If Response.Write(“<HR>”>End While

Page 24: Chapter 7 ADO.NET. 關聯式資料庫 Database Tables Records Fields Definition:

Performing Common Database Tasks

Step 3: Retrieving a Single Database Record<%@ Import Namespace=“System.Data.SqlClient” %><Script Language=“VB” Runat=“Server”>Sub Page_Load Dim conPubs As SqlConnection Dim cmdSelectCount As SqlCommand conPubs = New SqlConnection(“Server=localhost;uid=sa;pwd=secret;database=pubs”) conPubs.Open() cmdSelectCount = New SqlCommand(“Select Count(*) From Authors”, conPubs) lblResults.Text = cmdSelectCount.ExecuteScalar() conPubs.Close()End Sub</Script><html>…</html>