DataReader对象
DataReader对象用于从数据库中获取仅向前的只读数据流,由于在内存一次只存放一行数据,因此使用DataReader对象可提高应用程序的性能,大幅度减少对内存的需求。
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("person.mdb");
conn.Open();
string strSQL = "select * from grade";
OleDbCommand Comm = new OleDbCommand(strSQL, conn);
OleDbDataReader dr = Comm.ExecuteReader();
string html = "<table border=1>";
html += "<tr>";
html += "<td><b>学号</b></td>";
html += "<td><b>姓名</b></td>";
html += "<td><b>数学</b></td>";
html += "</tr>";
try
{
while (dr.Read())
{
html += "<tr>";
html += "<td>" + dr["学号"].ToString() + "</td>";
html += "<td>" + dr["姓名"].ToString() + "</td>";
html += "<td>" + dr["数学"].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
}
finally
{
dr.Close();
conn.Close();
}
Response.Write(html);
}
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("person.mdb");
conn.Open();
string strSQL = "select * from grade";
OleDbCommand Comm = new OleDbCommand(strSQL, conn);
OleDbDataReader dr = Comm.ExecuteReader();
string html = "<table border=1>";
html += "<tr>";
html += "<td><b>学号</b></td>";
html += "<td><b>姓名</b></td>";
html += "<td><b>数学</b></td>";
html += "</tr>";
try
{
while (dr.Read())
{
html += "<tr>";
html += "<td>" + dr["学号"].ToString() + "</td>";
html += "<td>" + dr["姓名"].ToString() + "</td>";
html += "<td>" + dr["数学"].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
}
finally
{
dr.Close();
conn.Close();
}
Response.Write(html);
}