1

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 :)

Community
  • 1
  • 1
puzzled
  • 57
  • 2
  • 8

2 Answers2

3

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;
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
1

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.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79