Example:  how to query a MS Access database

Posted on 2006-06-07
Last Modified: 2010-04-16

Could someone please provide me with a simple example of how to query a MS Access database from a C# program.  

Question by:bussd
    LVL 44

    Accepted Solution

    Hi bussd,

    you could start with this sample to open a db and run through a resultset

    share what you know, learn what you don't

    Assisted Solution

    using System;
    using System.Data;
    using System.Data.OleDb;

    class Test
        public static void Main()
            /* You need to have -> c:\Northwind.mdb */
            OleDbConnection _connection;
                _connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Northwind.mdb");
            catch (OleDbException e)
            OleDbCommand _command = _connection.CreateCommand();
            _command.CommandText = "SELECT CustomerID , CompanyName FROM Customers";
            OleDbDataReader _reader = _command.ExecuteReader();
            while (_reader.Read())
                Console.WriteLine("\t{0}\t{1}", _reader["CustomerID"], _reader["CompanyName"]);

    Author Comment

    Thanks to both!

    Bruintje, I used the examlple at your link and got it working but now I want to make the contents of this query available to many objects throughout my app.  Any idea how I'd do that?

    I have a singleton class and my idea is to keep a DataSet (or maybe some other object?) as a public static property and somehow copy the data from the reader into the DataSet.  Problem is that I'm not sure how to do this, or even if it's a good idea.

    Sorry if this is very basic... I'm totally new to C# and I'm trying to learn all I can while climbing a steep learning curve.
    LVL 44

    Expert Comment

    that's a very broad addition to your question

    if you need a grid  you can set the datasource property of the grid to the datareader

    DataGrid1.DataSource = datareader;

    for other things explain or ask a more detailed question

    Author Comment

    Sorry, I'll try to be more specific.

    The problem is that the data reader only exists for a short time inside one function.  Essentially, I want to keep a copy of a table from my database available in memory as long as my program is running.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 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

    23 Experts available now in Live!

    Get 1:1 Help Now