在我的DataSet中,有如下结果的两个DataTableTableA
料号 总需求量
-----------------------
A-001 1000
A-002 500
A-003 1200
....TableB
料号 总领料数
--------------------
A-001 800
A-002 300
A-003 1000
....我有没有办法得到一个DataTable料号 总需求量 总领料数
--------------------------------
A-001 1000 800
A-002 500 300
A-003 1200 1000
....因为我的TableA和TableB分别是从两个Access中的表,所以没办法用SELECT语句连到一起,大家看看有没有什么好的方法。
料号 总需求量
-----------------------
A-001 1000
A-002 500
A-003 1200
....TableB
料号 总领料数
--------------------
A-001 800
A-002 300
A-003 1000
....我有没有办法得到一个DataTable料号 总需求量 总领料数
--------------------------------
A-001 1000 800
A-002 500 300
A-003 1200 1000
....因为我的TableA和TableB分别是从两个Access中的表,所以没办法用SELECT语句连到一起,大家看看有没有什么好的方法。
using System.Collections;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System;
using System.Data;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DemonstrateMergeTable();
} private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items"); // Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn); // Set the primary key column.
table1.PrimaryKey = new DataColumn[] { idColumn }; // Add RowChanged event handler for the table.
table1.RowChanged += new
System.Data.DataRowChangeEventHandler(Row_Changed); // Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
} // Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values"); // Create a second DataTable identical to the first.
DataTable table2 = table1.Clone(); // Add column to the second column, so that the
// schemas no longer match.
table2.Columns.Add("newColumn", typeof(System.String)); // Add three rows. Note that the id column can't be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
table2.Rows.Add(row); row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
table2.Rows.Add(row); row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
table2.Rows.Add(row); // Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2, false, MissingSchemaAction.Add);
PrintValues(table1, "Merged With table1, schema added"); } private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row changed {0}\t{1}", e.Action,
e.Row.ItemArray[0]);
} private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("\t " + row[col].ToString());
}
Console.WriteLine();
}
}
}
}
select TableA.料号 ,TableA.总需求量,TableB.总领料数 from TableA left jion TableB where TableA.料号=TableB.料号字段你自己换成英文的吧
select TableA.料号 ,TableA.总需求量,TableB.总领料数 from TableA left outer jion TableB on TableA.料号=TableB.料号 还有内连接方式:select TableA.料号 ,TableA.总需求量,TableB.总领料数 from TableA , TableB where TableA.料号=TableB.料号 哈哈,不好意思
而在数据查询到dataset中两个表,在程序中合并呢.
在数据库中通过两个表关联查询出结果到dataset中..
这样好实现.
declare @TableA table(料号 varchar(20),总需求量 varchar(20))
declare @TableB table(料号 varchar(20),总领料数 varchar(20)) insert into @TableA
select 'A-001','1000' union all
select 'A-002','500' union all
select 'A-003','1200' insert into @TableB
select 'A-001','800' union all
select 'A-002','300' union all
select 'A-003','1000' select a.料号,a.总需求量,b.总领料数 from @TableA a left join @TableB b
on a.料号=b.料号
-----------------------------------------------------------------------------------------谁说的,Access当然可以实现跨数据库的表联接操作,只是你不知道而已.
SELECT TableA.料号,TableA.总需求量数,TableB.总领料数 FROM [;DATABASE=D:\数据库B.MDB;USER=ADMIN,PWD=;].TableB INNER JOIN TableA ON TableB.料号=TableA.料号
再写SQL选择此查询需求的部份
因为在ACCESS中查询语句只可以连接一个外部MDB文件.具体的帮助文件可参见ACCESS的帮助文件.至于是否可以将SQL语句
全部写在C#中连接两个数据库的表没有试过.
SELECT TableA.料号 ,TableA.总需求量 ,TableB.总领料数 FROM [;DATABASE=D:\***.MDB;USER=ADMIN;PWD=*].TableB INNER JOIN TableA ON TableB.料号=TableA.料号.
这样可以将两个数据库的表链成一个.
再在C#中将此表载入DataSet中