如何对DataTable进行检索和排序

显示结果 

 

CustomerIDCompanyNameCountry
WHITCWhite Clover MarketsUSA
TRAIHTrail's Head Gourmet ProvisionersUSA
THECRThe Cracker BoxUSA
THEBIThe Big CheeseUSA
SPLIRSplit Rail Beer & AleUSA
SAVEASave-a-lot MarketsUSA
RATTCRattlesnake Canyon GroceryUSA
OLDWOOld World DelicatessenUSA
LONEPLonesome Pine RestaurantUSA
LETSSLet's Stop N ShopUSA
LAZYKLazy K Kountry StoreUSA
HUNGCHungry Coyote Import StoreUSA
GREALGreat Lakes Food MarketUSA


源代码  

 

 
<%  @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 >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值