C# 将访问数据库表加载到数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2374221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:52:11  来源:igfitidea点击:

Loading Access DB Table to Datatable

c#.netms-accessoledb

提问by Marcelo

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

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

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

我使用以下代码成功将其加载到 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();

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

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

采纳答案by Justin Niessner

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