datareader1.aspx

来自「《精通ASP.NET网络编程》附带实例」· ASPX 代码 · 共 43 行

ASPX
43
字号
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<html>
<head>
<Script Language=VB Runat=Server>
   Sub Page_Load(Sender As Object, e As EventArgs)
Dim mySQLConnection As OleDbConnection
Dim mySQLCommand As OleDbCommand
Dim html As String
Dim myReader As OleDbDataReader
'进行数据库链接
mySQLConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath("Sample.mdb"))
mySQLCommand = new OleDbCommand("Select * from 学生成绩表", mySQLConnection)
'打开数据库链接并执行SQL语句
mySQLCommand.Connection.Open()
'将结果返回给myReader对象

myReader=mySQLCommand.ExecuteReader()
html="<Table>"
html+="<TR>"
html+="<TD><B>学号</B></TD>"
html+="<TD><B>姓名</B></TD>"
html+="</TR>"
try
'读出每一条记录
  While(myReader.Read()) 
  html+="<TR>"
  html+="<TD>" +  myReader("学号").ToString() + "</TD>"
  html+="<TD>" +  myReader("姓名").ToString() + "</TD>"
  html+="</TR>"
  End While
  html+="</Table>"

finally
'关闭链接
  myReader.Close()
  mySQLConnection.Close()
End Try
  Response.Write(html)
End Sub

</Script>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?