2009-05-28 27 views

Trả lời

13

Bạn đang tìm kiếm nhiều khả năng nhất cho phương thức DataTable.Merge.

Ví dụ:

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

Bạn có thể thử này:

public static DataTable Union (DataTable First, DataTable Second) 
{ 

     //Result table 
     DataTable table = new DataTable("Union"); 

     //Build new columns 
     DataColumn[] newcolumns = new DataColumn[First.Columns.Count]; 

     for(int i=0; i < First.Columns.Count; i++) 
     { 
      newcolumns[i] = new DataColumn(
      First.Columns[i].ColumnName, First.Columns[i].DataType); 
     } 

     table.Columns.AddRange(newcolumns); 
     table.BeginLoadData(); 

     foreach(DataRow row in First.Rows) 
     { 
      table.LoadDataRow(row.ItemArray,true); 
     } 

     foreach(DataRow row in Second.Rows) 
     { 
      table.LoadDataRow(row.ItemArray,true); 
     } 

     table.EndLoadData(); 
     return table; 
} 

Từ here (không kiểm tra).

+0

Có lẽ ông cũng muốn đưa vào tài khoản các trường hợp khi cả hai DataTables có hồ sơ phù hợp (giá trị tương tự cho tất cả các cột) –

2

Bạn có thể sử dụng concat từ LINQ to bộ dữ liệu (được chương tự do của LINQ trong hành động) để tham gia cùng họ và sau đó .AsDataTable để tạo ra các bảng (giả sử bạn thực sự muốn họ như một DataTable)

+0

Tôi không sử dụng LINQ ... – Dhana

+0

LINQ là như jQuery của vũ trụ truy cập dữ liệu .NET, đó là câu trả lời cho mọi thứ. – TheTXI

+1

@LazyBoy: bạn sẽ sớm (nghiêm túc mặc dù, chương của cuốn sách đi qua các System.Data.DataSetExtensions lắp ráp và afforances nó cung cấp mở rộng DataSets để cung cấp nhiều quyền lực hơn DataSets đã cung cấp (và với sức mạnh phức tạp đó. ..)). @ TheTXI: Nếu nó không thể được thực hiện bởi sprinklingh trong một số PS và một số LINQ, điểm: D –

1

Tình cờ gặp này câu hỏi, và Ruben Bartelink đã đưa ra một câu trả lời tuyệt vời, nhưng không có mã. Vì vậy, tôi đã phải tìm nó ở nơi khác, mà đánh bại điểm của StackOverflow. Bây giờ là năm 2010, các câu trả lời khác được đưa ra không hoàn toàn khả thi. Để tham khảo, đây là mã thể hiện phương thức mở rộng CopyToDataTable(). Đó là trong VB để không ăn cắp thẻ tín dụng từ Ruben nếu anh muốn xem lại quá khứ và gửi một câu trả lời hoàn chỉnh hơn :)

Public Function GetSchema(ByVal dbNames As IEnumerable(Of String)) As DataTable 
    Dim schemaTables As New List(Of DataTable)() 
    For Each dbName As String In dbNames 
     Dim cnnStr = GetConnectionString(dbName) 
     Dim cnn As New SqlConnection(cnnStr) 
     cnn.Open() 
     Dim dt = cnn.GetSchema("Columns") 
     cnn.Close() 
     schemaTables.Add(dt) 
    Next 

    Dim dtResult As DataTable = Nothing 
    For Each dt As DataTable In schemaTables 
     If dtResult Is Nothing Then 
     dtResult = dt 
     Else 
     dt.AsEnumerable().CopyToDataTable(dtResult, LoadOption.PreserveChanges) 
     End If 
    Next 

    Return dtResult 
End Function 
0

Hãy thử này sử dụng LINQ to DataSet, phải thêm tài liệu tham khảo cho System.Data. DataSetExtensions.dll, một cách tiếp cận khác, thay thế cho phương thức DataTable.Merge).

static void Main(string[] args) 
{ 
    DoUnion(); 
} 

private static void DoUnion() 
{ 
    DataTable table1 = GetProducts(); 
    DataTable table2 = NewProducts(); 
    var tbUnion = table1.AsEnumerable() 
     .Union(table2.AsEnumerable()); 
    DataTable unionTable = table1.Clone(); 
    foreach (DataRow fruit in tbUnion) 
    { 
     var fruitValue = fruit.Field<string>(0); 
     Console.WriteLine("{0}->{1}", fruit.Table, fruitValue); 
     DataRow row = unionTable.NewRow(); 
     row.SetField<string>(0, fruitValue); 
     unionTable.Rows.Add(row); 
    } 
} 

private static DataTable NewProducts() 
{ 
    DataTable table = new DataTable("CitricusTable"); 
    DataColumn col = new DataColumn("product", typeof(string)); 
    table.Columns.Add(col); 
    string[] citricusFruits = { "Orange", "Grapefruit", "Lemon", "Lime", "Tangerine" }; 
    foreach (string fruit in citricusFruits) 
    { 
     DataRow row = table.NewRow(); 
     row.SetField<string>(col, fruit); 
     table.Rows.Add(row); 
    } 
    return table; 
} 

private static DataTable GetProducts() 
{ 
    DataTable table = new DataTable("MultipleFruitsTable"); 
    DataColumn col = new DataColumn("product", typeof(string)); 
    table.Columns.Add(col); 
    string[] multipleFruits = { "Breadfruit", "Custardfruit", "Jackfruit", "Osage-orange", "Pineapple" }; 
    foreach (string fruit in multipleFruits) 
    { 
     DataRow row = table.NewRow(); 
     row.SetField<string>(col, fruit); 
     table.Rows.Add(row); 
    } 
    return table; 
} 

antonio

Các vấn đề liên quan