如何對DataTable進行檢索和排序
阿新 • • 發佈:2019-02-15
{
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);
}
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
string Sql ="SELECT CustomerID, CompanyName, Country FROM Customers";
SqlConnection thisConnection =new SqlConnection(ConnectionString);
SqlDataAdapter adapter
// 建立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);
}