在 C# 中使用 LINQ 查询 MS Access 数据库

发布于 2024-12-06 02:23:34 字数 590 浏览 0 评论 0原文

可能的重复:
使用 LINQ 和 C# 查询 Microsoft Access MDB 数据库 < /p>

使用 MS Access 数据库并想知道是否可以使用 LINQ 查询该数据库?我阅读了有关数据集的内容,但通过阅读以下内容: http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx 我发现没有多少数据库可以通过数据集访问。谁能帮我解决这个问题?谢谢 :)

Possible Duplicate:
Query Microsoft Access MDB Database using LINQ and C#

Im working with MS access database and wanted to know if i could use LINQ to query this database? I read about datasets, but by reading this: http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx
i see that not much of a database can be accesed through datasets. Can anyone help me as to how i could go bout this? Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-12-13 02:23:34

不幸的是LINQ不支持Access数据库。作为解决方法,您可以使用 Ado.net DataSet 从数据库中提取数据

//create the connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb";

//create the database query
string query = "SELECT * FROM MyTable";

//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

//create a DataTable to hold the query results
DataTable dTable = new DataTable();

//fill the DataTable
dAdapter.Fill(dTable);

然后您可以使用 LINQ 来反对执行查询(即使我不会建议您使用这种方法,因为性能不是很好)

var results = from myRow in dTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;

Unfortunately LINQ does not support access database. As work around you could use Ado.net DataSet to pull out the data from your database

//create the connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb";

//create the database query
string query = "SELECT * FROM MyTable";

//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

//create a DataTable to hold the query results
DataTable dTable = new DataTable();

//fill the DataTable
dAdapter.Fill(dTable);

Then you can use LINQ to object to do your query (even if I won’t recommend you to use this approach because of the performance are not very good)

var results = from myRow in dTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
溺孤伤于心 2024-12-13 02:23:34

这些人开发了一个 Linq2Access 项目。我从未使用过它,也不知道它的质量或成熟度,但它可能值得一试。

These guys have developed a Linq2Access project. I have never used it and have no idea of the quality or maturity but it's probably worth checking out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文