2011-09-22 102 views
1

可能重複:
Query Microsoft Access MDB Database using LINQ and C#查詢MS Access數據庫在C#

林與MS Access數據庫的工作,並想知道如果我可以使用LINQ查詢這個數據庫?我讀了關於數據集,但通過閱讀:http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx 我看到,沒有太多的數據庫可以通過數據集。任何人都可以幫助我,我可以怎麼回合呢?謝謝:)

+0

請閱讀:http://stackoverflow.com/questions/295772/query-microsoft-access-mdb-database-using-linq-and-c – adatapost

回答

3

不幸的是LINQ不支持訪問數據庫。隨着工作你周圍可以使用Ado.net數據集從數據庫

//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; 
1

These大家已經開發了一個Linq2Access項目。我從來沒有用過它,也不知道質量或成熟度,但可能值得一試。