2014-11-09 53 views
0

我在Microsoft Visual Studio中配置了accessdb数据源,我有一个数据集和一个名为“承包商”的数据表。如何在表单中搜索这些数据?更重要的是,如何在代码中引用此表?选择数据表中的数据

p.s.我已经在google上搜索关于数据表的所有东西,所有示例都显示在本地实例化一个数据表,然后添加数据,然后搜索它而不是引用已存在的数据。所以我做了尽职调查。需要帮忙。

感谢

+0

。执行asEnumerable()扩展数据库使用OleDbCommand和OleDbDataReader

using (System.Data.OleDb.OleDbConnection oledbConnection = new System.Data.OleDb.OleDbConnection(ConnectionString)) { oledbConnection.Open(); using (System.Data.OleDb.OleDbCommand getData = new System.Data.OleDb.OleDbCommand()) { getData.CommandText = "select * from table "; getData.Connection = oledbConnection; using (System.Data.OleDb.OleDbDataReader readData = getData.ExecuteReader()) { if (readData.HasRows) { while (readData.Read()) { // get the data here } } } } 

你有迄今为止尝试过的代码样本?你使用'OleDbDataReader'吗? – grovesNL 2014-11-09 08:09:13

+0

这是我的配置是:connectionString =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = | DataDirectory | \ Phase2.accdb” providerName =“System.Data.OleDb”我通过向导我做到了这一点想要在代码中的表“承包商”上执行SQL,虽然不是DataGridViews – 2014-11-09 08:15:10

+0

这就是连接字符串。关于从数据库中查询(读取)数据的代码呢? – Tim 2014-11-09 08:16:04

回答

0

如果您想查询,如果你想查询一个数据表的使用数据表

var readData = from currentRow in theDataTable.AsEnumerable() 
where currentRow.Field<int>("RowNo") == 1 
select currentRow;