将Access DB表加载到Datatable

时间:2010-03-03 19:16:22

标签: c# .net ms-access oledb

我有一个.ACCDB格式的数据库和一些表格。

我使用以下代码成功将其加载到OleDbDataReader中:

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();

我想将表“clientes”加载到数据表中。我该怎么办?

1 个答案:

答案 0 :(得分:24)

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);
}