Method for connecting ASP. NET oledb to Access database, oledbaccess
To use the OleDBCommand operation class, you must introduce the System. Data. OleDb namespace.
Connect to an access database with a password to read the data and place the data in the table to verify the correctness of the connection and command usage.
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. data. oleDb; public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// database connection, acquisition of record set // change the path String sqlconn = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = E:/pro/web/access_test/app_data/Northwind. mdb; Jet OLEDB: Database Password = 111111 "; OleDbConnection myConnection = new OleDbConnection (sqlconn); myConnection. open (); OleDbCommand myCommand = new OleDbCommand ("select * from forwarder", myConnection); OleDbDataReader myReader; myReader = myCommand. executeReader (); // read the record set Response. write ("<table border = 1 cellspacing = 0 cellpadding = 2>"); Response. write ("<tr bgcolor = # DAB4B4>"); for (int I = 0; I <myReader. fieldCount; I ++) Response. write ("<td>" + myReader. getName (I) + "</td>"); Response. write ("</tr>"); while (myReader. read () {Response. write ("<tr>"); for (int I = 0; I <myReader. fieldCount; I ++) Response. write ("<td>" + myReader [I]. toString () + "</td>"); Response. write ("</tr>");} Response. write ("</table>"); // close the record set and connect to myReader. close (); myConnection. close ();}}
As follows:
The complete project file has been uploaded.