答案: 下面示例中的第一个代码块对于某类 ASP 应用程序是很典型的,该类应用程序使用 ADO 读取和操作从单个 SQL 查询返回的记录集。它使用 ADO Recordset 对象读取从用 Microsoft Access 提供的 Northwind 示例数据库返回的数据记录。这些代码将包含在具有 .asp 文件扩展名的文件中。
[Visual Basic]
<%@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(ADOrs("FirstName") & " " _
& 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 一起运行。修改后的代码行以粗体显示。注意,在第一行上添加了具有 aspcompat=true 属性的 <%@ Page > 指令。
[Visual Basic]
<%@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(rs("FirstName").Value _
& " " & 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 记录集的使用方式几乎相同。请注意,数据集可以由一个或多个构成内存驻留数据库的 DataTables、DataRelations 和 Constraints 的集合组成,因此 ADO.NET 数据集比 ADO 记录集灵活得多。
为了使用 ADO.NET,需要导入 System.Data 和 System.Data.OleDb 命名空间。如果数据源是 SQL Server 数据库,则导入 System.Data.SqlClient 命名空间而不是 System.Data.OleDb。有关使用 ADO 和 SQL .NET 数据提供程序的连接对象的详细信息,请参见管理连接。
[Visual Basic]
<%@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(MyTable.Rows(loop1).Item("FirstName") _
& " " & MyTable.Rows(loop1).Item("LastName") & "<br>")
Next loop1
End If
Response.Write("<p>End of data.</p>")
End Sub
</Script>
</html>
在数据库查询(甚至是多表联接查询)返回单个记录集的情况下,可以通过与使用 ADO 记录集的方式几乎相同的方式使用单个 DataTable(在此示例中为 MyTable)。
参考《NET FRAMEWORK SDK文挡》
上一个:asp.net中一次更新DATAGRID中所有记录
下一个:利用JS在asp.net中实现左导航页的隐藏