1. 程式人生 > >如何對DataTable進行檢索和排序

如何對DataTable進行檢索和排序

{
                
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);
            }