逐步解說:連接至物件中的資料
更新:2007 年 11 月
此逐步解說可示範如何建立物件來保存客戶和訂單資料,然後根據這些物件來建立物件資料來源。物件資料來源會出現在 [資料來源] 視窗中,其中的項目會拖曳到表單上,以在物件的公用屬性內建立繫結至資料的控制項。此逐步解說也將示範如何使用 TableAdapter,以便從資料庫擷取資料,並填入物件。
物件資料來源的建立方式,可以透過執行資料來源組態精靈並選取 [物件] 做為資料來源之型別。完成 [資料來源組態精靈] 之後,您就可以從資料來源視窗中,將可用物件的公用屬性拖曳至表單上。
注意事項: |
---|
您需要建置包含物件的專案,才能讓它出現在 [資料來源組態精靈] 上。如果精靈中未提供您的物件,請重新建置包含所需物件的專案。 |
逐步解說將說明的工作包括:
建立新的 [Windows 應用程式] 專案。
建立表示客戶和訂單的範例物件。
使用 [資料來源組態精靈],根據範例物件建立及設定應用程式中的物件資料來源。
將控制項加入到繫結至自訂物件中之資料的表單。
使用 TableAdapter 建立資料集,在物件和資料庫之間移動資料。
編輯 TableAdapter 的主查詢。
將查詢加入到 TableAdapter。
將資料庫的資料填入物件中。
建立專案
若要建立新的 Windows 應用程式專案
從 [檔案] 功能表中,建立 [新增專案]。
建立名稱為 ObjectBindingWalkthrough 的 [Windows 應用程式],再按一下 [確定]。如需詳細資訊,請參閱建立 Windows 架構的應用程式。
隨即建立 ObjectBindingWalkthrough 專案,並將其加入至 [方案總管]。
這個逐步解說需要繫結一些物件,所以第一個步驟是建立表示客戶和訂單的一些範例物件。若要表示客戶,將會建立一個表示單一客戶的 Customer 物件。若要表示訂單,將會建立一個表示單一訂單的 Order 物件以及一個表示 Order 物件集合的 Orders 物件。而 Customer 物件的集合則將使用 BindingSource 類別中的內建集合 (本逐步解說稍後會加以說明)。
建立 Customer 物件
若要建立 Customer 物件
在 [專案] 功能表上按一下 [加入類別]。
將新類別命名為 Customer,再按一下 [加入]。
將 Customer 類別檔中的程式碼替換成下列程式碼:
注意事項: Customer 物件包含型別 Orders 的 ordersCollection 屬性。編輯器將顯示指出 [型別 'Orders' 未定義] 的訊息。這是預期的錯誤訊息,當您在下節建立 Order 和 Orders 類別時,它就會消失。
''' <summary> ''' A single customer ''' </summary> Public Class Customer Public Sub New() End Sub ''' <summary> ''' Creates a new customer ''' </summary> ''' <param name="customerId">The ID that uniquely identifies this customer</param> ''' <param name="companyName">The name for this customer</param> ''' <param name="contactName">The name for this customer's contact</param> ''' <param name="contactTitle">The title for this contact</param> ''' <param name="address">The address for this customer</param> ''' <param name="city">The city for this customer</param> ''' <param name="region">The region for this customer</param> ''' <param name="postalCode">The postal code for this customer</param> ''' <param name="country">The country for this customer</param> ''' <param name="phone">The phone number for this customer</param> ''' <param name="fax">The fax number for this customer</param> Public Sub New(ByVal customerId As String, _ ByVal companyName As String, _ ByVal contactName As String, _ ByVal contactTitle As String, _ ByVal address As String, _ ByVal city As String, _ ByVal region As String, _ ByVal postalCode As String, _ ByVal country As String, _ ByVal phone As String, _ ByVal fax As String) customerIDValue = customerId companyNameValue = companyName contactNameValue = contactName contactTitleValue = contactTitle addressValue = address cityValue = city regionValue = region postalCodeValue = postalCode countryValue = country phoneValue = phone faxValue = fax End Sub Private customerIDValue As String ''' <summary> ''' The ID that uniquely identifies this customer ''' </summary> Public Property CustomerID() As String Get Return customerIDValue End Get Set(ByVal value As String) customerIDValue = value End Set End Property Private companyNameValue As String ''' <summary> ''' The name for this customer ''' </summary> Public Property CompanyName() As String Get Return companyNameValue End Get Set(ByVal Value As String) companyNameValue = Value End Set End Property Private contactNameValue As String ''' <summary> ''' The name for this customer's contact ''' </summary> Public Property ContactName() As String Get Return contactNameValue End Get Set(ByVal Value As String) contactNameValue = Value End Set End Property Private contactTitleValue As String ''' <summary> ''' The title for this contact ''' </summary> Public Property ContactTitle() As String Get Return contactTitleValue End Get Set(ByVal Value As String) contactTitleValue = Value End Set End Property Private addressValue As String ''' <summary> ''' The address for this customer ''' </summary> Public Property Address() As String Get Return addressValue End Get Set(ByVal Value As String) addressValue = Value End Set End Property Private cityValue As String ''' <summary> ''' The city for this customer ''' </summary> Public Property City() As String Get Return cityValue End Get Set(ByVal Value As String) cityValue = Value End Set End Property Private regionValue As String ''' <summary> ''' The region for this customer ''' </summary> Public Property Region() As String Get Return regionValue End Get Set(ByVal Value As String) regionValue = Value End Set End Property Private postalCodeValue As String ''' <summary> ''' The postal code for this customer ''' </summary> Public Property PostalCode() As String Get Return postalCodeValue End Get Set(ByVal Value As String) postalCodeValue = Value End Set End Property Private countryValue As String ''' <summary> ''' The country for this customer ''' </summary> Public Property Country() As String Get Return countryValue End Get Set(ByVal Value As String) countryValue = Value End Set End Property Private phoneValue As String ''' <summary> ''' The phone number for this customer ''' </summary> Public Property Phone() As String Get Return phoneValue End Get Set(ByVal Value As String) phoneValue = Value End Set End Property Private faxValue As String ''' <summary> ''' The fax number for this customer ''' </summary> Public Property Fax() As String Get Return faxValue End Get Set(ByVal Value As String) faxValue = Value End Set End Property Private ordersCollection As New System.ComponentModel.BindingList(Of Order) ''' <summary> ''' The orders for this customer ''' </summary> Public Property Orders() As System.ComponentModel.BindingList(Of Order) Get Return ordersCollection End Get Set(ByVal value As System.ComponentModel.BindingList(Of Order)) ordersCollection = value End Set End Property Public Overrides Function ToString() As String Return Me.CompanyName & " (" & Me.CustomerID & ")" End Function End Class
namespace ObjectBindingWalkthrough { /// <summary> /// A single customer /// </summary> public class Customer { /// <summary> /// Creates a new customer /// </summary> public Customer() { } /// <summary> /// Creates a new customer /// </summary> /// <param name="customerID"></param> /// <param name="companyName"></param> /// <param name="contactName"></param> /// <param name="contactTitle"></param> /// <param name="address"></param> /// <param name="city"></param> /// <param name="region"></param> /// <param name="postalCode"></param> /// <param name="country"></param> /// <param name="phone"></param> /// <param name="fax"></param> public Customer(string customerID, string companyName, string contactName, string contactTitle, string address, string city, string region, string postalCode, string country, string phone, string fax) { customerIDValue = customerID; } private string customerIDValue; /// <summary> /// The ID that uniquely identifies this customer /// </summary> public string CustomerID { get { return customerIDValue; } set { customerIDValue = value; } } private string companyNameValue; /// <summary> /// The name for this customer /// </summary> public string CompanyName { get { return companyNameValue; } set { companyNameValue = value; } } private string contactNameValue; /// <summary> /// The name for this customer's contact /// </summary> public string ContactName { get { return contactNameValue; } set { contactNameValue = value; } } private string contactTitleValue; /// <summary> /// The title for this contact /// </summary> public string ContactTitle { get { return contactTitleValue; } set { contactTitleValue = value; } } private string addressValue; /// <summary> /// The address for this customer /// </summary> public string Address { get { return addressValue; } set { addressValue = value; } } private string cityValue; /// <summary> /// The city for this customer /// </summary> public string City { get { return cityValue; } set { cityValue = value; } } private string regionValue; /// <summary> /// The region for this customer /// </summary> public string Region { get { return regionValue; } set { regionValue = value; } } private string postalCodeValue; /// <summary> /// The postal code for this customer /// </summary> public string PostalCode { get { return postalCodeValue; } set { postalCodeValue = value; } } private string countryValue; /// <summary> /// The country for this customer /// </summary> public string Country { get { return countryValue; } set { countryValue = value; } } private string phoneValue; /// <summary> /// The phone number for this customer /// </summary> public string Phone { get { return phoneValue; } set { phoneValue = value; } } private string faxValue; /// <summary> /// The fax number for this customer /// </summary> public string Fax { get { return faxValue; } set { faxValue = value; } } private System.ComponentModel.BindingList<Order> ordersCollection = new System.ComponentModel.BindingList<Order>(); public System.ComponentModel.BindingList<Order> Orders { get { return ordersCollection; } set { ordersCollection = value; } } public override string ToString() { return this.CompanyName + " (" + this.CustomerID + ")"; } } }
建立 Order 物件
建立 Order 物件和 Orders 集合
選取 [專案] 功能表上的 [加入類別]。
將新類別命名為 Order,再按一下 [加入]。
將 Order 類別檔中的程式碼替換成下列程式碼:
''' <summary> ''' A single order ''' </summary> Public Class Order Public Sub New() End Sub ''' <summary> ''' Creates a new order ''' </summary> ''' <param name="orderid">The identifier for this order</param> ''' <param name="customerID">The customer who placed this order</param> ''' <param name="employeeID">The ID of the employee who took this order</param> ''' <param name="orderDate">The date this order was placed</param> ''' <param name="requiredDate">The date this order is required</param> ''' <param name="shippedDate">The date the order was shipped</param> ''' <param name="shipVia">The shipping method for this order</param> ''' <param name="freight">The freight charge for this order</param> ''' <param name="shipName">The name of the recipient for this order</param> ''' <param name="shipAddress">The address to ship this order to</param> ''' <param name="shipCity">The city to ship this order to</param> ''' <param name="shipRegion">The region to ship this order to</param> ''' <param name="shipPostalCode">The postal code to ship this order to</param> ''' <param name="shipCountry">The country to ship this order to</param> Public Sub New(ByVal orderid As Integer, _ ByVal customerID As String, _ ByVal employeeID As Integer, _ ByVal orderDate As DateTime, _ ByVal requiredDate As DateTime, _ ByVal shippedDate As DateTime, _ ByVal shipVia As Integer, _ ByVal freight As Decimal, _ ByVal shipName As String, _ ByVal shipAddress As String, _ ByVal shipCity As String, _ ByVal shipRegion As String, _ ByVal shipPostalCode As String, _ ByVal shipCountry As String) orderIDValue = orderid customerIDValue = customerID employeeIDValue = employeeID orderDateValue = orderDate requiredDateValue = requiredDate shippedDateValue = shippedDate shipViaValue = shipVia freightValue = freight shipAddressValue = shipAddress shipCityValue = shipCity shipRegionValue = shipRegion shipPostalCodeValue = shipPostalCode shipCountryValue = shipCountry End Sub Private orderIDValue As Integer ''' <summary> ''' Identifier for this order ''' </summary> Public Property OrderID() As Integer Get Return orderIDValue End Get Set(ByVal value As Integer) orderIDValue = value End Set End Property Private customerIDValue As String ''' <summary> ''' The customer who placed this order ''' </summary> Public Property CustomerID() As String Get Return customerIDValue End Get Set(ByVal Value As String) customerIDValue = Value End Set End Property Private employeeIDValue As Integer ''' <summary> ''' The ID of the employee who took this order ''' </summary> Public Property EmployeeID() As Integer Get Return employeeIDValue End Get Set(ByVal Value As Integer) employeeIDValue = Value End Set End Property Private orderDateValue As DateTime ''' <summary> ''' The date this order was placed ''' </summary> Public Property OrderDate() As DateTime Get Return orderDateValue End Get Set(ByVal Value As DateTime) orderDateValue = Value End Set End Property Private requiredDateValue As DateTime ''' <summary> ''' The date this order is required ''' </summary> Public Property RequiredDate() As DateTime Get Return requiredDateValue End Get Set(ByVal Value As DateTime) requiredDateValue = Value End Set End Property Private shippedDateValue As DateTime ''' <summary> ''' The date this order was shipped ''' </summary> Public Property ShippedDate() As DateTime Get Return shippedDateValue End Get Set(ByVal Value As DateTime) shippedDateValue = Value End Set End Property Private shipViaValue As Integer ''' <summary> ''' The shipping method for this order ''' </summary> Public Property ShipVia() As Integer Get Return shipViaValue End Get Set(ByVal Value As Integer) shipViaValue = Value End Set End Property Private freightValue As Decimal ''' <summary> ''' The freight charge for this order ''' </summary> Public Property Freight() As Decimal Get Return freightValue End Get Set(ByVal Value As Decimal) freightValue = Value End Set End Property Private shipNameValue As String ''' <summary> ''' The name of the recipient for this order ''' </summary> Public Property ShipName() As String Get Return shipNameValue End Get Set(ByVal Value As String) shipNameValue = Value End Set End Property Private shipAddressValue As String ''' <summary> ''' The address to ship this order to ''' </summary> Public Property ShipAddress() As String Get Return shipAddressValue End Get Set(ByVal Value As String) shipAddressValue = Value End Set End Property Private shipCityValue As String ''' <summary> ''' The city to ship this order to ''' </summary> Public Property ShipCity() As String Get Return shipCityValue End Get Set(ByVal Value As String) shipCityValue = Value End Set End Property Private shipRegionValue As String ''' <summary> ''' The region to ship this order to ''' </summary> Public Property ShipRegion() As String Get Return shipRegionValue End Get Set(ByVal Value As String) shipRegionValue = Value End Set End Property Private shipPostalCodeValue As String ''' <summary> ''' The postal code to ship this order to ''' </summary> Public Property ShipPostalCode() As String Get Return shipPostalCodeValue End Get Set(ByVal Value As String) shipPostalCodeValue = Value End Set End Property Private shipCountryValue As String ''' <summary> ''' The country to ship this order to ''' </summary> Public Property ShipCountry() As String Get Return shipCountryValue End Get Set(ByVal Value As String) shipCountryValue = Value End Set End Property Private customerValue As Customer ''' <summary> ''' The customer this order belongs to ''' </summary> Public Property Customer() As Customer Get Return customerValue End Get Set(ByVal Value As Customer) customerValue = Value End Set End Property End Class ''' <summary> ''' A collection of Orders ''' </summary> Public Class Orders Inherits System.ComponentModel.BindingList(Of Order) End Class
using System; namespace ObjectBindingWalkthrough { /// <summary> /// A single order /// </summary> public class Order { /// <summary> /// Creates a new order /// </summary> public Order() { } /// <summary> /// Creates a new order /// </summary> /// <param name="orderid"></param> /// <param name="customerID"></param> /// <param name="employeeID"></param> /// <param name="orderDate"></param> /// <param name="requiredDate"></param> /// <param name="shippedDate"></param> /// <param name="shipVia"></param> /// <param name="freight"></param> /// <param name="shipName"></param> /// <param name="shipAddress"></param> /// <param name="shipCity"></param> /// <param name="shipRegion"></param> /// <param name="shipPostalCode"></param> /// <param name="shipCountry"></param> public Order(int orderid, string customerID, int employeeID, DateTime orderDate, DateTime requiredDate, DateTime shippedDate, int shipVia, decimal freight, string shipName, string shipAddress, string shipCity, string shipRegion, string shipPostalCode, string shipCountry) { } private int orderIDValue; /// <summary> /// The ID that uniquely identifies this order /// </summary> public int OrderID { get { return orderIDValue; } set { orderIDValue = value; } } private string customerIDValue; /// <summary> /// The customer who placed this order /// </summary> public string CustomerID { get { return customerIDValue; } set { customerIDValue = value; } } private int employeeIDValue; /// <summary> /// The ID of the employee who took this order /// </summary> public int EmployeeID { get { return employeeIDValue; } set { employeeIDValue = value; } } private DateTime orderDateValue; /// <summary> /// The date this order was placed /// </summary> public DateTime OrderDate { get { return orderDateValue; } set { orderDateValue = value; } } private DateTime requiredDateValue; /// <summary> /// The date this order is required /// </summary> public DateTime RequiredDate { get { return requiredDateValue; } set { requiredDateValue = value; } } private DateTime shippedDateValue; /// <summary> /// The date this order was shipped /// </summary> public DateTime ShippedDate { get { return shippedDateValue; } set { shippedDateValue = value; } } private int shipViaValue; /// <summary> /// The shipping method of this order /// </summary> public int ShipVia { get { return shipViaValue; } set { shipViaValue = value; } } private decimal freightValue; /// <summary> /// The freight charge for this order /// </summary> public decimal Freight { get { return freightValue; } set { freightValue = value; } } private string shipNameValue; /// <summary> /// The name of the recipient for this order /// </summary> public string ShipName { get { return shipNameValue; } set { shipNameValue = value; } } private string shipAddressValue; /// <summary> /// The address to ship this order to /// </summary> public string ShipAddress { get { return shipAddressValue; } set { shipAddressValue = value; } } private string shipCityValue; /// <summary> /// The city to ship this order to /// </summary> public string ShipCity { get { return shipCityValue; } set { shipCityValue = value; } } private string shipRegionValue; /// <summary> /// The region to ship this order to /// </summary> public string ShipRegion { get { return shipRegionValue; } set { shipRegionValue = value; } } private string shipPostalCodeValue; /// <summary> /// The postal code to ship this order to /// </summary> public string ShipPostalCode { get { return shipPostalCodeValue; } set { shipPostalCodeValue = value; } } private string shipCountryValue; /// <summary> /// The country to ship this order to /// </summary> public string ShipCountry { get { return shipCountryValue; } set { shipCountryValue = value; } } } /// <summary> /// A collection of Order objects /// </summary> class Orders: System.ComponentModel.BindingList<Order> { } }
從 [檔案] 功能表上,選擇 [全部儲存]。
建立物件資料來源
執行 [資料來源組態精靈],以便根據前一步驟建立的物件來建立資料來源。
建立物件資料來源
按一下 [資料] 功能表,並選擇 [顯示資料來源],即可開啟 [資料來源] 視窗。
按一下 [資料來源] 視窗中的 [加入新資料來源]。
選取 [選擇資料來源類型] 頁面上的 [物件],再按一下 [下一步]。
展開 [ObjectBindingWalkthrough] 節點,然後選取 [Customer] 物件。
注意事項: 如果沒有 [Customer] 物件,請按一下 [取消],再從 [建置] 功能表選取 [建置 ObjectBindingWalkthrough]。在成功建置專案之後,重新啟動精靈 (步驟 2),然後自訂物件即會出現。
按一下 [完成]。
[Customer] 物件隨即出現在 [資料來源] 視窗中。
建立資料繫結表單
繫結至 Customer 物件的控制項建立方式,是從 [資料來源] 視窗將項目拖曳到表單上。
若要建立繫結至物件屬性之控制項的表單
從 [方案總管] 中,選取 [Form1],再按一下 [檢視表設計工具]。
從 [資料來源] 視窗,將 [Customer] 節點拖曳至 [Form1]。
展開 [Customer] 節點,並從 [資料來源] 視窗將 [Orders] 節點拖曳到 [Form1] 上。
建立 TableAdapter,將資料庫中的資料載入到自訂物件中
若要在物件和資料庫之間移動資料,將會使用 TableAdapter。您可以使用資料來源組態精靈來建立客戶和訂單資料表的 TableAdapter。
若要建立 TableAdapter
選擇 [資料] 功能表上的 [加入新資料來源]。
選取 [選擇資料來源類型] 頁面上的 [資料庫]。
在 [選擇資料連接] 頁上,執行下列其中一項:
如果下拉式清單中有提供 Northwind 範例資料庫的資料連接,請選取這個資料連接。
-或-
選取 [新增連接],設定與 Northwind 資料庫之間的新資料連接。如需詳細資訊,請參閱 HOW TO:建立連接至 SQL Server 資料庫或 HOW TO:建立至 Access 資料庫的連接。
按一下 [將連接字串儲存到應用程式組態檔] 頁面上的 [下一步]。
在 [選擇您的資料庫物件] 頁面上,展開 [資料表] 節點。
選取 [Customers] 和 [Orders] 資料表,再按一下 [完成]。
[NorthwindDataSet] 會加入專案中,且 [Customers] 和 [Orders] 資料表會出現在 [NorthwindDataSet] 節點下方的 [資料來源] 視窗中。
將資料集和 TableAdapter 加入到 Form1
您可以從 [工具箱] 拖曳 CustomersTableAdapter、OrdersTableAdapter 和 NorthwindDataSet 的代表性元件,將它們的執行個體加入到表單上。
若要將 Customers 資料表中的資料填入 Customer 物件
從 [建置] 功能表中,選擇 [建置方案]。
從 [工具箱] 將 [NorthwindDataSet] 拖曳至 [Form1] 上。
從 [工具箱] 將 [CustomersTableAdapter] 拖曳至 [Form1] 上。
從 [工具箱] 將 [OrdersTableAdapter] 拖曳至 [Form1] 上。
將查詢加入到 CustomersTableAdapter,以便只傳回少數客戶
在實際的應用程式中,您可能永遠都不會傳回整個資料表。在此逐步解說中,將會傳回前五名客戶。
注意事項: |
---|
您通常會傳入參數,以選取想要傳回的客戶;但是為了簡潔起見,此逐步解說中會以硬式編碼方式來編碼查詢,以便僅傳回五名客戶,而且不需要建立使用者介面來輸入參數值。 |
若要將其他查詢加入至 CustomersTableAdapter
在 [方案總管] 中,按兩下 NorthwindDataSet.xsd 檔。
NorthwindDataSet 隨即在 [DataSet 設計工具] 中開啟。
以滑鼠右鍵按一下 [CustomersTableAdapter],並選取 [加入查詢]。
TableAdapter 查詢組態精靈隨即開啟。
保留 [使用 SQL 陳述式] 的預設值,再按一下 [下一步]。
保留 [傳回資料列的 SELECT] 的預設值,再按一下 [下一步]。
以下列項目取代 SQL 陳述式,再按一下 [下一步]:
SELECT Top 5 CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers
清除 [填入 DataTable] 核取方塊。
將 [傳回 DataTable] 方法命名為 GetTop5Customers,然後按一下 [完成]。
[GetTop5Customers] 查詢即會加入到 [CustomersTableAdapter]。
修改 OrdersTableAdapter 上的查詢,以便只傳回所需客戶的訂單
從資料庫擷取訂單時,您可能不會想要傳回訂單的整個資料表,而只需要特定客戶的訂單。下列程序將詳述如何重新設定具有新查詢的 TableAdapter (與上一個步驟中,將其他查詢加入 [CustomersTableAdapter] 剛好相反)。
若要重新設定 TableAdapter 的主要查詢來傳回單一客戶的訂單
以滑鼠右鍵按一下 [OrdersTableAdapter],並選擇 [設定]。
TableAdapter 查詢組態精靈 隨即開啟。
以下列項目取代 SQL 陳述式,再按一下 [下一步]:
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry FROM Orders WHERE CustomerID = @CustomerID
清除 [填入 DataTable] 核取方塊。
將 [傳回 DataTable] 方法命名為 GetDataByCustomerID,然後按一下 [完成]。
OrdersTableAdapter 的主要 Fill 查詢即會替換為 [GetDataByCustomerID] 查詢。
從 [建置] 功能表中選取 [建置方案],即可建置專案。
加入程式碼,以便將資料載入到 Customer 和 Order 物件中
若要將資料載入到自訂物件中,您可執行將傳回新資料表的 TableAdapter 查詢 (與填入現有資料表的 TableAdapter 查詢相反)。然後,程式碼在資料表中執行迴圈,並將客戶資訊填入每一個 Customer 物件,同時也會填入每一個 Customer.Orders 集合中的所有訂單。請注意每個 Customer 物件加入到 CustomerBindingSource 之內部集合 (CustomerBindingSource.Add(currentCustomer)) 的方式;也就是說,BindingSource 會提供可透過 List 屬性存取的 Customers 內建強型別集合。
若要載入具有資料的物件
從 [方案總管] 中,選取 [Form1],再按一下 [檢視程式碼]。
將 [Form1] 中的程式碼取代成下列程式碼:
Public Class Form1 Private Sub LoadCustomers() Dim customerData As NorthwindDataSet.CustomersDataTable = _ CustomersTableAdapter1.GetTop5Customers() Dim customerRow As NorthwindDataSet.CustomersRow For Each customerRow In customerData Dim currentCustomer As New Customer() With currentCustomer .CustomerID = customerRow.CustomerID .CompanyName = customerRow.CompanyName If Not customerRow.IsAddressNull Then .Address = customerRow.Address End If If Not customerRow.IsCityNull Then .City = customerRow.City End If If Not customerRow.IsContactNameNull Then .ContactName = customerRow.ContactName End If If Not customerRow.IsContactTitleNull Then .ContactTitle = customerRow.ContactTitle End If If Not customerRow.IsCountryNull Then .Country = customerRow.Country End If If Not customerRow.IsFaxNull Then .Fax = customerRow.Fax End If If Not customerRow.IsPhoneNull Then .Phone = customerRow.Phone End If If Not customerRow.IsPostalCodeNull Then .PostalCode = customerRow.PostalCode End If If Not customerRow.Is_RegionNull Then .Region = customerRow._Region End If End With LoadOrders(currentCustomer) CustomerBindingSource.Add(currentCustomer) Next End Sub Private Sub LoadOrders(ByRef currentCustomer As Customer) Dim orderData As NorthwindDataSet.OrdersDataTable = _ OrdersTableAdapter1.GetDataByCustomerID(currentCustomer.CustomerID) Dim orderRow As NorthwindDataSet.OrdersRow For Each orderRow In orderData Dim currentOrder As New Order() With currentOrder .OrderID = orderRow.OrderID .Customer = currentCustomer If Not orderRow.IsCustomerIDNull Then .CustomerID = orderRow.CustomerID End If If Not orderRow.IsEmployeeIDNull Then .EmployeeID = orderRow.EmployeeID End If If Not orderRow.IsFreightNull Then .Freight = orderRow.Freight End If If Not orderRow.IsOrderDateNull Then .OrderDate = orderRow.OrderDate End If If Not orderRow.IsRequiredDateNull Then .RequiredDate = orderRow.RequiredDate End If If Not orderRow.IsShipAddressNull Then .ShipAddress = orderRow.ShipAddress End If If Not orderRow.IsShipCityNull Then .ShipCity = orderRow.ShipCity End If If Not orderRow.IsShipCountryNull Then .ShipCountry = orderRow.ShipCountry End If If Not orderRow.IsShipNameNull Then .ShipName = orderRow.ShipName End If If Not orderRow.IsShippedDateNull Then .ShippedDate = orderRow.ShippedDate End If If Not orderRow.IsShipPostalCodeNull Then .ShipPostalCode = orderRow.ShipPostalCode End If If Not orderRow.IsShipRegionNull Then .ShipRegion = orderRow.ShipRegion End If If Not orderRow.IsShipViaNull Then .ShipVia = orderRow.ShipVia End If End With currentCustomer.Orders.Add(currentOrder) Next End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load LoadCustomers() End Sub End Class
using System; using System.Windows.Forms; namespace ObjectBindingWalkthrough { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load += Form1_Load; } private void LoadCustomers() { NorthwindDataSet.CustomersDataTable customerData = customersTableAdapter1.GetTop5Customers(); foreach (NorthwindDataSet.CustomersRow customerRow in customerData) { Customer currentCustomer = new Customer(); currentCustomer.CustomerID = customerRow.CustomerID; currentCustomer.CompanyName = customerRow.CompanyName; if (customerRow.IsAddressNull() == false) { currentCustomer.Address = customerRow.Address; } if (customerRow.IsCityNull() == false) { currentCustomer.City = customerRow.City; } if (customerRow.IsContactNameNull() == false) { currentCustomer.ContactName = customerRow.ContactName; } if (customerRow.IsContactTitleNull() == false) { currentCustomer.ContactTitle = customerRow.ContactTitle; } if (customerRow.IsCountryNull() == false) { currentCustomer.Country = customerRow.Country; } if (customerRow.IsFaxNull() == false) { currentCustomer.Fax = customerRow.Fax; } if (customerRow.IsPhoneNull() == false) { currentCustomer.Phone = customerRow.Phone; } if (customerRow.IsPostalCodeNull() == false) { currentCustomer.PostalCode = customerRow.PostalCode; } if (customerRow.IsRegionNull() == false) { currentCustomer.Region = customerRow.Region; } LoadOrders(currentCustomer); customerBindingSource.Add(currentCustomer); } } private void LoadOrders(Customer currentCustomer) { NorthwindDataSet.OrdersDataTable orderData = ordersTableAdapter1.GetDataByCustomerID(currentCustomer.CustomerID); foreach (NorthwindDataSet.OrdersRow orderRow in orderData) { Order currentOrder = new Order(); currentOrder.OrderID = orderRow.OrderID; if (orderRow.IsCustomerIDNull() == false) { currentOrder.CustomerID = orderRow.CustomerID; } if (orderRow.IsEmployeeIDNull() == false) { currentOrder.EmployeeID = orderRow.EmployeeID; } if (orderRow.IsFreightNull() == false) { currentOrder.Freight = orderRow.Freight; } if (orderRow.IsOrderDateNull() == false) { currentOrder.OrderDate = orderRow.OrderDate; } if (orderRow.IsRequiredDateNull() == false) { currentOrder.RequiredDate = orderRow.RequiredDate; } if (orderRow.IsShipAddressNull() == false) { currentOrder.ShipAddress = orderRow.ShipAddress; } if (orderRow.IsShipCityNull() == false) { currentOrder.ShipCity = orderRow.ShipCity; } if (orderRow.IsShipCountryNull() == false) { currentOrder.ShipCountry = orderRow.ShipCountry; } if (orderRow.IsShipNameNull() == false) { currentOrder.ShipName = orderRow.ShipName; } if (orderRow.IsShippedDateNull() == false) { currentOrder.ShippedDate = orderRow.ShippedDate; } if (orderRow.IsShipPostalCodeNull() == false) { currentOrder.ShipPostalCode = orderRow.ShipPostalCode; } if (orderRow.IsShipRegionNull() == false) { currentOrder.ShipRegion = orderRow.ShipRegion; } if (orderRow.IsShipViaNull() == false) { currentOrder.ShipVia = orderRow.ShipVia; } currentCustomer.Orders.Add(currentOrder); } } private void Form1_Load(object sender, EventArgs e) { LoadCustomers(); } } }
測試應用程式
若要測試應用程式
按下 F5,執行應用程式。
表單隨即開啟,且會將範例資料填入 DataGridView 控制項。
巡覽 DataGridView 中的客戶,以顯示其關聯的訂單。
後續步驟
若要在應用程式中加入功能
加入程式碼,將自訂物件中的資料傳回資料庫。如需詳細資訊,請參閱 HOW TO:以 TableAdapter 直接存取資料庫。
選取 DataGridView 智慧標籤上的 [編輯資料行],重新排列 [DataGridView] 中的資料行。