ado.net 学习 06 DataSet,DataTable提供一个有用的方法Select()

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         try
13         {
14             con.Open();
15             // Fill the DataSet with the Products table.
16             da.Fill(ds, "Products");
17         }
18         finally
19         {
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("<li>");
30             htmlStr.Append(childRow["ProductName"].ToString());
31             htmlStr.Append("</li>");
32         }
33         htmlStr.Append("</ul>");
34 
35         // Show the generated HTML code.
36         HtmlContent.Text = htmlStr.ToString();
37     }

运行结果:

对比

posted @ 2013-05-29 23:02  罗汉果  阅读(230)  评论(0编辑  收藏  举报