Tuesday, September 9, 2008

Access Database with ADO.NET Connection

Walkthrough: Editing an Access Database with ADO.NET

using System.Data.OleDb;
private void ReadRecords()
{
OleDbConnection conn = null;
OleDbDataReader reader = null;
try
{
conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath("Pets/Pets.mdb"));
conn.Open();
OleDbCommand cmd =
new OleDbCommand("Select * FROM PetTable", conn);
reader = cmd.ExecuteReader();
datagrid.DataSource = reader;
datagrid.DataBind();
}
// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
}

No comments: