Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Visual C# » Connect to Access Database in C# and ADO.NET

Connect to Access Database in C# and ADO.NET

This artilce explains how to connect and get data from an Access database in C# and ADO.NET.

Page Views : 277806
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

These are very simple steps to create and connect an Access database in C#. 

  • Create Access database (e.g  student)



  • Now open you notepad and click on save As button. Name then Imran.hdl. Change save type "ALL FILES".

  • Now double click on imran.udl file. A wizard will start like this:

  • Click Provider TAB, select Microsoft Jet 4.0 OLE DB (denoted by black arrow) then click next. Now click "Select or enter a database name" and select the desire database then click open.

  • Now click on test connection and click OK
  • Now edit this UDL file with note pad and copy link as shown below



  • Now Open Visual Studio editor and create Text boxes and buttons like

  • Double click on the form and add some code

cmd.CommandText = "insert into student values(" + this.textBox1.Text + ",'" + this.textBox2.Text + ",'" + this.textBox3.Text + ",'" + this.textBox4.Text + "');";
int temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
    MessageBox.Show("Record Added");
}
else
{
    MessageBox.Show("Record not Added");
}
mycon.Close();

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by Edgar Picuasi on Jun 15, 2011
gracias
Contents added by dsfdsf dsfdsf on Nov 14, 2009

hx

Contents added by trushant pachnekar on Jul 10, 2009
dfgdfgfdgfd
Contents added by Kirk Vilaimal on May 25, 2009
Hi, I'm Kirk  just  beginner
 [Top] Rate this article
 
 About the author
 
Muhammad Imran Taj
I am imran. I have done Masters degree from Hazara University Pakistan. Now i am working with United Nations Habitat as a Database Assistant
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
hi sir by Naeem On October 11, 2008
assallam wallekum bhai jaan well hor r u ............. its nice artical and very help full for me and thenx to u sir........... sir i needs some more help if u have time na then plz tell me
Reply | Email | Modify 
ERROR by George On March 29, 2009
The CODE: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace InterfataBaza { public partial class Form1 : Form { private OleDbConnection mycon; public Form1() { mycon=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\george\My Documents\studenti.mdb;Persist Security Info=False"); InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { cmd.CommandText = "insert into student values(" + this.textBox1.Text + ",'" + this.textBox2.Text + ",'" + this.textBox3.Text + ",'" + this.textBox4.Text + "');"; int temp = cmd.ExecuteNonQuery(); if (temp > 0) { MessageBox.Show("Record Added"); } else { MessageBox.Show("Record not Added"); } mycon.Close(); } } } The ERRORS: Error 1 The name 'cmd' does not exist in the current context Error 2 The name 'cmd' does not exist in the current context
Reply | Email | Modify 
how to remove error of cmd by jay On April 4, 2009
kindly answer
Reply | Email | Modify 
Re: how to remove error of cmd by George On April 12, 2009
Where is the answer ?

Reply | Email | Modify 
Beginner by Kirk On May 25, 2009
Hi  I'm  Kirk,  just  beginner
Reply | Email | Modify 
con problem solved by zain On August 15, 2009
con is here command
private oledbcommand con;
its solved

Reply | Email | Modify 
Re: con problem solved by Anthony On March 9, 2010
How was the cmd error solved?
Reply | Email | Modify 
Re: con problem solved by elizabeth On May 11, 2010
how is it solved. kindly define.
Reply | Email | Modify 
Fix by d On June 27, 2010
The answer for the cmd error is that the coding is wrong, he never defined the 'cmd' variable to begin with.

To fix this, add this line below where you have the connection string (The thing that starts with 'Provider=Microsoft.Jet')

OleDbCommand cmd = new OleDbCommand();

This defines cmd as an OleDbcommand, which will solve the error.
He also has not defined the command to have a connection string, so add this below that line also:

cmd.Connection = myCon;

And also, you need to open the connection first. Add this above the "int temp = cmd.ExecuteNonQuery()" line...
myCon.Open();
Reply | Email | Modify 
Fix by d On June 27, 2010
Just to add, the fixed code will look like this (I used one of my own Database's for this example):

OleDbCommand cmd = new OleDbCommand();
OleDbConnection myCon = new OleDbConnection();
myCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\Program Files\SongsDB.mdb";
cmd.CommandText = "insert into student values(" + this.textBox1.Text + ",'" + this.textBox2.Text + ",'" + this.textBox3.Text + ",'" + this.textBox4.Text + "');";
cmd.Connection = myCon;
myCon.Open();
int temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
    MessageBox.Show("Record Added");
}
else
{
    MessageBox.Show("Record not Added");
}
myCon.Close();
Reply | Email | Modify 
:)) by Martin On July 8, 2010
SQL injection ?!?!?
Reply | Email | Modify 
Can you have a customised query with the LIKE %....% pattern in your C# application using Access 2003 ? by eugene On September 22, 2010
Hello Imran,
According to your example, can you have a customised query with the:
WHERE .... LIKE %something%
pattern in your C# application using Access 2003 ?

Thanks for your help.
Reply | Email | Modify 
i want to submit projecton on asp.net by manoj On October 8, 2010
i have to submit project within 15 days ...i have submited synopsis on online evevt mangement so plz help me...main bahut tension main hu...my mail id u knoe it but again write here manse000@gmail.com.if u have this one project plz send on this email.....and thanks for database
Reply | Email | Modify 
Annoying Exception by Ekene On November 7, 2010
Please, someone should help me out. I'm getting an annoying Unhandled exception(System.NullReferenceException: Object reference not set to an instance of an object.)
Reply | Email | Modify 
Re: Annoying Exception by d On November 11, 2010
Post your code it will help to figure out where the issue lies
Reply | Email | Modify 
Re: Annoying Exception by Ekene On November 23, 2010
Alright Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Registration
{
    public partial class Form1 : Form
    {
        private OleDbConnection myCon;
        public Form1()
        {
            String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Aykaynay\Documents\Access Db\Reg_db.mdb;Persist Security Info=False";
            OleDbConnection myCon = new OleDbConnection(connString);
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myCon.Open();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            OleDbCommand cmd = new OleDbCommand("INSERT INTO Registration VALUES ('" + this.txtUsername.Text + "', '" + this.txtPassword.Text + "')",myCon);
            cmd.Connection = myCon;
           
            int temp = cmd.ExecuteNonQuery();
            if (temp > 0)
            {
                MessageBox.Show("Record Added");
            }
            else
            {
                MessageBox.Show("Record not Added");
            }
            myCon.Close();
        }
    }
}
Reply | Email | Modify 
Re: Re: Annoying Exception by d On December 8, 2010
What line does it occur at? Your creating two myCon objects, remove the private version.
Reply | Email | Modify 
Than ks by Raghav On December 8, 2010
ITs really nice But my problem is I want to create user DSN through C# code for my windows application for SQl server 2005
Reply | Email | Modify 
Re: Than ks by d On December 8, 2010
Explain it in more detail please.
Reply | Email | Modify 
About Search Query!!! by Raza On December 8, 2010
Assalam O Alaikum! Imran bhai I was looking article on Search query but I found your article which very helpful to me. If you know about Search query how to make so plz post or email me. Raza_mrmq2009@yahoo.com Regards Raza Qureshi.
Reply | Email | Modify 
** by a On February 9, 2011
thank you for source code.
Reply | Email | Modify 
THank Imran Sir by Vilas On March 15, 2011
Sir I heartly say to u... THANKING YOU... because i learn such important coding from your article.....:)
Reply | Email | Modify 
I have a question by Russell On March 30, 2011
In this article, it says to create a blank file and save it as "Imran.udl", then double-click on it to get the "Data Link properties" window to show up. Is there a step missing here? Is there something that needs to be typed into the *.udl file before saving it? (When I double-click on the file, it just opens up in Notepad again.)
Reply | Email | Modify 
TO RUSSEL by Vilas On April 9, 2011
RUSSEL SIR U JUST GO STEP BY STEP.. U WILL SOLVE YOUR QUIRES. THANKS
Reply | Email | Modify 
Re: TO RUSSEL by Russell On May 17, 2011
...but double-clicking on an empty file didn't get me anywhere!
Reply | Email | Modify 
hi by ben On June 1, 2011
i wanted to connect a web form log in with ms access, help needed pls. thank u
Reply | Email | Modify 
Microsoft Jet 4.0 OLE DB is not seen infile.udl saved in OD Windows 7 by Molutty On June 6, 2011
Microsoft Jet 4.0 OLE DB is not seen infile.udl saved in OD Windows 7
Reply | Email | Modify 
Microsoft Jet 4.0 OLE DB is not seen infile.udl saved in OD Windows 7 by Molutty On June 6, 2011
Microsoft Jet 4.0 OLE DB is not seen infile.udl saved in OD Windows 7
Reply | Email | Modify 
Microsoft Jet 4.0 OLE DB is not seen infile.udl saved in OD Windows 7 by Molutty On June 6, 2011
Microsoft Jet 4.0 OLE DB is not seen infile.udl saved in OD Windows 7
Reply | Email | Modify 
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.