1DataSet 1 DataSet DataSetSystem.Data DataSet DataTableDataRelation DataTable DataSet DataSetTyped DataSet DataSet DataSetDataTable DataSetDataTable DataTable 45 DataSet DataSet DataSetTypedUntyped DataSetsDataSet DataTableDataTableDataTable DataSetTablesDataSetSchema DataSetDataTable DataColumnDataSets.xsd CustomersTable Visual StudioDataSet DataSetDataSet DataTable DataRelation DataSetData Source Configuration Wizard Microsoft.NET Framework 2.0 Windows
7 DataSet DataSet TableColumnDataSet DataSet NOTE DataSet DataAdapterDataSetDataAdapter DataSet DataSetDataSetDataSet NorthwindDataSet DataSet Dim NorthwindDataSet As New DataSet ("NorthwindDataSet") DataSet NorthwindDataSet = new DataSet ("NorthwindDataSet"); DataSetDataTable DataSetDataSet DataTableNorthwindDataSet CustomersTableOrdersDataTableDataTable ' DataTable Dim CustomersTable As New DataTable Dim OrdersDataTable As New DataTable ' DataTableDataSetTables NorthwindDataSet.Tables.Add(CustomersTable) NorthwindDataSet.Tables.Add(OrdersDataTable)
1DataSet // DataTable DataTable CustomersTable = new DataTable (); DataTable OrdersDataTable = new DataTable (); // DataTableDataSetTables NorthwindDataSet.Tables.Add(CustomersTable); NorthwindDataSet.Tables.Add(OrdersDataTable); DataSetDataTable DataSetDataTableDataRelation DataTable DataRelation Constructor DataSet Relations NorthwindDataSet CustomersOrdersDataTableCustomerID ' DataRelation Dim CustomersOrders As New DataRelation ("CustomersOrders", _ CustomersTable.Columns("CustomerID"), OrdersTable.Columns("CustomerID")) ' DataSet NorthwindDataSet.Relations.Add(CustomersOrders) // DataRelation DataRelation CustomersOrders = new DataRelation ("CustomersOrders", CustomersTable.Columns["CustomerID"], OrdersTable.Columns["CustomerID"]); // DataSet NorthwindDataSet.Relations.Add(CustomersOrders); Microsoft.NET Framework 2.0 Windows
7 DataTable DataTable DataRowDataRowGetParentRow GetChildRowsGetParentRow DataRowGetChildRows DataRow Order Customer Dim Customer As DataRow = SelectedOrdersRow.GetParentRow("FK_Orders_Customers") DataRow Customer = SelectedOrdersRow.GetParentRow("FK_Orders_Customers"); Customer Order Dim Orders() As DataRow = SelectedCustomersRow.GetChildRows( _ "FK_Orders_Customers") DataRow[] Orders = SelectedCustomersRow.GetChildRows("FK_Orders_Customers"); DataSet DataSetMergeDataSetDataSet DataSet DataSetDataSet DataSet DataSetMergePreserveChanges DataSetTrue DataSet DataSetMerge
1DataSet MissingSchemaActionDataSet DataSetMissingSchemaAction AddDataSetDataSet DataSet AddWithKey DataSet ErrorDataSetDataSet Merge IgnoreDataSetDataSet O l d S a l e s D a t a S e t S a l e s - HistoryDataSetPreserveChangesTrue SalesHistoryDataSet.Merge(OldSalesDataSet, True, MissingSchemaAction.Ignore) SalesHistoryDataSet.Merge(OldSalesDataSet, true, MissingSchemaAction.Ignore); DataSet DataSet DataSet DataSetCopy Dim CopyOfDataSet As New DataSet CopyOfDataSet = OriginalDataSet.Copy DataSet CopyOfDataSet = new DataSet(); CopyOfDataSet = OriginalDataSet.Copy(); Microsoft.NET Framework 2.0 Windows
第7章 離線環境下的資料編輯作業 Lab 建立DataSet物件 在下列的練習裡將分別建立具型別 Typed 與不具型別 Untyped 的 DataSet物件 Exercise 1 利用DataSet設計工具建立DataSet DataSet設計工具是專供設計階段使用的工具 藉由自伺服器總管拖曳資料 庫裡的資料表的方式 來輔助建立具型別的DataSet物件 當資料表被拖曳 至設計工具上時 這些資料表便自動被轉換成DataSet裡具型別的物件 做 為資料存取的程式碼將大幅度地被簡化 1. 首先先建立一個名為DataSetDesignerExample的視窗應用程式 2. 自 專案 功能表裡選擇執行 加入新項目 選項 3. 選擇資料集樣版 並命名為NorthwindDataSet.xsd 4. 自伺服器總管理挑選Customers資料表 並將其拖曳至設計工具上 5. 自伺服器總管理挑選Orders資料表 並將其拖曳至設計工具上 完成拖曳Customers以及Orders資料表至DataSet設計工具上後 此時 的設計工具看起來應接近如圖7-1所示 圖7-1 DataSet設計工具上對應至Customers以及Orders這兩個資料表的 DataTable物件以及TableAdapter物件
1DataSet 6. Build 7. ListBoxForm1Customers- ListBox 8. Form1 o Name = GetCustomersButton o Text = Get Customers 9. Get CustomersGetCustomers- Button_Click ' Northwind DataSet Dim NorthwindDataSet1 As New NorthwindDataSet ' CustomersTableAdapter Dim CustomersTableAdapter1 As New NorthwindDataSetTableAdapters. CustomersTableAdapter ' FillCustomers DataTable CustomersTableAdapter1.Fill(NorthwindDataSet1.Customers) ' CustomersCompanyNameListBox For Each NWCustomer As NorthwindDataSet.CustomersRow In NorthwindDataSet1.Customers.Rows CustomersListBox.Items.Add(NWCustomer.CompanyName) Next // Northwind DataSet NorthwindDataSet NorthwindDataSet1 = new NorthwindDataSet (); // CustomersTableAdapter NorthwindDataSetTableAdapters.CustomersTableAdapter CustomersTableAdapter1 = new NorthwindDataSetTableAdapters.CustomersTableAdapter(); // FillCustomers DataTable CustomersTableAdapter1.Fill(NorthwindDataSet1.Customers); // CustomersCompanyNameListBox foreach (NorthwindDataSet.CustomersRow NWCustomer in NorthwindDataSet1. Microsoft.NET Framework 2.0 Windows
7 Customers.Rows) { CustomersListBox.Items.Add(NWCustomer.CompanyName); } 10.Get Customers CompanyName7-2CustomersListBox 7-2Get CustomersCustomersListBox CompanyName Exercise 2DataSet Data Source Configuration Wizard DataSet 1. DataSourceWizardExample 2. NOTE Data 10
1DataSet 3. 7-3Choose A Data Source Type 7-3 4. 7-4 Choose Your Data Connection Northwind 7-4 Microsoft.NET Framework 2.0 Windows 11
7 5. 7-5 Application Configuration File 7-5 6. 7-6 Customers 7-6 12
1DataSet 7. DataSet Data-bound 8. Form1 9. CustomersForm17-7 7-7CustomersVisual Studio IDEForm1 CustomersVisual Studio Customers DataTable 10. Customers7-8 Microsoft.NET Framework 2.0 Windows 13
第7章 離線環境下的資料編輯作業 圖7-8 顯示於Form1上的Customers資料 Exercise 3 設定不具型別的DataSet物件 自工具箱裡拖曳DataSet物件至表單上 以建立不具型別的DataSet物件 1. 建立一個名為UntypedDataSet的視窗應用程式 2. 自工具箱的資料 Data 區段裡拖曳一個DataSet物件至Form1之上 3. 自 加入資料集 Add DataSet 的對話方塊裡 選擇 不具型別 資料集 並按 確定 4. 拖曳一個DataGridView控制項至表單之上 5. 選取元件匣上的DataSet1執行個體 並切換至屬性視窗上的Tables屬 性 用滑鼠點擊Tables屬性後方的Ellipsis( )按鈕 以開啟 資料表 集合編輯器 Tables Collection Editor 6. 加入一個資料表並將其Name以及TableName屬性設定為Categories 7. 選取Columns屬性並用滑鼠點擊Ellipsis 按鈕 以開啟 資料行 集合編輯器 Columns Collection Editor 8. 加入一個資料行 並做以下設定 o AllowDBNull = False o AutoIncrement = True 14
1DataSet o ColumnName = CategoryID o DataType = System.Int32 o Name = CategoryID 9. o AllowDBNull = False o ColumnName = CategoryName o Name = CategoryName 10. Constraints Unique Constraint 11. CategoryIDPrimary key 12. Constraints Collection Editor 13. o Name = FillDataSetButton o Text = Fill DataSet 14. Form1_Load DataGridView1.DataSource = DataSet1.Tables("Categories") DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect datagridview1.datasource = DataSet1.Tables["Categories"]; datagridview1.selectionmode = DataGridViewSelectionMode.FullRowSelect; 15. FillDataSetButton_Click Dim newrow As DataRow = DataSet1.Tables("Categories").NewRow() newrow.item("categoryname") = "Beverages" DataSet1.Tables("Categories").Rows.Add(newRow) Dim newrow2 As DataRow = DataSet1.Tables("Categories").NewRow() newrow2.item("categoryname") = "Condiments" Microsoft.NET Framework 2.0 Windows 15
7 DataSet1.Tables("Categories").Rows.Add(newRow2) Dim newrow3 As DataRow = DataSet1.Tables("Categories").NewRow() newrow3.item("categoryname") = "Seafood" DataSet1.Tables("Categories").Rows.Add(newRow3) DataRow newrow = DataSet1.Tables["Categories"].NewRow(); newrow["categoryname"] = "Beverages"; DataSet1.Tables["Categories"].Rows.Add(newRow); DataRow newrow2 = DataSet1.Tables["Categories"].NewRow(); newrow2["categoryname"] = "Condiments"; DataSet1.Tables["Categories"].Rows.Add(newRow2); DataRow newrow3 = DataSet1.Tables["Categories"].NewRow(); newrow3["categoryname"] = "Seafood"; DataSet1.Tables["Categories"].Rows.Add(newRow3); 16. Form1DataSet1 17. Tables DataSet 18. NameTableNameProducts 19. o AllowDBNull = False o AutoIncrement = True o ColumnName = ProductID o DataType = System.Int32 o Name = ProductID 20. o AllowDBNull = False o ColumnName = ProductName o DataType = System.String o Name = ProductName 16
1DataSet 21. o AllowDBNull = False o ColumnName = CategoryID o DataType = System.Int32 o Name = CatID 22. ConstraintsProducts 23. ProductID 24. Foreign Key Constraint o = Categories o = Products o = CategoryID o = CategoryID 25. 26. 27. DataSet1Relations EllipsisDataSet1DataRelation 28. Relations Collection Editor o = CategoriesProducts o = Categories o = Products o = CategoryID o = CategoryID 29. 30. FilllDataSetButton_Click Microsoft.NET Framework 2.0 Windows 17
7 Dim newrow4 As DataRow = DataSet1.Tables("Products").NewRow() newrow4.item("categoryid") = 1 newrow4.item("productname") = "Chai" DataSet1.Tables("Products").Rows.Add(newRow4) Dim newrow5 As DataRow = DataSet1.Tables("Products").NewRow() newrow5.item("categoryid") = 2 newrow5.item("productname") = "Aniseed Syrup" DataSet1.Tables("Products").Rows.Add(newRow5) Dim newrow6 As DataRow = DataSet1.Tables("Products").NewRow() newrow6.item("categoryid") = 3 newrow6.item("productname") = "Ikura" DataSet1.Tables("Products").Rows.Add(newRow6) Dim newrow7 As DataRow = DataSet1.Tables("Products").NewRow() newrow7.item("categoryid") = 1 newrow7.item("productname") = "Chang" DataSet1.Tables("Products").Rows.Add(newRow7) Dim newrow8 As DataRow = DataSet1.Tables("Products").NewRow() newrow8.item("categoryid") = 2 newrow8.item("productname") = "Chef Anton's Gumbo Mix" DataSet1.Tables("Products").Rows.Add(newRow8) Dim newrow9 As DataRow = DataSet1.Tables("Products").NewRow() newrow9.item("categoryid") = 3 newrow9.item("productname") = "Boston Crab Meat" DataSet1.Tables("Products").Rows.Add(newRow9) DataRow newrow4 = DataSet1.Tables["Products"].NewRow(); newrow4["categoryid"] = 1; newrow4["productname"] = "Chai"; DataSet1.Tables["Products"].Rows.Add(newRow4); DataRow newrow5 = DataSet1.Tables["Products"].NewRow(); newrow5["categoryid"] = 2; newrow5["productname"] = "Aniseed Syrup"; DataSet1.Tables["Products"].Rows.Add(newRow5); DataRow newrow6 = DataSet1.Tables["Products"].NewRow(); 18
1DataSet newrow6["categoryid"] = 3; newrow6["productname"] = "Ikura"; DataSet1.Tables["Products"].Rows.Add(newRow6); DataRow newrow7 = DataSet1.Tables["Products"].NewRow(); newrow7["categoryid"] = 1; newrow7["productname"] = "Chang"; DataSet1.Tables["Products"].Rows.Add(newRow7); DataRow newrow8 = DataSet1.Tables["Products"].NewRow(); newrow8["categoryid"] = 2; newrow8["productname"] = "Chef Anton's Gumbo Mix"; DataSet1.Tables["Products"].Rows.Add(newRow8); DataRow newrow9 = DataSet1.Tables["Products"].NewRow(); newrow9["categoryid"] = 3; newrow9["productname"] = "Boston Crab Meat"; DataSet1.Tables["Products"].Rows.Add(newRow9); 31. DataGridView1_CellDoubleClick ' CategoryID Dim Category As Integer = CInt(DataGridView1.SelectedRows(0). Cells("CategoryID").Value) ' DataRow Dim rows() As DataRow = DataSet1.Tables("Categories").Select("CategoryID = " & Category) ' GetChildRows Dim ProductList As String = "" For Each r As DataRow In rows(0).getchildrows("categoriesproducts") ProductList += r.item("productname").tostring & Environment.NewLine Next ' MessageBox.Show(ProductList) // CategoryID int Category = (int)datagridview1.selectedrows[0].cells["categoryid"]. Microsoft.NET Framework 2.0 Windows 19
7 Value; // DataRow DataRow[] rows = DataSet1.Tables["Categories"].Select("CategoryID = " + Category); // GetChildRows string ProductList = ""; foreach (DataRow r in rows[0].getchildrows("categoriesproducts")) { ProductList += r["productname"].tostring() + Environment.NewLine; } // MessageBox.Show(ProductList); 32. Fill DataSet 33. Category Product DataSet DataTableDataRelationDataSet DataSetDataSet Designer Data Source Configuration WizardDataSet DataSetTableAdapter DataSet DataSet DataSet 20
1DataSet 1. DataSet A. Pointer B. DataTableDataRelation C. D. 2. DataSet A. DataTableDataRelationDataAdapter B. DataTableDataColumnDataRelation C. DataTableDataRelationConstraint D. DataTableDataColumnType 3. DataSet A. DataSetGetParentRowGetChildRows B. DataTableGetParentRowGetChildRows C. DataRowGetParentRowGetChildRows D. DataRelationParentColumnsChildColumns NOTE Microsoft.NET Framework 2.0 Windows 21