table1所有数据->table2中怎么做

解决方案 »

  1.   

    Merging Data from Multiple Data Sets
    The DataSet class also supports a couple of ways of moving data between different data sets. Your first instinct might be to simply get a DataRow reference from one data set, perhaps from a Select operation or from iterating through, and then add that to the table in another data set. Unfortunately, this isn't allowed and will throw an exception. When a data row is added to a table, it becomes a part of that table, and it cannot belong to more than one table at a time. Only a row that has been created with the DataTable.NewRow method can be added to that table with the Add method on the table's Rows collection.So how do you get a row or set of rows of data that belong to one data set into another? One way is to use the Merge method. The behavior of this method can get fairly complex because it has a number of overloads, and it depends on what the state of the rows(Added, Modified, Deleted) are in the data set they are coming from(the source), and whether there are corresponding rows in the data set they are being merged into(the target). For simple cases, though, things are pretty straightforward.Consider a situation where you have the Customers table in one data set and the Orders table in another data set. You want to get them both into a single data set so that you can create a parent-child relationship between them based on the CustomerID foreign key that exists in the Orders table. To do this, you would simple call Merge on the data set containing the Customers table, passing in the Orders data set. The Merge method would create an Orders table in the Customers data set and copy over all the rows from the Orders data set. You could then construct the relation between the two tables in code as follows:
    private DataSet MergeCustomersAndOrders()
    {
        // Code to create and fill the two data sets...
        // customersDS contains just Customers,
        // ordersDS contains just Orders    // Merge the orders into the customers
        customersDS.Merge(ordersDS);
        
        DataColumn custIDParentCol = customersDS.Tables["Customers"].Columns["CustomerID"];
        DataColumn custIDChildCol = customersDS.Tables["Orders"].Columns["CustomerID"];
        DataRelation custOrders = new DataRelation("CustomersOrders", custIDParentCol, custIDChildCol);
        
        ...
        return customersDS; // contains both tables and relation
    }
       }
      

  2.   

    If you want to add a row from a table in one data set into the same table in another data set with the row state of Added, you can pass the ItemArray property of the source row to an overload of the Add Method on the Rows collection of the target table. This transfers the values of the cells as an array of objects into the target table, creating a new row to cantain them. If you want to add a row from a source table into a target and preserve its state, you can use the ImportRows method on the DataTable class. The following shows both of these approaches.private void AddAndImport(DataSet dsTarget, DataSet dsSource0
    {
        // Add the values from a single row from the source to 
        // the target with the row state set to Added
        dsTarget.Tables["Customers"].Rows.Add(dsSource.Tables["Customers"].Rows[0].ItemArray);    // Import a single row preserving row state
        dsTarget.Tables["Customers"].ImportRow(dsSource.Tables["Customers"].Rows[0]);
    }
    }
      

  3.   

    A common use of Merge is in conjunction with the Select method to extract a set of rows and place them in another data set, either for display or transfer to another method or client.private void OnSelect(object sender, EventArgs e)
    {
        // Select the rows using helper method
        DataRow[] rows = SelectRows(m_SelectTextBox.Text);
        
        // Show the result
        if(rows.Length > 0)
        {
            // Create a new data set
            DataSet localDS = new DataSet();
            // Merge the selected rows into it
            localDS.Merge(rows);
            // Bind the grid to this
            m_ResultsGrid.DataSource = localDS.Tables["Customers"];
        }
    }Another way to get a row into a table as a new row is to use the LoadDataRow method on the table, passing in an array of objects that contain the column values. Typically you will get this object array from the ItemArray on the source row.Because there isn't any way to directly set the row state of rows in a data table, if you want to get a row into a particular state, such as modified or deleted, there are some tricks you can play. For either of those states, you can first set the row state to unmodified by calling the AcceptChanges method on the row. To get the state set to modified, set the value of one of the columns to its current value by setting the column value against itself as shown next. To set the deleted state, just call Delete on the row:DataSet data = new DataSet();
    // Code to fill data set with Customers...DataRow modRow = data.Tables["Customers"].Rows[0];
    modRow.AcceptChanges(); // Set state to unchanged
    // Set state to modified:
    modRow["CompanyName"] = modRow="CompanyName"];DataRow delRow = data.Tables["Customers"].Rows[1];
    delRow.AcceptChanges(); // Set state to unchanged
    delRow.Delete(); // Set state to deleted