首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ADO.NET >> 正文

如何对DataTable进行检索和排序

显示结果

 

CustomerID CompanyName Country
WHITC White Clover Markets USA
TRAIH Trail's Head Gourmet Provisioners USA
THECR The Cracker Box USA
THEBI The Big Cheese USA
SPLIR Split Rail Beer & Ale USA
SAVEA Save-a-lot Markets USA
RATTC Rattlesnake Canyon Grocery USA
OLDWO Old World Delicatessen USA
LONEP Lonesome Pine Restaurant USA
LETSS Let's Stop N Shop USA
LAZYK Lazy K Kountry Store USA
HUNGC Hungry Coyote Import Store USA
GREAL Great Lakes Food Market USA


源代码


<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<HTML>
    
<HEAD>
        
<title>使用DataTable进行检索和排序示例</title>
        
<script language="C#" runat="server">

            
void Page_Load(object sender, System.EventArgs e)
            
{
                
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
                
string Sql = "SELECT CustomerID, CompanyName, Country FROM Customers";

                SqlConnection thisConnection 
= new SqlConnection(ConnectionString);
                SqlDataAdapter adapter 
= new SqlDataAdapter(Sql, thisConnection);

                
// 创建DataTable对象
                DataTable table = new DataTable();

                
// 填充数据到DataTable
                adapter.Fill(table);

                
// 定义筛选条件字符串和排序字符串
                string strExpr = "Country = 'USA'";
                
string strSort = "CompanyName DESC";

                
// 获得经过筛选和排序后的数据
                DataRow [] resultRows = table.Select(strExpr, strSort);

                
// 显示经过筛选和排序后的数据
                DisplayRows(resultRows, DisplayLabel);
            }

            
            
// 显示DataRow数组中的内容
            public void DisplayRows(DataRow [] rows, Label label)
            
{
                
// 检查返回数据是否为空
                if(rows.Length <= 0)
                
{
                    label.Text 
= "没有数据";
                    
return;
                }

                label.Text 
= "";

                
// 遍历DataRow数组的行和列,显示数据
                label.Text += "<Table border='1'>";
                label.Text 
+= "<TR><TH>CustomerID</TH><TH>CompanyName</TH><TH>Country</TH></TR>";
                
foreach(DataRow row in rows)
                
{
                    label.Text 
+= "<TR>";
                    
for(int i=0; i<row.Table.Columns.Count; i++)
                    
{
                        label.Text 
+= "<TD>";
                        label.Text 
+= row[i];
                        label.Text 
+= "</TD>";
                    }

                    label.Text 
+= "</TR>";
                }

                label.Text 
+= "</Table>";
            }


        
</script>
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
            
<H3>使用DataTable进行检索和排序示例</H3>
            
<asp:Label id="DisplayLabel" runat="server">Label</asp:Label>
        
</form>
    
</body>
</HTML>
【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航