怎样把3张DataTable表合并成1张DataTable表?

解决方案 »

  1.   

    而且使合并后新的Datatable表里面没有重复的数据
      

  2.   

    使用DataRow 一条一条把2,3表里的数据加到1里去
      

  3.   

    ref:http://jiezhi.cnblogs.com/archive/2005/01/05/86838.aspx
      

  4.   

    好像不是很容易,其实那你在用DataAdapter来Fill表的时候,为什么不直接Fill到一张表,然后再处理不是更简单。
      

  5.   

    最简单的就是使用DataSet.Merge 方法,你去msdn上察看该方法有什么用就知道了^_^
      

  6.   

    为什么在sql的时候不合成一个呢?
      

  7.   

    那3张DataTable表只是临时的datatable表
    现在就想把这3张datatable表合并成1张新的datatable表(也就是说把这3张表的数据放到新的datatable表里面去,里面的数据没有重复的)
      

  8.   

    不要用.net客户端来操作作 应该用sql直接操作数据库
    INSERT table_2
        SELECT *
        FROM table_1(把table_1的数据插入table_2)
      

  9.   

    用sql 三表合一union ,再用dataset加載吧
      

  10.   

    http://community.csdn.net/Expert/topic/4391/4391131.xml?temp=.8685877
      

  11.   

    谁给具体代码例子呢?
    3张datatable 合并1张新的datatable
      

  12.   

    别看例子了,这个DataSet.Merge 就可以。
      

  13.   

    用Merge的话要 Table的名字都相同哦
      

  14.   

    的确用DataSet.Merge就可以了(表的名字可以不同)
    MSDN中有现成的例子:
    以下示例创建一个简单的 DataSet,该数据集包含一个表、两个列和十个行。创建了与第一个表几乎相同的第二个 DataTable,二者的区别在于向第二个表中添加了新的 DataColumn。向第二个表中添加两个行,然后将该表合并到 DataSet(preserveChanges 参数设置为 false,missingSchemaAction 参数设置为 MissingSchemaAction.Add)中。
    [C#] 
    private void DemonstrateMergeTableAddSchema(){
       // Create a DataSet with one table, two columns, and ten rows.
       DataSet ds = new DataSet("myDataSet");
       DataTable t = new DataTable("Items");   // Add table to the DataSet
       ds.Tables.Add(t);   // Create and add two columns to the DataTable
       DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
       c1.AutoIncrement=true;
       DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
       t.Columns.Add(c1);
       t.Columns.Add(c2);   // Set the primary key to the first column.
       t.PrimaryKey = new DataColumn[1]{ c1 };   // Add RowChanged event handler for the table.
       t.RowChanged+= new DataRowChangeEventHandler(Row_Changed);   // Add ten rows.
       for(int i = 0; i <10;i++){
          DataRow r=t.NewRow();
          r["Item"]= i;
          t.Rows.Add(r);
       }   // Accept changes.
       ds.AcceptChanges();
       PrintValues(ds, "Original values");   // Create a second DataTable identical to the first, with
       // one extra column using the Clone method.
       DataTable t2 = t.Clone();
       t2.Columns.Add("extra", typeof(string));   // Add two rows. Note that the id column can't be the 
       // same as existing rows in the DataSet table.
       DataRow newRow;
       newRow=t2.NewRow();
       newRow["id"]= 12;
       newRow["Item"]=555;
       newRow["extra"]= "extra Column 1";
       t2.Rows.Add(newRow);   newRow=t2.NewRow();
       newRow["id"]= 13;
       newRow["Item"]=665;
       newRow["extra"]= "extra Column 2";
       t2.Rows.Add(newRow);   // Merge the table into the DataSet.
       Console.WriteLine("Merging");
       ds.Merge(t2,false,MissingSchemaAction.Add);
       PrintValues(ds, "Merged With Table, Schema Added");
    }private void Row_Changed(object sender, DataRowChangeEventArgs e){
       Console.WriteLine("Row Changed " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]);
    }private void PrintValues(DataSet ds, string label){
       Console.WriteLine("\n" + label);
       foreach(DataTable t in ds.Tables){
          Console.WriteLine("TableName: " + t.TableName);
          foreach(DataRow r in t.Rows){
             foreach(DataColumn c in t.Columns){
                Console.Write("\t " + r[c] );
             }
             Console.WriteLine();
          }
       }
    }
      

  15.   

    =======================================
    未处理的“SystemFramework.ExceptionManagement.LogonException”类型的异常出现在 wak.uky.dll 中。其他信息: 未将对象引用设置到对象的实例
    ==========================================
    这是这什么错误?
      

  16.   

    关键还是这三个表的结构是否一样,如一样就直接用DataSet.Merge就行了,如不一样就要特殊处理了
      

  17.   

    表的结构是一样~我最后是把3张datatable表(要对比不同的数据),合并成一张新datatable表,用新的datatable表去更新sql server数据库里面的那张表
      

  18.   

    虽然没有用过dNet,SQL上应该是可行的!
      

  19.   

    楼主为什么在查询时要把数据保存在三个datatable中呢,直接用union到一个datatable中是最方便的了.不过用DataSet.Merge应该是可以的,首先创建一个新的DataTable 对象:
       DataSet ds = new DataSet("myDataSet");
       DataTable t = new DataTable("Items");
       ds.Tables.Add(t);
       DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
       c1.AutoIncrement=true;
       DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
       t.Columns.Add(c1);
       t.Columns.Add(c2);
    注意按照你的表结构来构造新的DataTable,构造完毕后可调用ds.Merge(datatable,false,MissingSchemaAction.Add)方法,应该可以把你的三个表合并到一起了。
      

  20.   

    这样合并可以吗?
    表1是dt1
    表2是dt2
    表3是dt3
    this.myds = new DataSet(); 
    myds.Merge(this.dt1,false,MissingSchemaAction.Add);
    myds.Merge(this.dt2,false,MissingSchemaAction.Add);
    myds.Merge(this.dt3,false,MissingSchemaAction.Add);
      

  21.   

    还有一个问题就是我的DataSet合并后;我合并成的那张表是那一个啊?
    那到是  this.myds.Tables[0].TableName;  吗?
      

  22.   

    表1是dt1
    表2是dt2
    表3是dt3
    this.myds = new DataSet(); 
    myds.Merge(this.dt1,false,MissingSchemaAction.Add);
    myds.Merge(this.dt2,false,MissingSchemaAction.Add);
    myds.Merge(this.dt3,false,MissingSchemaAction.Add);
    是不是合并后的数据是在myds里面,那么怎样取合并后的数据呢?
    this.myds.Tables[用表名访问]这只是取某个表而已?我要取的是合并后的数据
      

  23.   

    通过this.myds.Tables[用表名访问]取得合并后的表,不也就是取得了数据了吗?你可以进行更新等一系列的数据操作了,不明白楼主取得数据是什么意思,要获得什么样的效果!