在我的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语句连到一起,大家看看有没有什么好的方法。

解决方案 »

  1.   

    select A.总需求量,B.总领料数 from TableA as A inner join TableB as B on A.料号=B.料号 
      

  2.   

    只能是新加一列然后自己找到对应的值插入进去了,应该没有什么自动的方式。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,
                    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();
                }
            }
        }    
    }
      

  3.   

    利用sql 连接查询 生成新表
    select TableA.料号 ,TableA.总需求量,TableB.总领料数 from TableA left jion TableB where TableA.料号=TableB.料号字段你自己换成英文的吧
      

  4.   

    更正一下:
    select TableA.料号 ,TableA.总需求量,TableB.总领料数 from TableA left outer jion TableB on TableA.料号=TableB.料号 还有内连接方式:select TableA.料号 ,TableA.总需求量,TableB.总领料数 from TableA , TableB where TableA.料号=TableB.料号 哈哈,不好意思
      

  5.   

    为什么不在数据库中实现为,
    而在数据查询到dataset中两个表,在程序中合并呢.
    在数据库中通过两个表关联查询出结果到dataset中..
    这样好实现.
      

  6.   


    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.料号
      

  7.   

    因为我的TableA和TableB分别是从两个Access中的表,所以没办法用SELECT语句连到一起,大家看看有没有什么好的方法。 
    -----------------------------------------------------------------------------------------谁说的,Access当然可以实现跨数据库的表联接操作,只是你不知道而已.
      

  8.   

    在其中一个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#中连接两个数据库的表没有试过.
      

  9.   

    在一个ACCESS中做成以下查询
    SELECT TableA.料号 ,TableA.总需求量 ,TableB.总领料数 FROM [;DATABASE=D:\***.MDB;USER=ADMIN;PWD=*].TableB INNER JOIN TableA ON TableB.料号=TableA.料号.
    这样可以将两个数据库的表链成一个.
    再在C#中将此表载入DataSet中
      

  10.   

    hao tie  zi ,xiexie