由asp向asp.net的应用程序转变过程
下面示例中的第一个代码块对于某类 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>(文章来源 www.iocblog.net)
下面的示例阐释将前面示例转换为 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 数据提供程序的连接对象的详细信息,请参见管理连接。
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。