在实际编程工程中,常常遇到这样的情况:DataTable并不是数据库中的,或者DataTable尚未写到数据库,或者从数据库中读出的DataTable已经在本地被改动,又没有写回数据库(可能还要作其他改动),在这些情况下,其实只要用.NET类库中提供的DataView类的强大功能(主要是用它的RowFilter属性),就能方便地解决这类查询问题。 常有网友在网上询问怎么在DataTable中执行DataTable.Select("条件")返回DataTable,今天我在这里给个解决方法给大家参考:代码如下
/// <summary>
/// 执行DataTable中的查询返回新的DataTable
///
</summary>
/// <param name="dt">源数据DataTable</param>
///
<param name="condition">查询条件</param>
///
<returns></returns>
private DataTable GetNewDataTable(DataTable
dt,string condition)
{
DataTable newdt = new
DataTable();
newdt=dt.Clone();
DataRow[] dr =
dt.Select(condition);
for(int
i=0;i<dr.Length;i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return
newdt;//返回的查询结果
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有