ADO.NET and Access database I

illustration that depicts the process of connecting to an Access database using ADO.NET in Visual C#.
How to connect to an Access database from Visual C# .NET, retrieve data and display it using a DataGrid with ADO.NET, using a dataSet.

In the tutorial I assume you already know the basics of the SQL language.

First download the database we will use in this example and save it somewhere:

Start a new ‘Windows Application’ project named ‘accessDB’.

From the View menu of Microsoft Visual C# .NET select Server Explorer, or press Ctrl+Alt+S.

From the list right-click Data Connections and choose ‘Add Connection…’.

In the ‘Provider’ tab select Microsoft Jet 4.0 OLE DB Provider (used for connecting to an Access database), and click Next.

Use the ‘…’ button to browse for an Access Database and choose the database you have downloaded, db1.mdb. After clicking OK, test the connection by clicking the ‘Test Connection’ button.

It should say ‘Test connection succeeded.’.

Press OK and a window pops up that says ‘Please Enter MS JET OLE DB Initialization Information’.

Leave the defaults and just press OK.

Just like you see in the above screenshot, you can browse the Access file database. Yet the database is not connected to our program. For this you need to drag the node below ‘Data Connections’ on the form.

The node is named using the form ‘ACCESS.X:\PathToYourDatabase\file.mdb.Admin’. As I said, drag it on the form and ‘oleDbConnection1’ should appear below the form:

Next open the Toolbox (Ctrl+Alt+X) and from the ‘Data’ group drag an ‘OleDbDataAdapter’. The ‘Data Adapter Configuration Wizard’ starts.

Clicking next will take you to the part where you need to select the connection you wish to use. Choose the connection we have just created (ends up in db1.mdb.Admin). Press Next and then again Next (leave the default ‘Use SQL statements’).

Now you are beeing asked ‘What data should the data adapter load in the dataset?’. We want all the tables and all the columns therefore we need to take the following steps. Open the Query Builder using the button and you should now be able to add the two tables named ‘books’ and ‘categs’. Add them and close the small window and now we have two small windows representing the two tables. We want to select all the columns, therefore check ‘* (All Columns)’ on both tables.

The following SQL query is created:

SELECT books.*, categs.*

FROM (books INNER JOIN

categs ON books.ID = categs.ID)

We could also do it more simple by typing ‘SELECT * FROM books, categs’… it would have been the same.

Press OK to exit the Query Builder and then press Finish.

Now add a DataSet to our application by dragging one from the Data group (Toolbox). Choose ‘Untyped DataSet (no schema)’.

DataSets are used to store the query results with which we later populate the DataGrid.

Before we start coding we need something where we display the results. The DataGrid is perfect for that, so drag one from the Toolbox, the Windows Forms group.

Now doubleclick the form so we can go to the ‘Form1_Load’ event.

First we should set the SQL query:

oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM books, categs where books.bookCateg = categs.ID";

Next we clear the DataSet preparing it for the operation

dataSet1.Clear();

Next we use the ‘Fill()’ method of oleDbDataAdapter1 to fill the dataSet1 with the result of the SQL query:

oleDbDataAdapter1.Fill(dataSet1, "Books");

Finally we bind it using the DataGrid:

dataGrid1.SetDataBinding(dataSet1, "Books");

Here is all the code we wrote in one place:

private void Form1_Load(object sender, System.EventArgs e)
{

oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM books, categs where books.bookCateg = categs.ID";

dataSet1.Clear();

oleDbDataAdapter1.Fill(dataSet1, "Books");

dataGrid1.SetDataBinding(dataSet1, "Books");

}

ADO.NET and Access database II

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top