1.DataTable提供一个有用的方法Select(),它可以使用SQL表达式查询某行,Select()方法使用的表达式和select语句的Where子句的作用一样,其依据是DataTable中已经存在内在数据。
DataRow[] matchRows = ds.Tables["Products"].Select("discontinued=1");
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 // Create the Connection, DataAdapter, and DataSet. 4 string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; 5 SqlConnection con = new SqlConnection(connectionString); 6 7 string sqlProd = "SELECT ProductName, CategoryID,discontinued FROM Products"; 8 9 SqlDataAdapter da = new SqlDataAdapter(sqlProd, con);10 DataSet ds = new DataSet();11 12 try13 {14 con.Open();15 // Fill the DataSet with the Products table.16 da.Fill(ds, "Products");17 }18 finally19 {20 con.Close();21 }22 23 StringBuilder htmlStr = new StringBuilder("");24 25 DataRow[] matchRows = ds.Tables["Products"].Select("discontinued=1");26 27 foreach (DataRow childRow in matchRows)28 {29 htmlStr.Append("
运行结果:
对比