比较标准的读取数据语句asp.net c#

protected void Page_Load(object sender, EventArgs e)
{
string str_provider = "Provider=Microsoft.Jet.OLEDB.4.0;";
string str_source = "Data Source=" + MapPath("../App_Data/data.mdb");
string str_conn = str_provider + str_source;

OleDbConnection conn;
OleDbCommand comm;
OleDbDataReader datar;

string str_sql = "select c1 from data ";

conn = new OleDbConnection(str_conn);
conn.Open();
comm = new OleDbCommand(str_sql, conn);

datar = comm.ExecuteReader();
while (datar.Read())
{
reader.InnerHtml += datar["c1"].ToString() + "<br/>";
}
conn.Close();
}

----------------------------------------优化1

 

protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MapPath("../App_Data/data.mdb"));
conn.Open();
OleDbCommand cmd = new OleDbCommand("select c1 from data", conn);
OleDbDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
reader.InnerHtml += odr["c1"].ToString() + "<br/>";
}
conn.Close();
}

 

------------------------------------------------------优化2

protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|datadirectory|data.mdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand("select c1 from data", conn);

using (OleDbDataReader odr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (odr.Read())
{
reader.InnerHtml += odr["c1"].ToString() + "<br/>";
}

}

}

 

 

<div id="reader" runat="server" style="border: 1px solid black">

比较标准的读取数据语句asp.net c#

上一篇:[WPF 自定义控件]创建包含CheckBox的ListBoxItem


下一篇:【100】VS2013创建Windows服务与调试服务