merge,几个datatable之间的合并,可以的

解决方案 »

  1.   

    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();
          }
       }
    }
      

  2.   

    Sorry,可能我回答得有些问题,但是dataset中也可以设定几个datatable之间的关系啊,dataview不能把相关的数据返回显示吗?请回答,我不能再回答了。
      

  3.   

    使用Merge破坏了数据集的结构,我想达到的效果是类似于Sql数据库中的View,即把多个表Join成一个数据集合,这个数据集合必须是一个整体。
    数据集中的表确实可以定义相互之间的关系,但如何利用这些关系实现表之间的Join并生成一个非DataTable的数据集合是我关心的。
      

  4.   

    用datarelation关联的只是几个表 并不是对这几个表的查询啊 
    没试过 回去看看
      

  5.   

    可以不在后面完成合并,你可以之前合并生成一个Table,不必要在这些问题上考虑过多的东西,如果实在不行你还可以在本地生成一个Access数据库来进行查询。