我整天都被困在这......我对能解决它的人的爱!!!!
所以,有2个表
在(ds.Tables [“Access”])中的访问表,其中包含
的列
条形码,描述,仓库中的数量等
和(ds.Tables [“Excel”]中的Excel表)包含
的列
条形码,量化有序等,但命名为F2,F4,F8
那么如何在条形码上匹配Excel和Access匹配最终的DataTable / DataSet(我可以在DataGridView上使用.DataSource的任何内容)
OleDbConnection conAccess = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\PROGRAMMING.LAB\\geekx_recent.mdb");
OleDbDataAdapter daAccess = new OleDbDataAdapter("Select Barcode AS BARCODE, quantity AS QTY_WH From Stock", conAccess);
DataSet dsAll = new DataSet();
daAccess.Fill(dsAll, "Access");
OleDbConnection conExcel = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\PROGRAMMING.LAB\\order.xlsx;Extended Properties = Excel 12.0 Xml");
OleDbDataAdapter daExcel = new OleDbDataAdapter("SELECT F2 AS BARCODE, F8 AS ORDER_CUST FROM [phil$] WHERE ISNUMERIC(F8)",conExcel);
daExcel.Fill(dsAll, "Excel");
DataRelation dr = dsAll.Relations.Add("Excel_Access", dsAll.Tables["Excel"].Columns["BARCODE"], dsAll.Tables["Access"].Columns["BARCODE"], false);
DataTable dtExcel = dsAll.Tables["Excel"];
DataTable dtAccess = dsAll.Tables["Access"];
DataTable dtAll = new DataTable();
var query = from ex in dtExcel.AsEnumerable()
join ac in dtAccess.AsEnumerable()
on ex.Field<string>("BARCODE") equals ac.Field<string>("BARCODE")
select new
{
Order = ex.Field<string>("ORDER_CUST"),
Stock = ac.Field<string>("QTY_WH"),
Barcode = ex.Field<string>("BARCODE")
};
foreach (var q in query)
{
dtAll.Rows.Add(q.Barcode, q.Stock, q.Order);
}
dataGridView1.DataSource = dtAll;
返回
System.Data.DataSetExtensions.dll中发生了未处理的“System.InvalidCastException”类型异常
附加信息:无法将类型为“System.Double”的对象强制转换为“System.String”。
在选择新区块
任何想法?
答案 0 :(得分:0)
请参阅以下代码以及关于加入表格的网页:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
var results =
from c1 in dt1.AsEnumerable()
join c2 in dt2.AsEnumerable()
on c1.Field<int>("ID") equals c2.Field<int>("ID") into cs
select new { Category = c1.Field<string>("ColB"), Fields = cs };