一、填充dataset
1 class Program 2 { 3 private static string constr = "server=.;database=northwnd;integrated security=sspi"; 4 static void Main(string[] args) 5 { 6 string sql = "select contactname,companyname from customers"; 7 using (SqlConnection con=new SqlConnection(constr)) 8 { 9 SqlDataAdapter sda = new SqlDataAdapter(sql, constr); 10 DataSet ds = new DataSet(); 11 sda.Fill(ds, "Customers"); 12 foreach (DataRow row in ds.Tables["Customers"].Rows) 13 Console.WriteLine("‘{0}‘ from {1}", 14 row[0], 15 row[1]); 16 17 con.Close(); 18 } 19 Console.ReadLine(); 20 } 21 }
二、dataset主外键关系
1 class Program 2 { 3 private static string constr = "server=.;database=northwnd;integrated security=sspi"; 4 static void Main(string[] args) 5 { 6 string select = "SELECT * FROM Products"; 7 string sel2 = "SELECT * FROM Categories"; 8 using (SqlConnection con=new SqlConnection(constr)) 9 { 10 SqlDataAdapter da = new SqlDataAdapter(select, con); 11 12 DataSet ds = new DataSet(); 13 14 15 ManufactureProductDataTable(ds); 16 17 da.Fill(ds, "Products"); 18 19 foreach (DataRow row in ds.Tables["Products"].Rows) 20 Console.WriteLine("‘{0}‘ from {1}", 21 row[0], 22 row[1]); 23 24 SqlDataAdapter da2 = new SqlDataAdapter(sel2, con); 25 26 27 ManufactureCategoryTable(ds); 28 29 da2.Fill(ds, "Categories"); 30 31 AddForeignKeyConstraint(ds); 32 33 con.Close(); 34 } 35 } 36 public static void ManufactureProductDataTable(DataSet ds) 37 { 38 DataTable products = new DataTable("Products"); 39 products.Columns.Add(new DataColumn("ProductID", typeof(int))); 40 products.Columns.Add(new DataColumn("ProductName", typeof(string))); 41 products.Columns.Add(new DataColumn("SupplierID", typeof(int))); 42 products.Columns.Add(new DataColumn("CategoryID", typeof(int))); 43 products.Columns.Add(new DataColumn("QuantityPerUnit", typeof(string))); 44 products.Columns.Add(new DataColumn("UnitPrice", typeof(decimal))); 45 products.Columns.Add(new DataColumn("UnitsInStock", typeof(short))); 46 products.Columns.Add(new DataColumn("UnitsOnOrder", typeof(short))); 47 products.Columns.Add(new DataColumn("ReorderLevel", typeof(short))); 48 products.Columns.Add(new DataColumn("Discontinued", typeof(bool))); 49 50 ManufacturePrimaryKey(products); 51 52 ds.Tables.Add(products); 53 } 54 public static void ManufacturePrimaryKey(DataTable dt) 55 { 56 DataColumn[] pk = new DataColumn[1]; 57 pk[0] = dt.Columns["ProductID"]; 58 dt.Constraints.Add(new UniqueConstraint("PK_Products", pk[0])); 59 dt.PrimaryKey = pk; 60 } 61 public static void ManufactureCategoryTable(DataSet ds) 62 { 63 DataTable categories = new DataTable("Categories"); 64 65 categories.Columns.Add(new DataColumn("CategoryID", typeof(int))); 66 categories.Columns.Add(new DataColumn("CategoryName", typeof(string))); 67 categories.Columns.Add(new DataColumn("Description", typeof(string))); 68 69 categories.Constraints.Add(new UniqueConstraint("PK_Categories", categories.Columns["CategoryID"])); 70 71 categories.PrimaryKey = new DataColumn[1] { categories.Columns["CategoryID"] }; 72 73 ds.Tables.Add(categories); 74 75 } 76 public static void AddForeignKeyConstraint(DataSet ds) 77 { 78 DataColumn parent = ds.Tables["Categories"].Columns["CategoryID"]; 79 DataColumn child = ds.Tables["Products"].Columns["CategoryID"]; 80 81 ForeignKeyConstraint fk = new ForeignKeyConstraint("FK_Product_CategoryID", parent, child); 82 83 fk.UpdateRule = Rule.Cascade; 84 fk.DeleteRule = Rule.SetNull; 85 86 // Create the constraint 87 // If this fails, you have a row in the products table with no associated category 88 ds.Tables["Products"].Constraints.Add(fk); 89 90 } 91 }
三、数据关联关系
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 DataSet ds = CreateDataSetWithRelationships(); 7 8 9 DataRow aBuilding = ds.Tables["Building"].NewRow(); 10 11 aBuilding["BuildingID"] = 1; 12 aBuilding["Name"] = "The Lowry"; 13 14 ds.Tables["Building"].Rows.Add(aBuilding); 15 16 17 DataRow aRoom = ds.Tables["Room"].NewRow(); 18 19 aRoom["RoomID"] = 1; 20 aRoom["Name"] = "Reception"; 21 aRoom["BuildingID"] = 1; 22 23 ds.Tables["Room"].Rows.Add(aRoom); 24 25 26 aRoom = ds.Tables["Room"].NewRow(); 27 aRoom["RoomID"] = 2; 28 aRoom["Name"] = "The Modern Art Gallery"; 29 aRoom["BuildingID"] = 1; 30 31 ds.Tables["Room"].Rows.Add(aRoom); 32 33 34 foreach (DataRow theBuilding in ds.Tables["Building"].Rows) 35 { 36 DataRow[] children = theBuilding.GetChildRows("Rooms"); 37 int roomCount = children.Length; 38 39 Console.WriteLine("Building {0} contains {1} room{2}", 40 theBuilding["Name"], 41 roomCount, 42 roomCount > 1 ? "s" : ""); 43 44 45 foreach (DataRow theRoom in children) 46 Console.WriteLine("Room: {0}", theRoom["Name"]); 47 } 48 49 50 foreach (DataRow theRoom in ds.Tables["Room"].Rows) 51 { 52 DataRow[] parents = theRoom.GetParentRows("Rooms"); 53 54 foreach (DataRow theBuilding in parents) 55 Console.WriteLine("Room {0} is contained in building {1}", theRoom["Name"], theBuilding["Name"]); 56 } 57 } 58 public static DataSet CreateDataSetWithRelationships() 59 { 60 DataSet ds = new DataSet("Relationships"); 61 62 ds.Tables.Add(CreateBuildingTable()); 63 ds.Tables.Add(CreateRoomTable()); 64 65 66 ds.Relations.Add("Rooms", ds.Tables["Building"].Columns["BuildingID"], ds.Tables["Room"].Columns["BuildingID"]); 67 68 return ds; 69 } 70 public static DataTable CreateBuildingTable() 71 { 72 DataTable aBuilding = new DataTable("Building"); 73 aBuilding.Columns.Add(new DataColumn("BuildingID", typeof(int))); 74 aBuilding.Columns.Add(new DataColumn("Name", typeof(string))); 75 aBuilding.Constraints.Add(new UniqueConstraint("PK_Building", aBuilding.Columns[0])); 76 aBuilding.PrimaryKey = new DataColumn[] { aBuilding.Columns[0] }; 77 78 return aBuilding; 79 } 80 81 public static DataTable CreateRoomTable() 82 { 83 DataTable aRoom = new DataTable("Room"); 84 aRoom.Columns.Add(new DataColumn("RoomID", typeof(int))); 85 aRoom.Columns.Add(new DataColumn("Name", typeof(string))); 86 aRoom.Columns.Add(new DataColumn("BuildingID", typeof(int))); 87 aRoom.Constraints.Add(new UniqueConstraint("PK_Room", aRoom.Columns[0])); 88 aRoom.PrimaryKey = new DataColumn[] { aRoom.Columns[0] }; 89 90 return aRoom; 91 } 92 }
时间: 2024-10-10 12:46:00