2011-11-03 92 views
-1

如何使用連接到Access數據庫填充GridView如何使用到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(); 

但現在該怎麼辦?

回答

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(); 
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

希望這有助於