由asp向asp.net的应用程序转变过程(2)

分类: asp.net   出处:iocblog整理  更新时间:2009-03-20   添加到收藏  

    [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文挡》

上一页 [1] [2]



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。