zoukankan      html  css  js  c++  java
  • 一个DataTable赋值给另一个DataTable的常用方法

    DataView view = new DataView();
    view.Table = DataTableA;
    view.RowFilter = "itemType = 'book'";//itemType是DataTableA中的一个字段
    DataTableB= view.ToTable();
    或者:
    DataRow[] rows = DataTableA.Select("itemType = 'book'");
    DataTableB= DataTableA.Clone();
    foreach (DataRow row in rows)
    {
         DataTableB.ImportRow(row);
    }

    或者

    /// 执行DataTable中的查询返回新的DataTable
    /// </summary>
    /// <param name="dt">源数据DataTable</param>
    /// <param name="condition">查询条件</param>
    /// <returns></returns>
    private DataTable GetNewDataTable(DataTable dt, string condition,string sortstr)
    {
          DataTable newdt = new DataTable();
          newdt = dt.Clone();
          DataRow[] dr = dt.Select(condition,sortstr);
          for (int i = 0; i < dr.Length; i++)
          {
              newdt.ImportRow((DataRow)dr[i]);
          }
          return newdt;//返回的查询结果

    }

    或者 逐列的形式

    public static int TableDataExchange(DataSet ds, string tableName, DataTable sourceDT) 
            {
                 for(int i=0;i<sourceDT.Rows.Count;i++)
                 {
                     DataRow drNew = ds.Tables[tableName].NewRow();
                     foreach (DataColumn dc in sourceDT.Columns)
                     {
                         drNew[dc.ColumnName] = sourceDT.Rows[i][dc.ColumnName];
                     }
                     ds.Tables[tableName].Rows.Add(drNew);
                 }
                 ds.Tables[tableName].AcceptChanges();
                 DataTable dt = ds.Tables[tableName];
                 return ds.Tables[tableName].Rows.Count;
                 
            }

    或者

    DataSet 对象是支持 ADO.NET的断开式、分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据或者是DataRow的数据,但是只有DataSet和DataTable的复制是支持深层复制的,就是说不仅能复制元素的结构,而且能复制元素的数据,而DatatDataRow没有相关的复制的方法,下面将简单介绍下这些数据元素的复制问题。

    DataTable sourceTable; 
    DataTable objectTable; 
    DatatDataRow sourceRow; 
    DatatDataRow objectRow; 
    DataRow tempRow; 
    DataSet souceDataSet = new DataSet();

    复制DataSet

      DataSet object = souceDataSet.Copy();//深复制 
      DataSet object = souceDataSet.Clone();//浅复制,只复制架构

    复制DataTable

      objectTable  = sourceTable .Copy();//深复制 
      objectTable  = sourceTable .Clone();//浅复制,只复制架构

    复制DataRow

        项目开发中经常遇到这种错误-“此行已属于另一个表” 。导致这个错误的语句如下:


         objectTable .Rows.Add(SourceDataRow); 
     

          分析了一下原因,因为DataRow DataTable 都是传引用调用的。所以一个行在一个表中了,就不能再增加到另外一个表。

    具体方法:

    1  ImportRow方法:public void ImportRow( DataRow DataRow);

    objectTable = sourceTable.clone();//必须先复制表的架构,使具有相同的的列或关系! 
    foreach (DataRow oRow in sourceTable) 

           objectTable.ImportRow(oRow);//在objectTable中添加一个新行,并将sourceRow的值复制进去,要求表的结构一样!

    }

    _____________________________________________________________________________________________________

    2. 循环DataTable的每个列

    DataRow aDataRow = objectTable.NewRow();

        foreach(DataColumn aDataColumn in sourceTable.Columns)

        {

            aDataRow [aDataColumn.ColumnName] = sourceTable[i][aDataColumn.ColumnName];

        }

        objectTable.Rows.Add(aDataRow);

    3. 自定义复制

    objectTable.Columns.Add ("id");//不需要有一样的架构,只复制自己需要的列!  
    Object [] myArry = new Object [1];  
       foreach (DataRow oRow in sourceTable) 

      tempRow = objectTable.NewRow();//此方法必须调用! 
      myArry[0] = oRow["id"];//如果myArry中没有源表中的id列的话就会报错! 
      tempRow.ItemArray = myArry;//ItemArray属性为Object类型数组,根据程序的需要需要可自行复制多个列的数据! 
      objectTable.Rows.Add(tempRow); //此方法必须调用,否则DataRow中的数据将不能显示!

    } 
    _____________________________________________________________________________________________________

    4. LoadDataRow方法:public DataRow LoadDataRow(Object[] values,bool fAcceptChanges);

        Object[] newRow = new Object[3]; 
       // 设置对象数组的值 
        newRow[0] = "Hello"; 
        newRow[1] = "World"; 
        newRow[2] = "two"; 
        DataRow myRow; 
        ObjectTable.BeginLoadData(); 
        // 将新行添加到表中 
        myRow = ObjectTable.LoadDataRow(newRow, true);//标志要设置为true,表示添加新行 
        ObjectTable.EndLoadData();


    该方法比较复杂,如果只是简单的复制现有行的数据来添加新行的话建议不要采用,具体用法请参看sdk文挡。

    或者:

    < type="text/JavaScript"> alimama_pid="mm_10249644_1605763_4929893"; alimama_titlecolor="0000FF"; alimama_descolor ="000000"; alimama_bgcolor="FFFFFF"; alimama_bordercolor="E6E6E6"; alimama_linkcolor="008000"; alimama_bottomcolor="FFFFFF"; alimama_anglesize="0"; alimama_bgpic="0"; alimama_icon="0"; alimama_sizecode="16"; alimama_width=658; alimama_height=60; alimama_type=2; < src="http://a.alimama.cn/inf.js" type=text/javascript>

    我们经常需要向DataTable中添加一行数据,大多数的情况下都是把一些从UI的控件和程序的变量中收集的数据添加到DataTable中。如以下的语句把供应商代码和名称添加到DataTable中:

    DataTable dtProvider = new DataTable();
                DataRow drRow = dtProvider.NewRow();
                drRow[0] = txtProviderCode.Text.Trim();
                drRow[1] = txtProviderName.Text.Trim();
                dtProvider.Rows.Add(drRow);

    大多数的情况下这几行语句是完全可以满足要求的。但是如果想把另一个同样结构的DataTable的某一行添加到这个dtProvider中,就不能简单的添加了。否则会提示一个错误“This row belongs to another table.”。这个时候我们必须定义另一个DataRow,把源DataRow的数据赋到目的DataRow中,再Add进DataTable中就可以了。如下所示:

    DataTable dtProvider = new DataTable();
                DataRow drTarget = dtProvider.NewRow();
                drTarget.ItemArry = drSource.ItemArry;
                // 注意:这里的drSource是另一个相同结构的DataTable中的一行。
                dtProvider.Rows.Add(drTarget);
                < type="text/JavaScript"> alimama_pid="mm_10249644_1605763_5027492"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=0; < src="http://a.alimama.cn/inf.js" type=text/javascript>

    我们度尽的年岁,好像一声叹息,不过是劳苦愁烦,转眼成空 ​​​​
  • 相关阅读:
    Oracle 11g SQL Fundamentals Training Introduction02
    Chapter 05Reporting Aggregated data Using the Group Functions 01
    Chapter 01Restriicting Data Using The SQL SELECT Statemnt01
    Oracle 11g SQL Fundamentals Training Introduction01
    Chapter 04Using Conversion Functions and Conditional ExpressionsConditional Expressions
    Unix时代的开创者Ken Thompson (zz.is2120.bg57iv3)
    我心目中计算机软件科学最小必读书目 (zz.is2120)
    北京将评估分时分区单双号限行 推进错时上下班 (zz)
    佳能G系列领军相机G1X
    选购单反相机的新建议——心民谈宾得K5(转)
  • 原文地址:https://www.cnblogs.com/tinya/p/4082844.html
Copyright © 2011-2022 走看看