Access数据库连接与Repeater数据控件绑定

一、web.config配置access数据库连接字符串

<configuration>

    <appSettings>
      <add key="SQLConnString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="/>
      <add key="dbPath" value="~/App_Data/mydb.mdb"/>
    </appSettings>

</configuration>

二、数据库、表

数据库:App_Data/mydb.mdb

 

 

三、web窗体文件(connAccess.aspx)

        Repeater数据控件绑定<br />
        <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
         <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <a href="News_show.aspx?ID=<%# Eval("userid")%>" target=_blank><%# Eval("username") %></a>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        </asp:Repeater>

四、查询、绑定处理(connAccess.aspx.cs)

1、命名空间导入:using System.Data.OleDb;

2、

protected void Page_Load(object sender, EventArgs e)
    {
        string settings = ConfigurationManager.AppSettings["SQLConnString"].ToString() + Server.MapPath(ConfigurationManager.AppSettings["dbPath"]) + ";";
        //Response.Write(settings);
        //Response.End();
        OleDbConnection myconn = new OleDbConnection(settings);
        myconn.Open(); //打开数据库连接
        string sqlstr = "select * from admin";
        OleDbCommand mycmd = new OleDbCommand(sqlstr, myconn);//创建数据库命令
        OleDbDataReader mydr = mycmd.ExecuteReader();
        //Repeater数据控件绑定
        Repeater1.DataSource = mydr;//指定数据源
        Repeater1.DataBind();//绑定到指定的数据源

     }

你可能感兴趣的:(Access)