标签:
相关知识:
代码示例:
(示例数据库使用红皮书的示例数据库:AdventureWorks_WroxSSRS2012)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Data; 7 using System.Data.SqlClient; 8 9 namespace ConsoleApplication10 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 string strConn = @"server=Joe-PC;database=AdventureWorks_WroxSSRS2012;uid=sa;pwd=root"; 16 SqlConnection conn = new SqlConnection(strConn); 17 18 DataSet ds = new DataSet(); 19 20 string strCmd1 = "SELECT ProductCategoryID,Name FROM Production.ProductCategory"; 21 SqlDataAdapter da1 = new SqlDataAdapter(strCmd1, conn); 22 // 将第一个查询结果集合填入DataSet中,并且将DataTable命名为"Category" 23 da1.Fill(ds, "Category"); 24 25 string strCmd2 = "SELECT ProductSubcategoryID,ProductCategoryID,Name From Production.ProductSubcategory"; 26 SqlDataAdapter da2 = new SqlDataAdapter(strCmd2, conn); 27 // 将第二个查询结果集合填入DataSet中,并且将DataTable命名为"Subategory" 28 da2.Fill(ds, "Subcategory"); 29 30 // 使用视图 31 // 打印表中的数据 32 Console.WriteLine("主类别表:"); 33 DataTable dt1 = ds.Tables["Category"];//获得Category表 34 DataView dv1 = new DataView(dt1); //创建视图 35 dv1.Sort = "ProductCategoryID ASC"; //设置排序规则 36 foreach (DataRowView drv in dv1) 37 { 38 Console.WriteLine("{0}:{1}", drv[0], drv["Name"]); 39 } 40 41 Console.WriteLine(""); 42 43 Console.WriteLine("过滤后的子类别表:"); 44 DataTable dt2 = ds.Tables["Subcategory"]; 45 DataView dv2 = new DataView(dt2); 46 dv2.RowFilter = "ProductSubcategoryID>10";//设置过滤条件 47 dv2.Sort = "ProductSubcategoryID ASC"; 48 foreach (DataRowView drv in dv2) 49 { 50 Console.WriteLine("{0}:{1}", drv[0], drv["Name"]); 51 } 52 53 Console.WriteLine(""); 54 55 // 在两个表之间建立关联 56 DataRelation relation = new DataRelation("ProductCategory_ProductSubcategory", 57 dt1.Columns["ProductCategoryID"], dt2.Columns["ProductCategoryID"]); 58 ds.Relations.Add(relation);//将关联添加到DataSet的集合中 59 try 60 { 61 for (int i = 0; i < dt1.Rows.Count; i++) 62 { 63 DataRow dri = dt1.Rows[i]; 64 //根据关联找到数据相关的子类别数据 65 DataRow[] subRows = dri.GetChildRows(relation); 66 Console.WriteLine("{0}的子类别信息:", dri["Name"]); 67 foreach (DataRow dr in subRows) 68 { 69 Console.WriteLine("{0}:{1}", dr[0], dr["Name"]); 70 } 71 Console.WriteLine(""); 72 } 73 } 74 catch (Exception e) 75 { 76 Console.WriteLine(e); 77 } 78 } 79 } 80 }
标签:
原文地址:http://www.cnblogs.com/chenguangqiao/p/4354494.html