Loading Access DB Table to Datatable
Asked Answered
O

1

12

I have a database in .ACCDB format with some tables.

I'm successfully loading it into an OleDbDataReader with the following code:

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\\marcelo.accdb";

OleDbConnection conn = new OleDbConnection(connectionString);

string sql = "SELECT * FROM Clientes";

OleDbCommand cmd = new OleDbCommand(sql, conn);

conn.Open();

OleDbDataReader reader;

reader = cmd.ExecuteReader();

I'd like to load the table "clientes" to a datatable instead. How should I do it ?

Occupier answered 3/3, 2010 at 19:16 Comment(0)
G
29
string connString = 
    "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\\marcelo.accdb";

DataTable results = new DataTable();

using(OleDbConnection conn = new OleDbConnection(connString))
{
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM Clientes", conn);

    conn.Open();

    OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

    adapter.Fill(results);
}
Gollin answered 3/3, 2010 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.