日期:2014-05-17  浏览次数:20884 次

能否将两个Access数据中的两个表串到一起?
在我的DataSet中,有如下结果的两个DataTable

TableA
料号 总需求量
-----------------------
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语句连到一起,大家看看有没有什么好的方法。





------解决方案--------------------
建一个临时表
------解决方案--------------------
select A.总需求量,B.总领料数 from TableA as A inner join TableB as B on A.料号=B.料号 

------解决方案--------------------
只能是新加一列然后自己找到对应的值插入进去了,应该没有什么自动的方式。
C# code

using System.Collections.Generic;
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,