• 比较标准的读取数据语句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">

    天道酬勤,厚积薄发。 君子之行,静以修身,俭以养德。 非淡泊无以明志,非宁静无以致远。 如有恒,何须三更起,半夜眠;最怕莫,三天打鱼两天晒网,竹篮打水一场空。
  • 相关阅读:
    学习笔记-记ActiveMQ学习摘录与心得(一)
    c#写个基础的Socket通讯
    c#配置文件appStrings配置节的读取、添加和修改
    做个无边框winform窗体,并美化界面
    winform模拟鼠标按键
    winform程序开机自动启动代码
    记入园第一天
    HTTP Post 测试工具 (C#源代码)
    VC下调试内存泄漏的办法
    VMware共享文件夹遇到的问题
  • 原文地址:https://www.cnblogs.com/houweidong/p/12319338.html
Copyright © 2020-2023  润新知