如何使用与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();
但是现在呢?
答案 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
的文章希望这有帮助