views:

100

answers:

1

I want to gather some data from some tables of an Access Database, I've found some solutions online, but I haven't found ways to fill a datatable, or dataset, and get each single field properly.

Is it easier for me to get whole tables then get just the info that i want, or should I make a lot of searches in the access DB getting just what i Want each time? Any code snippets for it?

info:

  • The Access Database is in an ACCDB file, with no user or password
  • I'm currently using VB.NET, but it doesn't matter if you answer in C#

--[EDIT]--
Sub question:
http://stackoverflow.com/questions/2373355/connecting-to-accdb-format-ms-access-database-through-oledb

+2  A: 

From here, you use the OleDbDataReader:

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

class MainClass
{
  static void Main(string[] args)
  {
    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\\Northwind.mdb";

    OleDbConnection conn = new OleDbConnection(connectionString);

    string sql = "SELECT * FROM Orders";

    OleDbCommand cmd = new OleDbCommand(sql, conn);

    conn.Open();

    OleDbDataReader reader;
    reader = cmd.ExecuteReader();

    while (reader.Read()) 
    {
      Console.Write(reader.GetString(0).ToString() + " ," );
      Console.Write(reader.GetString(1).ToString() + " ," );
      Console.WriteLine("");
    }

    reader.Close();
    conn.Close();
  }
}
Kyle Rozendo
what if the db is in .ACCDB format ?
MarceloRamires
They should work the same. Have you tried it?
Kyle Rozendo
Still applying, ass soon as I complete it i'll post in here. I'm migrating from other DB, so I have to take each data (connstr, location) from somewhere different. Thank you, by the way =)
MarceloRamires
The connection string should include Microsoft.ACE.OLEDB.12.0 not Microsoft.JET.OLEDB.4.0 (http://www.connectionstrings.com/access-2007)
Remou
It works. But what if i want to call the field from it's name, not from it's ordinal position in the table ? (without selecting only the field)
MarceloRamires
You can't directly. What you can however do is set up an Enum with the correct name->ordinal lookup, but this can get messy.
Kyle Rozendo
Thank you. It actually didn't work with ACCDB, only with .MDB (damn) check question linked in the edit.
MarceloRamires
The Jet 4 provider won't work with ACCDB. The answer should be edited to use the ACE provider since it's quite clearly stated in the original question that it's an ACCDB not MDB.
David-W-Fenton