Using system;
Using system. Data. oledb;
Class oledbtest {
Public static void main ()
{
// Create a database connection
Oledbconnection aconnection = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source = C: \\\ db1.mdb ");
// Create a command object and save the SQL query statement
Oledbcommand acommand = new oledbcommand ("select * From emp_test", aconnection );
Try
{
Aconnection. open ();
// Create a datareader object to connect to the form
Oledbdatareader areader = acommand. executereader ();
Console. writeline ("this is the returned data from emp_test table ");
// Traverse the database cyclically
While (areader. Read ())
{
Console. writeline (areader. getint32 (0). tostring ());
}
// Close the reader object
Areader. Close ();
// Close the connection, which is very important
Aconnection. Close ();
}
// Some common Exception Handling
Catch (oledbexception E)
{
Console. writeline ("error: {0}", E. errors [0]. Message );
}
}
}
Note: use SQL statements in access to prevent the field names and Keywords of the database. . Net. Otherwise, an error is returned.