在 C# 中合并两个数据表

发布于 2024-12-10 00:26:31 字数 725 浏览 1 评论 0原文

我有两个数据表,如下

Table1
--------------------------------
Id     | Batch  | Qty
-----------------------------
1         A1       5
2         A2       5
3         A3       5
4         A4       5


Table2
--------------------------------
Id     | Batch  | Qty
-----------------------------
1         A1       6
2         A2       6
3         A3       6
5         A5       10

Expected result
--------------------------------
Id     | Batch  | Qty
-----------------------------
1         A1       6 (Qty updated)
2         A2       6 (Qty updated)
3         A3       6 (Qty updated)
4         A4       5 (remains as same)
5         A5       10 (row in table 2)

我如何在 C# 中实现这一点。如果有人知道这个数据表操作请分享..

I Have two datatables as follows

Table1
--------------------------------
Id     | Batch  | Qty
-----------------------------
1         A1       5
2         A2       5
3         A3       5
4         A4       5


Table2
--------------------------------
Id     | Batch  | Qty
-----------------------------
1         A1       6
2         A2       6
3         A3       6
5         A5       10

Expected result
--------------------------------
Id     | Batch  | Qty
-----------------------------
1         A1       6 (Qty updated)
2         A2       6 (Qty updated)
3         A3       6 (Qty updated)
4         A4       5 (remains as same)
5         A5       10 (row in table 2)

How can i achieve this in c#. If anybody knows this data table operation please share..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2024-12-17 00:26:31

尝试这样的事情,这是一个如何合并两个数据表的示例......

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Data; 

namespace ConsoleApplication1 
{ 
  class Program 
  { 
     static void Main(string[] args) 
     { 
          DataColumn col; 
          DataTable table1 = new DataTable(); 
          table1.PrimaryKey = new DataColumn[] { 
          col = table1.Columns.Add("slot_id") 
           }; 
          col.DataType = typeof(int); 
          col.Unique = true; 
          col = table1.Columns.Add("appointment_time"); 
          col = table1.Columns.Add("patient_name"); 
          col = table1.Columns.Add("patient_doctor"); 

          table1.Rows.Add(1, "0900", "George Michael"); 

          DataTable table2 = new DataTable(); 
          table2.PrimaryKey = new DataColumn[] { 
          col = table2.Columns.Add("slot_id") 
           }; 
          col.DataType = typeof(int); 
          col.Unique = true; 
         col = table2.Columns.Add("appointment_time"); 

          table2.Rows.Add(1, "0900"); 
          table2.Rows.Add(2, "1000"); 
          table2.Rows.Add(3, "1100"); 
          table2.Rows.Add(4, "1200"); 

         DataTable merged = new DataTable(); 
        merged.Merge(table1); 
        merged.Merge(table2); 

        foreach (DataColumn dc in merged.Columns) 
        Console.Write(dc.ColumnName + "\t"); 
        Console.WriteLine(); 

       foreach (DataRow dr in merged.Rows) 
       { 
            foreach (DataColumn dc in merged.Columns) 
            Console.Write(dr[dc.ColumnName] + "\t"); 
            Console.WriteLine(); 
       } 
       Console.WriteLine(); 

       Console.Write("Press any key to continue . . . "); 
       Console.ReadKey(true); 
       Console.WriteLine(); 
    } 
  } 
} 

try something like this, this is an example how to merge two datatables...

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Data; 

namespace ConsoleApplication1 
{ 
  class Program 
  { 
     static void Main(string[] args) 
     { 
          DataColumn col; 
          DataTable table1 = new DataTable(); 
          table1.PrimaryKey = new DataColumn[] { 
          col = table1.Columns.Add("slot_id") 
           }; 
          col.DataType = typeof(int); 
          col.Unique = true; 
          col = table1.Columns.Add("appointment_time"); 
          col = table1.Columns.Add("patient_name"); 
          col = table1.Columns.Add("patient_doctor"); 

          table1.Rows.Add(1, "0900", "George Michael"); 

          DataTable table2 = new DataTable(); 
          table2.PrimaryKey = new DataColumn[] { 
          col = table2.Columns.Add("slot_id") 
           }; 
          col.DataType = typeof(int); 
          col.Unique = true; 
         col = table2.Columns.Add("appointment_time"); 

          table2.Rows.Add(1, "0900"); 
          table2.Rows.Add(2, "1000"); 
          table2.Rows.Add(3, "1100"); 
          table2.Rows.Add(4, "1200"); 

         DataTable merged = new DataTable(); 
        merged.Merge(table1); 
        merged.Merge(table2); 

        foreach (DataColumn dc in merged.Columns) 
        Console.Write(dc.ColumnName + "\t"); 
        Console.WriteLine(); 

       foreach (DataRow dr in merged.Rows) 
       { 
            foreach (DataColumn dc in merged.Columns) 
            Console.Write(dr[dc.ColumnName] + "\t"); 
            Console.WriteLine(); 
       } 
       Console.WriteLine(); 

       Console.Write("Press any key to continue . . . "); 
       Console.ReadKey(true); 
       Console.WriteLine(); 
    } 
  } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文