Hi, I have developed my program with three windows forms and would like to bind to the Access database to read data from particular tables, How do I bind?

Recommended Answers

All 2 Replies

Use ADO.NET!

You can't ignore ADO.NET or avoid it when building applications that consume data in both contexts I mean Windows forms and web forms

ADO.NET offers two Modes to deal with data
The connected mode witch means that we are connected directly with the database
I give u a piece of code that illustrate this case:
suppose that we will connect to a data base "C:\Test.mdb" in order to consume data from a table within it called "Table"

OleDbConnection myConnection = new OleDbConnection(/*This is the connection string*/@"Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\Test.mdb");
            myConnection.Open();
            OleDbCommand myCommand = new OleDbCommand("Select * From Table", myConnection);
            OleDbDataReader myReader = myCommand.ExecuteReader();
            try
            {
                while (myReader.Read())
                {
                    MessageBox.Show(myReader[0].ToString());

                }
            }
            finally
            {
                myReader.Close();
            }
            myConnection.Close();

This DataReader is used to only read forward data directly from the data base

The disconnected mode is the most used by the programmers it consists on loading data to an xml files format named DataSets cached in the computed memory and the connection then is released you deal then with cached data, once you finished you can update your data base from the cached data in the datasets and those code lines help you to perform the deal

OleDbConnection myConnection = new OleDbConnection(/*This is the connection string*/@"Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\Test.mdb");
            myConnection.Open();
            OleDbCommand myCommand = new OleDbCommand("Select * From Table", myConnection);
OleDbConnection myConnection = new OleDbConnection(/*This is the connection string*/@"Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\Test.mdb");
            myConnection.Open();
            OleDbCommand myCommand = new OleDbCommand("Select * From Table", myConnection);
            OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand);
            DataSet myDataSet = new DataSet();
            try
            {
                myAdapter.Fill(myDataSet);
            }
            catch (Exception caught) { }
            myConnection.Close();

After entering changes on myDataSet during the a given session an you want to save modification to your database use this framgement of code

try
            {
                myAdapter.Update(myDataSet);
            }
            catch (Exception caught) { }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.