zoukankan      html  css  js  c++  java
  • C# 行(datarow)拷贝方法

    有两个表A和B,两表结构相同。现在需要将A表中部分行拷贝到B表中。
    如果我们直接用 DataTableB.rows.add(dataTableA.rows[0]) 这样的方法式会报"row已经属于A表"这样的错误。
    所以我们可以用另外的方法。
    方法1,使用dataTable.ImportRow()方法。代码如下:
                            //得到A表中的部分行
                            DataRow[] drA = dtA.Select("aimtype=3");
                            
    //实例B
                            DataTable dtB = dtA.Clone();               
                            
    if (drA.Length > 0)
                            {
                                
    foreach (DataRow drVal in drA)
                                {
                                    
    //向B中增加行
                                    dtB.ImportRow(drVal);
                                }
                            }

    方法2,使用DataTable.Rows.Add(params object[] values)方法,代码如下:
                            //得到A表中的部分行
                            DataRow[] drA = dtA.Select("aimtype=3");
                            
    //实例B
                            DataTable dtB = dtA.Clone();               
                            
    if (drA.Length > 0)
                            {
                                
    foreach (DataRow drVal in drA)
                                {
                                    
    //向B中增加行
                                    dtB.Rows.Add(drVal.ItemArray);
                                }
                            }
  • 相关阅读:
    hdu 2546 饭卡
    poj 2262 Goldbach's Conjecture
    poj 1287 Networking
    poj 2377 Bad Cowtractors
    poj 1789 Truck History
    poj 2349 Arctic Network
    poj 1258 Agri-Net
    acdream 20140730 D题
    hdu 1012 素数判定
    hdu 2098 分拆素数和
  • 原文地址:https://www.cnblogs.com/scottckt/p/1583225.html
Copyright © 2011-2022 走看看