ASP.NET 資料存取
.NET Framework 包含一項名為 ADO.NET 的新資料存取技術,是 ADO 的改良版本。但是,組成 ADO.NET 的類別與您所熟悉的 ADO 物件並不相同。必須對現有 ADO 應用程式作些變更,才能將它們轉換為 ADO.NET。不必立刻對現有 ADO 應用程式作變更,這些程式就可以在 ASP.NET 下執行;但是,因為 ADO 會在 ASP.NET 下作用,所以將 ADO 應用程式轉換為 ADO.NET 是值得的。對中斷連接的應用程式而言,ADO.NET 提供超越 ADO 中斷連接資料錄集 (Disconnected Recordset) 的效能優勢。ADO 需要傳送和接受元件是 COM 物件,而 ADO.NET 以標準 XML 格式傳送資料,所以不需要 COM 封送處理 (Marshaling) 或資料型別轉換。
擷取自資料庫的資料通常以兩種方式使用。資料錄可由 ASP 程式碼檢視和管理,作用如同某些動作的預先指標,不必直接對用戶端顯示,或是資料錄只在資料表或方格中對用戶端顯示。這個主題中的範例顯示如何將典型的 ADO 案例轉換為 ADO.NET。除了所顯示的簡短範例之外,還有很多的 ADO.NET 模型;這僅是將通用的 ASP 和 ADO 案例移植至 .NET Framework 的快速概觀。如需 ADO.NET 介紹的詳細資訊,請參閱 ADO.NET 概觀。
使用單一資料表
下列範例中程式碼的第一區塊是典型的 ASP 應用程式,它利用 ADO 讀取和管理從單一 SQL 查詢傳回的一組資料錄。它使用 ADO Recordset 物件讀取從 Microsoft Access 提供的 Northwind 範例資料庫所傳回的資料錄。這個程式碼將包含在副檔名為 .asp 的檔案中。
<%@LANGUAGE=VBSCRIPT%>
<!
This ASP example uses ADO to read records from a database and print two
fields from all returned records to an ASP page. Connection to the Northwind database is through an ODBC system data source (DSN.
>
<html>
<body>
<%
dim ADOconn, ADOrs, sqlstr
sqlstr="SELECT * FROM Employees;"
set ADOconn = Server.CreateObject("ADODB.Connection")
ADOconn.Open "DSN = Test"
set ADOrs = ADOconn.execute(sqlstr)
if ADOrs.BOF and ADOrs.EOF then ' Query didn't return any records.
Response.Write("No Records.")
else
ADOrs.MoveFirst
Do While Not ADOrs.EOF
Response.Write(Server.HtmlEncode(ADOrs("FirstName")) & " " _
& Server.HtmlEncode(ADOrs("LastName")) & "<br>")
ADOrs.MoveNext
Loop
Response.Write("<p>End of data.")
end if
ADOrs.close
set ADOrs = nothing
%>
</body>
</html>
以下範例說明將之前範例轉換為 ASP.NET 應用程式所需的最基本的變更。大部份的必要變更是為了符合新的 Visual Basic 語法。這個檔案可重新命名為副檔名為 .aspx 的檔案,並且可以利用 ASP.NET 來執行。修訂過的行以粗體表示。請注意第一行的 <%@ Page > 指示詞加入了 aspcompat=true 屬性。
<%@Page aspcompat=true Language = VB%>
<!
This example uses ADO to read records from a database and print two
fields from all records in the database to an ASP.NET page.
The database is located on the server and connection is through an ODBC system data source (DSN.
>
<html>
<body>
<%
dim objConn, rs, sqlstr
sqlstr="SELECT * FROM Employees;"
objConn = Server.CreateObject("ADODB.Connection") ' Set removed.objConn.Open("DSN=TEST") ' Parentheses added.rs = objConn.execute(sqlstr) ' Set statement removed.
Response.Write("<p>ADO Test</p>")
if rs.BOF and rs.EOF then ' Query didn't return any records.
Response.Write("No Records")
else
rs.MoveFirst
Do While Not rs.EOF
' Specify Value property.
Response.Write(Server.HtmlEncode(rs("FirstName").Value) _
& " " & Server.HtmlEncode(rs("LastName").Value) & "<br>")
rs.MoveNext
Loop
Response.Write("<p>End of data")
end if
rs.close
rs = nothing ' Set statement removed.
%>
下一個範例為 ASP.NET 應用程式,這個程式使用 ADO.NET 從與前例相同的 Northwind 資料庫中讀取資料錄。這個產生與前例相同輸出的程式碼,已修改為符合 ASP.NET 程式碼區塊慣例。
這個範例建立 ADO.NET DataSet 物件,在這個例子中所包含的資料表的使用方式與 ADO Recordset 相同。請注意,DataSet 可包含一或一個以上的 DataTables、DataRelations 和 Constraints 的集合,這個集合形成常駐記憶體的資料庫,所以 ADO.NET DataSet 比 ADO Recordset 更具有彈性。
為了要使用 ADO.NET,您需要匯入 System.Data 和 System.Data.OleDb 命名空間 (Namespace)。如果您的資料來源是 SQL Server 資料庫,則應匯入 System.Data.SqlClient 命名空間而不是 System.Data.OleDb。如需使用 ADO 和 SQL .NET 資料提供者 (Data Provider) 連接物件的詳細資訊,請參閱 Managed 連接。
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<!
This example uses ADO.NET to read records from a database and print two
fields from all returned records to an ASP.NET page. The database
is located on the local server.
>
<html>
<Script Language=VB Runat=Server>
Sub Page_Load(Sender As Object, e As EventArgs)
Dim MyConnection As OleDbConnection
Dim MyCommand As OleDbDataAdapter
dim MyDataset As DataSet
dim MyTable As DataTable
dim loop1, numrows As Integer
dim sqlstr As String
sqlstr = "SELECT * FROM Employees;"
' Create a connection to the data source.
MyConnection = New OleDbConnection("Provider=SQLOLEDB;" _
& "server=localhost;"Integrated Security=SSPI;" _
& "Initial Catalog=Northwind")
' Create a Command object with the SQL statement.
MyCommand = New OleDbDataAdapter(sqlstr, MyConnection)
' Fill a DataSet with data returned from the database.
MyDataset = New DataSet
MyCommand.Fill(MyDataset)
' Create a new DataTable object and assign to it
' the new table in the Tables collection.
MyTable = New DataTable
MyTable = MyDataset.Tables(0)
' Find how many rows are in the Rows collection
' of the new DataTable object.
numrows = MyTable.Rows.Count
If numrows = 0 then
Response.Write("<p>No records.</p>")
Else
Response.Write("<p>" & Cstr(numrows) & " records found.</p>")
For loop1 = 0 To numrows - 1
' Print the values of the two columns in the Columns
' collection for each row.
Response.Write(Server.HtmlEncode(MyTable.Rows(loop1).Item("FirstName")) _
& " " & Server.HtmlEncode(MyTable.Rows(loop1).Item("LastName")) & "<br>")
Next loop1
End If
Response.Write("<p>End of data.</p>")
End Sub
</Script>
</html>
假使資料庫查詢 (即使是多資料表聯結查詢) 僅傳回單一的資料錄集,您可以利用十分類似使用 ADO Recordset 的方法來使用單一的 DataTable (這個範例中是 MyTable
)。
使用多重資料庫資料表
ADO.NET DataSet 可包含多個資料表和說明關聯式資料庫本機複本的一組關聯。可將它視為伺服器記憶體中的迷你資料庫。使用關聯物件,您的程式碼可巡覽資料表集合,以複雜的方式存取資料錄,而不必對主要資料存放區作進一步的查詢。如需 DataSet 元件的詳細資訊,請參閱 ADO.NET DataSet。
在控制項中顯示資料
若要在瀏覽器中以表格的格式顯示資料庫資料,ASP 開發人員必須撰寫程式碼,利用 HTML 標記散置這些資料,以便快速建置 HTML 資料表。ASP.NET 包括 DataGrid、DataList 和 Repeater 伺服器控制項,這些控制項能夠大幅簡化在 Web 網頁上顯示表格式資料的工作。ADO.NET 資料集可以輕易地繫結至這些控制項。ASP.NET 引擎與控制項搭配,會傳送純 HTML 3.2 到瀏覽器,產生豐富的格式化配置供使用者閱覽。
如需使用這些控制項顯示資料庫資料的特定範例,請參閱將 SQL 資料繫結至 DataGrid 控制項、將 SQL 資料繫結至 DataList 控制項和將 SQL 資料繫結至 Repeater 控制項。