locked
Connecting MS Access Database in C#

    Question


  • Hello Friends,

    My groupmates and I are having thesis right now, we need help.
    How can we connect database in C#,

    another problem is that storing and accessing of data on the database, .

    tnx...
     
    Friday, January 04, 2008 3:34 AM

Answers

  • Hi Dear,


    If you want to connect to Access Database.

    You have to follow the following steps:-

    1) First add  "using System.Data.OleDb;" at to top of the CS file.
    2) Create connection string and open connection.
    For access 2007
    string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";

    OleDbConnection MyConn = new OleDbConnection(ConnStr);


               
    3) Open this connection.
    MyConn.Open();

    4) Create object for command and reader to get the data from access database.

      OleDbCommand Cmd =  new OleDbCommand(StrCmd, MyConn);;
      OleDbDataReader ObjReader = Cmd.ExecuteReader();


    5) Now lood through the reader object to get the data

     if (ObjReader != null)
     {

    }

    6) After completing the processing
    ObjReader.Close();
    MyConn.Close();


    Thats all!

    Saturday, January 05, 2008 7:01 AM
  • The main thing that you need to know the ADO.NET

     

    use system.data.oledb

     

    This is the sample code for connecting with ACCESS

     

    Code Block

    OleDbConnection con;   // create connection
     OleDbCommand com;  // create command
     OleDbDataReader dr;  //Dataread for read data from database
     con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\mydb.mdb");
     com = new OleDbCommand("Select * from Table1",con);
     con.Open();                                        // open the coinnection
     dr = com.ExecuteReader();

    //now you can store into Gridview
    GridView1.DataSource = dr;
     GridView.DataBind();
     con.Close();  //Close the connection

     

     

    Simply you can use  insertcommand with OledbCommand

     

     

    Hope this will helps you

     

    Saturday, January 05, 2008 12:09 PM
  • Hi,

    When searching the internet, you find lots of articles. Like this one for instance, which gives you some explanation on several classes, and a simple example that connects to an Access database.

    Hope this helps.

     

    Friday, January 04, 2008 12:26 PM

All replies

  • Hi,

    When searching the internet, you find lots of articles. Like this one for instance, which gives you some explanation on several classes, and a simple example that connects to an Access database.

    Hope this helps.

     

    Friday, January 04, 2008 12:26 PM
  • Hi Dear,


    If you want to connect to Access Database.

    You have to follow the following steps:-

    1) First add  "using System.Data.OleDb;" at to top of the CS file.
    2) Create connection string and open connection.
    For access 2007
    string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";

    OleDbConnection MyConn = new OleDbConnection(ConnStr);


               
    3) Open this connection.
    MyConn.Open();

    4) Create object for command and reader to get the data from access database.

      OleDbCommand Cmd =  new OleDbCommand(StrCmd, MyConn);;
      OleDbDataReader ObjReader = Cmd.ExecuteReader();


    5) Now lood through the reader object to get the data

     if (ObjReader != null)
     {

    }

    6) After completing the processing
    ObjReader.Close();
    MyConn.Close();


    Thats all!

    Saturday, January 05, 2008 7:01 AM
  • The main thing that you need to know the ADO.NET

     

    use system.data.oledb

     

    This is the sample code for connecting with ACCESS

     

    Code Block

    OleDbConnection con;   // create connection
     OleDbCommand com;  // create command
     OleDbDataReader dr;  //Dataread for read data from database
     con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\mydb.mdb");
     com = new OleDbCommand("Select * from Table1",con);
     con.Open();                                        // open the coinnection
     dr = com.ExecuteReader();

    //now you can store into Gridview
    GridView1.DataSource = dr;
     GridView.DataBind();
     con.Close();  //Close the connection

     

     

    Simply you can use  insertcommand with OledbCommand

     

     

    Hope this will helps you

     

    Saturday, January 05, 2008 12:09 PM
  • Thank you for these replies sir. I found those useful too. I'm a C# beginner too, hoping to learn database programming too..
    Sunday, October 21, 2012 8:40 AM
  • Thanks for sharing.

    It helped me


    Deepak Chhirang

    Friday, November 02, 2012 7:07 AM
  • ya it works... thanx
    Friday, November 02, 2012 7:27 AM