c#2010 connect to access database

Posted on 2013-01-17
Last Modified: 2013-01-17

I am new to c# and gotta develop a Windows appplication.  Needs to connect with access database.

how is this done??

All tutts assume you are using sqlserverr

Question by:cycledude
  • 3
  • 2
LVL 15

Expert Comment

ID: 38787713
To be honest it is basically the same code!  It is generally a case of replace 'Sql' with 'OleDb'

SqlConnection connection = new SqlConnection("My connection string");


OleDbConnection connection = new OleDbConnection("my connection string");

Different namespaces but the syntax for them is generally the same.  The major difference would be in your connection string, but that isn't a big issue either.

For example: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDB.mdb"
LVL 15

Expert Comment

ID: 38787755
Example here, reading from a database with a table called MyTable and two columns one for UserName and one for UserBirthday and you want to read everything in this table.

                    OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDb.mdb");
                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM myTable");
                    cmd.CommandType = System.Data.CommandType.Text;
                    OleDbDataReader reader = default(OleDbDataReader);
                    reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                        while (reader.Read())
                            // do something, eg
                            string myUser = reader["UserName"].ToString();
                            string myBirthday = reader["UserBirthday"].ToString();
                            // Do something else

Open in new window


Author Comment

ID: 38787857
Thanks angus

I get a problem on the line

Reader = cmd.executereader ()


Executereader connection property has not been initialised

Any ideas?
LVL 15

Accepted Solution

angus_young_acdc earned 500 total points
ID: 38788190
Whoops missed a line!

Just after this: cmd.CommandType = System.Data.CommandType.Text;

put this:
cmd.Connection = connection;

Author Closing Comment

ID: 38788375
dude, you are a star!

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
compare between entities in entity framework 8 69
Graphics 2 27
Reference issues in Visual Studio 3 29
Call Controller Action Method from ASPX 2 13
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now