【问题标题】:query MS access database with LINQ in c# [duplicate]在c#中使用LINQ查询MS access数据库[重复]
【发布时间】:2011-11-22 16:32:10
【问题描述】:
【问题讨论】:
标签:
c#
database
linq
visual-studio-2008
ms-access-2003
【解决方案1】:
很遗憾 LINQ 不支持访问数据库。作为解决方法,您可以使用 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 to object 来做你的查询(即使我不建议你使用这种方法,因为性能不是很好)
var results = from myRow in dTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
【解决方案2】:
These 伙计们开发了一个 Linq2Access 项目。我从未使用过它,也不知道它的质量或成熟度,但它可能值得一试。