Accessing MS Access database from C#

Posted by Abilash on Stack Overflow See other posts from Stack Overflow or by Abilash
Published on 2010-12-24T13:51:11Z Indexed on 2010/12/24 13:54 UTC
Read the original article Hit count: 263

Filed under:
|
|
|

I want to use MS Access as database for my C# windows form application.I have used OleDb driver for connecting MS Access. I am able to select the records from the MS Access using OleDbConnection and ExecuteReader.But I am un able to insert,update and delete records. My code is as follows:

 OleDbConnection con=new OleDbConnection(strCon);
        try
        {
        con.Open();
        OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con);

        int a=com.ExecuteNonQuery();

        //OleDbCommand com = new OleDbCommand("SELECT * FROM DPMaster", con);
        //OleDbDataReader dr = com.ExecuteReader();
        //while (dr.Read())
        //{
        //    MessageBox.Show(dr[2].ToString());
        //}
        MessageBox.Show(a.ToString());
        }
        catch
        {
            MessageBox.Show("cannot");
        }

If I execute the commented block the application works.But the insert block doesnt works.Why I am unable to insert/update/delete the records into database?

My Connection String is as follows:

        string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xyz.mdb;Persist Security Info=True";

© Stack Overflow or respective owner

Related posts about c#

Related posts about ms-access