惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
V
Visual Studio Blog
S
Securelist
P
Palo Alto Networks Blog
A
Arctic Wolf
T
Tor Project blog
P
Proofpoint News Feed
I
InfoQ
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Apple Machine Learning Research
Apple Machine Learning Research
S
Secure Thoughts
Cyberwarzone
Cyberwarzone
Blog — PlanetScale
Blog — PlanetScale
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
腾讯CDC
Latest news
Latest news
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LINUX DO - 热门话题
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
C
Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
The Last Watchdog
The Last Watchdog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
Know Your Adversary
Know Your Adversary
U
Unit 42

博客园 - melody&bobo

C#加密解密 端口数据库连接 ANSI, UNICODE,UTF8编码的区别 调用系统API打印图片文字 windows2003安全设置 图片与字节数组相互转换的方法 jQuery Ajax 方法调用 Asp.Net WebService 的详细例子 使用instantclient_11_2和PL/SQL Developer工具包连接oracle 11g远程数据库 查看SQL 语句执行性能 SSIS 获取时间表达式 获得时间段之间每月的最后一天 ADSL拨号获得不同IP地址 C#(.Net) 解决Informix中文乱码问题 SQL SPLIT() 方法实现 JS获取URL参数值 表单提交文件 - melody&bobo - 博客园 nginx实现网站负载均衡测试实例(windows下IIS做负载实测) Web Application Stress Tool(WAS,Web应用负载测试工具)详细说明 Base64 加密字符串和文件
datatable操作集合
melody&bobo · 2011-03-10 · via 博客园 - melody&bobo

在.NET中使用datatable来操作数据是非常方便的,下面是对DataTable数据实现排序、检索、合并、分页、统计等功能,值得看一下:一、DataTable排序
1 获取DataTable的默认视图
2 对视图设置排序表达式
3 用排序后的视图导出的新DataTable替换就DataTable
(Asc升序可省略,多列排序用","隔开)
DataView dv = dt.DefaultView;
dv.Sort = "id Asc,name Desc";
dt = dv.ToTable();
二、DataTable检索
1 设置查询字符串
2 使用Select方法获取到所有满足条件的数据行对象数组
(多项查询条件间,用and隔开.模糊查询使用 like %)
DataRow[] matches = dt.Select("(id>=20) and (title='会议') and (yearstr like '%2007%')");
string strName = matches[0]["name"].ToString();
另外也可以用下面这种方法,将检索出来的新表绑定到GridView
DataView dv = dt.DefaultView;
dv.RowFilter = "id>=2";
this.GridView1.DataSource = dv;
this.GridView1.DataBind();

三、DataTable合并
假定有2个DataTable:Dt1 , Dt2。表结构一样
将Dt2接在Dt1后可采用此方法
dt1.Merge(dt2);
四、DataTable分页
PagedDataSource pds = new PagedDataSource();
                pds.AllowPaging = true;
                pds.DataSource = dvIntegralExpense;
                pds.AllowPaging = true;
                pds.PageSize = pager.PageSize;
                pds.CurrentPageIndex = pager.PageIndex;

                rptIntegralExpense.DataSource = pds;
                rptIntegralExpense.DataBind();

五:
  我们在使用Sql Server这些数据库时,可以轻松的通过Sum、Aver、Count等统计出相关结
 
果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Service获得
 
了DataSet,这个时候,可是没有办法回头去修改Select语句来获取这些统计了。那么在DataSe
 
t/DataTable中是否可以进行统计呢?答案是肯定的。
 
  在MSDN中,有一篇MS推荐的统计方法,就是逐行对数据进行求和统计,这个方法,其实有
 
等于无(或许这个方法只是针对于DataGrid求取小计用吧),因为这个方法中采用的是DataGri
 
d的ItemDataBind事件来对数据进行累加,同我们手动写代码统计没有什么区别。
  
  本文介绍一个简单的方法,不需要逐条记录进行计算就可以轻松的获得DataTable中的记录
 
统计结果。这个简单的方法就是调用功能强大的DataTable的函数Compute。
  
  一、调用说明(仅以C#为例,下同):
  
  public object Compute(string strExpression,string strFilter)
  
  参数:
  
  strExpression:要计算的表达式字符串,基本上类似于Sql Server中的统计表达式
  
  strFilter:统计的过滤字符串,只有满足这个过滤条件的记录才会被统计
  
  二、调用举例:
  
  以下示例,假设一个产品销售表table,描述某商场中各促销员销售的实际记录,包含字段
 
为:姓名(Name)、性别(Sex,0为女,1为男)、生日(Birthday)、销售产品的代码(ProID
 
)、销售的数量(Quantity)、销售价格(Price)。
  
  1.统计所有性别为女的销售员的数量:
  table.Compute("Count(*)","Sex=0");
  
  2.统计所有销售员中年龄大于20岁的
  table.Compute("Count(*)","Birthday<'"+today);//today为今天的日期字符串
  
  3.统计销售产品的平均价格
  table.Compute("Aver(Price)","true");
  
  4.统计产品代码为1的产品销售数量:
  table.Compute("Sum(Quantity)","ProID=1");
  
  5.统计所有产品的销售总金额:
  要统计总销售金额,由于table中不存在某项产品某个促销员销售的金额数据,但我们可以
 
通过Quantity*Price来获得。比如:
  table.Compute("Sum(Quantity*Price)","true");
  
  这里一个问题是:DataTable的统计功能没有SqlServer强,这个统计是错误的,因为Compu
 
te的统计不具备Sum(Quantity*Price)这样数据的功能。那怎么办呢?
  
  对于这样复杂数据的统计,我们可以在DataTable中创建一个新的字段来完成,比如Amount
 
,同时设置该字段的Expression为Quantity*Price,这样我们就可以使用统计功能了:
  table.Compute("Sum(Amount)","true");