文章探索:   分类:    关键字:  
  + 栏目导航
  + 相关文章
ASP.NET编程中url编码的转换
Asp.net把UTF-8编码转换为GB2312编码
如何用vb.net获取网页源代码2
如何用vb.net获取网页源代码
VB.Net中关于数组赋值
Imports 语句
Imports 语句分析
VB.NET入门--Imports 语句
asp.net连接Access数据库
Asp.net如何连接Access数据库
ASP.NET技巧:access下的分页方案
ASP.net的ACCESS数据分页方案
ASP.NET与ACCESS结合建设网站的经验总..
编译asp.net文件为dll文件
ASP.NET的错误处理机制
ASP.NET 2.0编程小技巧两则
ASP.NET 2.0发送电子邮件全面剖析之二
ASP.NET 2.0中发送电子邮件剖析之一
如何充分利用ASP.NET 2.0预编译
如何改变asp.net项目名称
轻松加密ASP.NET 2.0 Web程序配置信息
Visual Studio.NET常见问题解答
ASP.Net中将图片数据保存到XML文档
ASP.NET中随机数的产生
提高ASP.Net应用程序性能的十大方法
ASP.NET中利用cookies保持客户端信息
ASP.NET热点问题解答14个
用ASP.NET加密Cookie数据(2)
用ASP.NET加密Cookie数据(1)
ASP.NET中在线用户统计


技术教程 -> ASP.NET教程 ->  
asp向asp.net应用程序的转变过程
来源:转载   人气:723   录入时间:2007-11-8
     下面示例中的第一个代码块对于某类 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文挡》




Copyright(C)2007-2024 广州市佳沛数码科技有限公司 版权所有
公司地址: 广州市荔湾区东漖北路560号511室
电话:020-81803473 传真:020-81544987