Table.Select,Table.Fine,Table.Clone 用法 总结

本文介绍了DataTable的几种关键操作方法,包括Clone方法创建具有相同结构的新表、使用Find方法按主键查找行以及通过Select方法筛选数据行。这些技巧有助于提高数据处理效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 一:Clone返回新的 DataTable

Clone返回新的 DataTable,与当前的 DataTable 具有相同的架构;Copy:返回新的 DataTable,它具有与该 DataTable 相同的结构(表架构和约束)和数据 

DataRow[] _dr = DT.Select("CLSSBH='"+ Filter_Str +"'"); 
DataTable dt = DT.Clone() ; 
DataRow dr ; 
for( int j = 0 ;j < _dr.Length ;j ++ ) 

dr = dt.NewRow() ; 
dr = _dr[j] ; 
dt.ImportRow( dr ) ; 

  
我一般使用DataTable.Rows.Add(row.ItemArray); 

DataTable.ImportRow(row)

 

二:DataRowCollection.Find (Object)  获取由主键值指定的行。

DataRowCollection.Find (Object[])  获取包含指定的主键值的行。 
private void FindInMultiPKey(DataTable myTable){
    DataRow foundRow;
    // Create an array for the key values to find.
    object[]findTheseVals = new object[3];
    // Set the values of the keys to find.
    findTheseVals[0] = "John";
    findTheseVals[1] = "Smith";
    findTheseVals[2] = "5 Main St.";
    foundRow = myTable.Rows.Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != null)
      Console.WriteLine(foundRow[1]);
 }

DataTable.Select方法

private void GetRowsByFilter() 

    DataTable table = DataSet1.Tables["Orders"]; 

    // Presuming the DataTable has a column named Date. 
    string  = "Date > '1/1/00'"; 

    // Sort descending by column named CompanyName. 
    string sortOrder = "CompanyName DESC"; 
    DataRow[] foundRows; 

    // Use the Select method to find all rows matching the filter. 
    foundRows = table.Select(, sortOrder); 

    // Print column 0 of each returned row. 
    for(int j = 0; j < foundRows.Length; j ++) 
    { 
        Console.WriteLine(foundRows[j][0]); 
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值