How To Connect MS Access With Windows Form

How to connect MS Access (mdb ) file with windows form.

Step 1

Create a mdb file in MS Access as follows:
  • Open MS Access
  • Choose Blank Database
  • Save it as Microsoft Office Access Database (2000) format 
Hence your mdb file is created.
 
Step 2

Create a table in your database.
 
 
 
Step 3

Create a Windows form with a button for connectivity.
 
Step 4

Import system.Data.OleDb
  1. using System.Data.OleDb;  
 Now on button click event connect with MS Access:
  1. using System.Data.OleDb;  
  2.   
  3. namespace WindowsFormsApplication3  
  4. {  
  5.     public partial class Form1 : Form  
  6.     {  
  7.         public Form1()  
  8.         {  
  9.             InitializeComponent();  
  10.         }  
  11.   
  12.         OleDbConnection conn;  
  13.         private void btnConnect_Click(object sender, EventArgs e)  
  14.         {  
  15.   
  16.             var DBPath = Application.StartupPath + "\\Connection.mdb";  
  17.             conn = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + DBPath);  
  18.             conn.Open();  
  19.             System.Windows.Forms.MessageBox.Show("Connected successfully");  
  20.   
  21.         }  
  22.     }  
  23. }  
Hence your connection is established,