asp向asp.net应用程序的转变过程

2008-02-22 09:29:55来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

下面示例中的第一个代码块对于某类 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>")

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:为ASP.NET封装的SQL数据库访问类

下一篇:安装好.net之后如何运行asp.net程序