Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of asp.net DataTable-related Operation

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这期内容当中小编将会给大家带来有关asp.net DataTable相关操作的示例分析,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

#region DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回/// /// DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回/// eg:SortExprDataTable(dt,"Sex='男'","Time Desc",1)/// /// 传入的DataTable/// 筛选条件/// 排序条件/// 1,直接用DefaultView按条件返回,效率较高;2,DataTable筛选,排序返回符合条件行组成的新DataTablepublic static DataTable SortDataTable(DataTable dt, string strExpr, string strSort, int mode){ switch (mode) { case 1: //方法一 直接用DefaultView按条件返回 dt.DefaultView.RowFilter = strExpr; dt.DefaultView.Sort = strSort; return dt; case 2: //方法二 DataTable筛选,排序返回符合条件行组成的新DataTable DataTable dt1 = new DataTable(); DataRow[] GetRows = dt.Select(strExpr, strSort); //复制DataTable dt结构不包含数据 dt1 = dt.Clone(); foreach (DataRow row in GetRows) { dt1.Rows.Add(row.ItemArray); } return dt1; default: return dt; }}#endregion#region 获取DataTable前几条数据/// /// 获取DataTable前几条数据/// /// 前N条数据/// 源DataTable/// public static DataTable DtSelectTop(int TopItem, DataTable oDT){ if (oDT.Rows.Count < TopItem) return oDT; DataTable NewTable = oDT.Clone(); DataRow[] rows = oDT.Select("1=1"); for (int i = 0; i < TopItem; i++) { NewTable.ImportRow((DataRow)rows[i]); } return NewTable;}#endregion#region 获取DataTable中指定列的数据/// /// 获取DataTable中指定列的数据/// /// 数据源/// 新的DataTable的名词/// 指定的列名集合/// 返回新的DataTablepublic static DataTable GetTableColumn(DataTable dt, string tableName, params string[] strColumns){ DataTable dtn = new DataTable(); if (dt == null) { throw new ArgumentNullException("参数dt不能为null"); } try { dtn = dt.DefaultView.ToTable(tableName, true, strColumns); } catch (Exception e) { throw new Exception(e.Message); } return dtn;}#endregionusing System;using System.Collections.Generic;using System.Linq;using System.Data;using System.Collections;using System.Text;namespace GuanEasy{ /// /// DataSet助手 /// public class DataSetHelper { private class FieldInfo { public string RelationName; public string FieldName; public string FieldAlias; public string Aggregate; } private DataSet ds; private ArrayList m_FieldInfo; private string m_FieldList; private ArrayList GroupByFieldInfo; private string GroupByFieldList; public DataSet DataSet { get { return ds; } } #region Construction public DataSetHelper() { ds = null; } public DataSetHelper(ref DataSet dataSet) { ds = dataSet; } #endregion #region Private Methods private bool ColumnEqual(object objectA, object objectB) { if ( objectA == DBNull.Value && objectB == DBNull.Value ) { return true; } if ( objectA == DBNull.Value || objectB == DBNull.Value ) { return false; } return ( objectA.Equals( objectB ) ); } private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns) { bool result = true; for ( int i = 0; i < columns.Count; i++ ) { result &= ColumnEqual( rowA[ columns[ i ].ColumnName ], rowB[ columns[ i ].ColumnName ] ); } return result; } private void ParseFieldList(string fieldList, bool allowRelation) { if ( m_FieldList == fieldList ) { return; } m_FieldInfo = new ArrayList(); m_FieldList = fieldList; FieldInfo Field; string[] FieldParts; string[] Fields = fieldList.Split( ',' ); for ( int i = 0; i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report