zoukankan      html  css  js  c++  java
  • 比较标准的读取数据语句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">

    天道酬勤,厚积薄发。 君子之行,静以修身,俭以养德。 非淡泊无以明志,非宁静无以致远。 如有恒,何须三更起,半夜眠;最怕莫,三天打鱼两天晒网,竹篮打水一场空。
  • 相关阅读:
    想要快速上手 Spring Boot?看这些教程就足够了!| 码云周刊第 81 期
    Eclipse Java注释模板设置详解以及版权声明
    java跨域解决
    微信公众号创建
    Java与JS生成二维码与条形码
    Java基础break、continue语句的用法
    分布式文件系统介绍
    Hadoop的RPC机制及简单实现
    RPC简介与hdfs读过程与写过程简介
    JVM的Client模式与Server模式
  • 原文地址:https://www.cnblogs.com/houweidong/p/12319338.html
Copyright © 2011-2022 走看看