Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Example: how to query a MS Access database

Hello

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

Thanks.
0
bussd
Asked:
bussd
  • 2
  • 2
2 Solutions
 
bruintjeCommented:
Hi bussd,
----------

you could start with this sample to open a db and run through a resultset
http://www.csharphelp.com/archives/archive70.html

----------
bruintje
share what you know, learn what you don't
0
 
SergulathCommented:
using System;
using System.Data;
using System.Data.OleDb;

class Test
{
    public static void Main()
    {
        /* You need to have -> c:\Northwind.mdb */
        OleDbConnection _connection;
        try
        {
            _connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Northwind.mdb");
            _connection.Open();
        }
        catch (OleDbException e)
        {
            Console.WriteLine(e.Message);
            return;
        }
        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"]);
        }
        _reader.Close();
        _connection.Close();
    }
}
0
 
bussdAuthor Commented:
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.
0
 
bruintjeCommented:
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;
DataGrid1.DataBind();

for other things explain or ask a more detailed question
0
 
bussdAuthor Commented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now