Solved

c#2010 connect to access database

Posted on 2013-01-17
5
505 Views
Last Modified: 2013-01-17
Hi

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


Cheers
0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

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

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

Becomes

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"
0
 
LVL 15

Expert Comment

by:angus_young_acdc
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");
                    connection.Open();
                    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
                        }
                    }
                    connection.Close();

Open in new window

0
 

Author Comment

by:cycledude
ID: 38787857
Thanks angus

I get a problem on the line

Reader = cmd.executereader ()


Invalidoperationexception

Executereader connection property has not been initialised

Any ideas?
0
 
LVL 15

Accepted Solution

by:
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;
0
 

Author Closing Comment

by:cycledude
ID: 38788375
dude, you are a star!
thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 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