Sunday, October 7, 2012

Connect MS Access using C# (OleDB Connection)

Connect MS Access using C#  (OleDB Connection)

//Add References
//using System.Data.OleDb;
//Retrieving Record

            DataSet ds;
            OleDbCommand ocmd;
            OleDbDataAdapter da;
            OleDbConnection dbcon = new OleDbConnection("PROVIDER=microsoft.jet.oledb.4.0;data source =" + System.AppDomain.CurrentDomain.BaseDirectory + "\\dbase\\dbase.mdb");
            dbcon.Open();
            da = new OleDbDataAdapter("SELECT * FROM `tblstudent` ORDER BY `Last_Name`,`First_Name`, `Middle_Name` ASC", dbcon);
            ds = new DataSet();
            da.Fill(ds, "tblstudent");
            dbcon.Close();
            MessageBox.Show(ds.Tables["tblstudent"].Rows[5].ItemArray[3].ToString());

//INSERT, UPDATE, DELETE Record

OleDbCommand ocmd;

OleDbDataAdapter da;

 OleDbConnection dbcon = new OleDbConnection("PROVIDER=microsoft.jet.oledb.4.0;data source =" + System.AppDomain.CurrentDomain.BaseDirectory + "\\dbase\\dbase.mdb");

    dbcon.Open();

ocmd=new OleDbCommand("INSERT INTO tblstudent (`Stud_ID`) VALUES('00-99999')",dbcon);          

ocmd.ExecuteNonQuery();       

 dbcon.Close();

0 comments: