Asp.net| object DataReader object as a asp.net read records in a relatively good control, its biggest advantage is that the speed, the use of frequent, and in a large number of Web site access, the DataSet object to avoid too much memory space occupied, Causes the server to overload the situation, thus greatly enhances the performance!
Of course, it also has a bad place, that is, the DataReader object can only be judged by the read () method, which is a read-only, forward-only stream of data. In addition, you can create the object by using the Command object's ExecuteReader.
Here is the basic code of use for this control, and I hope it works for everyone.
<%@ Page language= "VB" contenttype= "text/html" responseencoding= "gb2312"%>
<%@ import namespace= "System.Data"%>
<%@ import namespace= "System.Data.SqlClient"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<script language= "VB" runat= "Server" >
Sub Page_Load (sender as object,e as EventArgs)
Dim cnn As SqlConnection
Dim cmd As SqlCommand
Dim html As String
Dim Datar As SqlDataReader
' Establish a Connection object
Cnn=new SqlConnection ("Server=lyws;uid=sa;pwd=sa;database=book")
Cmd=new SqlCommand ("select * from BookInfo", CNN)
Cmd.connection.open ()
' Assign the result set to the DataReader object
Datar=cmd.executereader ()
Response. Write ("<center>Response. Write ("<center><table border=1 style= ' width:18cm ' >")
Response. Write ("<tr>")
Response. Write ("<th> Book Code </th>")
Response. Write ("<th> Book name </th>")
Response. Write ("<th> Press </th>")
Response. Write ("<th> pricing </th>")
Response. Write ("<th> </th>")
Response. Write ("<th> author </th>")
Response. Write ("</tr>")
Try
' Invokes the reader method of the DataReader object, through the while loop, the result set
while (Datar.read ())
Response. Write ("<tr>")
Response. Write ("<td align= ' center ' >" + Datar ("Book Code"). ToString () + "</td>")
Response. Write ("<td>" + Datar ("book name"). ToString () + "</td>")
Response. Write ("<td>" + Datar ("Publishing House"). ToString () + "</td>")
Response. Write ("<td align= ' right ' >" + Datar ("pricing"). ToString () + "</td>")
Response. Write ("<td>" + Datar ("edition"). ToString () + "</td>")
Response. Write ("<td>" + Datar ("author"). ToString () + "</td>")
Response. Write ("</tr>")
End While
Response. Write ("</table></center>")
Finally
Datar.close ()
Cnn.close ()
End Try
End Sub
</script>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>datareader Object Test!</title>
<body bgcolor= "#9999ff" >
<br>
<br>
web+ Database Design Series-> deciduous silent 67060096
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service