如何使用与ACCESS DB的连接填充gridview

时间:2011-11-03 10:09:08

标签: c# asp.net gridview oledbconnection

如何使用与Access数据库的连接填充GridView

到目前为止,我有以下代码:

OleDbConnection myConnection = default(OleDbConnection);
OleDbCommand myCommand = default(OleDbCommand);
string strSQL = null;
strSQL = "SELECT * FROM tblLoginInfo " + "WHERE username='" + CustID.Replace("'", "''") + "' " + "AND password='" + CustPass.Replace("'", "''") + "';";
myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + Server.MapPath("login.mdb") + ";");

myCommand = new OleDbCommand(strSQL, myConnection);
myConnection.Open();

但是现在呢?

2 个答案:

答案 0 :(得分:4)

 OleDbConnection myConnection = default(OleDbConnection);
         OleDbCommand myCommand = default(OleDbCommand);
         string strSQL = null;
         strSQL = "SELECT * FROM tblLoginInfo " + "WHERE username='" + CustID.Replace("'", "''") + "' " + "AND password='" +
 CustPass.Replace("'", "''") + "';";
         myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source="
 + Server.MapPath("login.mdb") + ";");
        myConnection.Open();
         myCommand = new OleDbCommand(strSQL, myConnection);

         OleDbDataAdapter adp=new OledbDataAdapter(myCommand);
         dataset ds=new dataset();
         adp.fill(ds);
        gridview.datasource=ds;
        gridview.databind();

答案 1 :(得分:0)

尝试这样的事情

        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/AccesFile.mdb" SelectCommand="SELECT * FROM [TableName]">
        </asp:AccessDataSource>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1">
        </asp:GridView>

AccessDataSource, SqlDataSource and connecting to Access databases in ASP.NET

中所述

我还发现了这篇关于Bind GridView with MS Access Database table in Asp.net

的文章

希望这有帮助