Step by Step on How to Connect Microsoft Access Database in C#

In this tutorial, im going to show you how to connect Microsoft Access Database in C#. First we should create a new database in MS Access named “usermgt”, then create new table named “tbluseraccounts” and the design the table fields that looks like as shown below. a1 After this step, save the table and add one record to the table and it should look like as shown below. a2 At this time, we will now create a new project in C#, for me I am using Visual studio 2008. Then here’s my step on how to create a C# in visual studion 2008. First open the visual studio and click file then choose new project. a3 And on the new project, follow the all the step based on the figure shown below. a4 By the way folks in this application we are creating a “User Management System”. After you performed all the steps above, you may now design your application that looks like as the figure shown below. a5 At this time, before we proceed on adding the code for connecting the Database in C# we will copy first the database file inside the bin folder of our project. So that it will be easy for us later to connect it to our application. After of all the steps weve done, I think its time for us to move to our main topic on how to connect the database in C#. First double click the form. And add this “using System.Data.OleDb;” single line of code above “namespace WindowsFormsApplication1” and it should be look like as shown below. a6 The new added code is a .NET Framework Data Provider for OLE DB describes a collection of classes used to access an OLE DB data source in the managed space. Our next step is to set a new variable named “dt” as Datatable that will be used later on. And in this step also, we will connect to the server and connect to the database named “usermgt”. And the code will look like as shown below.
  1. public partial class Form1 : Form
  2. {
  3. //declare new variable named dt as New Datatable
  4. DataTable dt = new DataTable();
  5. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  6. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application .StartupPath + "/usermgt.mdb";
  7. OleDbConnection conn = new OleDbConnection(connection);
  8.  
  9. public Form1()
  10. {
  11.  
  12. InitializeComponent();
  13. }
  14.  
  15. private void Form1_Load(object sender, EventArgs e)
  16. {
  17.  
  18. }
  19.  
  20.  
  21. }
Then at this time, we’re going to add code for the ”Load record” button. To do this, double click the button and add the following code.
  1. loadrecord();//calling the function Loadrecord
This code wre added is a function that will load all the record from our database to the datagridview. And we will now add a code for the our function named “loadrecord()”. And here’s the following code:
  1. private void loadrecord()
  2. {
  3. string sql = "Select * from tbluseraccounts";
  4. OleDbDataAdapter da = new OleDbDataAdapter(sql , conn);
  5. da.Fill(dt);
  6. dataGridView1.DataSource = dt;
  7. }
Then here’s all the code for this application.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.OleDb;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13. public partial class Form1 : Form
  14. {
  15. //declare new variable named dt as New Datatable
  16. DataTable dt = new DataTable();
  17. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  18. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application .StartupPath + "/usermgt.mdb";
  19. OleDbConnection conn = new OleDbConnection(connection);
  20.  
  21. public Form1()
  22. {
  23.  
  24. InitializeComponent();// calling the function
  25. }
  26.  
  27. private void Form1_Load(object sender, EventArgs e)
  28. {
  29.  
  30. }
  31. private void loadrecord()
  32. {
  33. string sql = "Select * from tbluseraccounts";
  34. OleDbDataAdapter da = new OleDbDataAdapter(sql , conn);
  35. da.Fill(dt);
  36. dataGridView1.DataSource = dt;
  37. }
  38.  
  39. private void button1_Click(object sender, EventArgs e)
  40. {
  41. loadrecord();
  42. }
  43.  
  44.  
  45. }
  46. }
And after completing all the steps above. You may now press “F5” or start button. And when the user clicked the “Load record” Button, it will look like as shown below. a7

Add new comment