所以我最近问过以下问题:
Reading A Fixed Format Text File - Part 2
Reading A Fixed Format Text File
我终于可以使用以下代码读取访问数据库:
string DATABASE_PROVIDER = "Provider=Microsoft.ACE.OLEDB.12.0";
string CVS Application.StartupPath + ""\\Database.accdb";
string DATA_SOURCE = "Data Source" + CVS;
string connectionString = DATABASE_PROVIDER + DATA_SOURCE;
string TABLE = " FROM STUFF";
string SELECT = "SELECT CODE, NAME, ICON, FUNCTION;
string StringQueryCmd = SELECT + TABLE;
OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbCommand Command = OleDbCommand(StringQueryCmd,MyConnection);
OleDbAdapter MyDataAdapter = new OleDbAdapter(Command);
DataSet MyDataSet = new DataSet();
DataTable MyDataTable = new DataTable();
MyConnection.Open();
MyDataAdapter.Fill(MyDataSet,"STUFF");
MyConnection.Close();
我试图通过使用以下代码块来利用LINQ to DATASET:
var query = (
from order in MyDataTable.AsEnumerable()
where order.Field<int>("CODE") >= 41
select new
{
CODE = order.Field<int>("CODE"),
NAME = order.Field<string>("NAME"),
ICON = order.Field<string>("ICON"),
FUNCTION = order.Field<string>("FUNCTION")
}
);
查询的结果没有返回结果,我应该得到数千个结果,我的查询有任何根本错误吗?还有其他方法我生成一个限制DataTable信息的结果吗?
我需要能够提供一个与“NAME”列匹配的正则表达式的查询(或其他解决方案),并且只能得到几个结果。我将尝试使用正则表达式的能力来对每个名称进行匹配(这是一个布尔语句)。
答案 0 :(得分:2)
实例化后,您尚未通过数据初始化MyDataTable
,您已填写数据集但未填写数据表。所以只需试用MyDataSet.Tables[0]
而不是MyDataTable.AsEnumerable()
// DataSet filled in but data table still empty!
MyDataAdapter.Fill(MyDataSet,"STUFF");