Share via


Visual Studio에서 개체 바인딩

업데이트: 2007년 11월

Visual Studio에서는 응용 프로그램의 데이터 소스로 사용자 지정 개체(데이터 집합과 웹 서비스의 반대의 의미)와 함께 사용할 수 있는 디자인 타임 도구를 제공합니다.

사용자 지정 개체를 Visual Studio의 데이터 디자인 도구와 함께 사용하려면 해당 개체에 하나 이상의 public 속성이 있어야 합니다. 사용자 지정 개체를 데이터 소스 창과 같은 도구와 함께 사용하는 데 있어서 특정 생성자나 특성은 필요하지 않습니다.

개체를 데이터 소스로 사용할 때 응용 프로그램 논리를 구현하는 방법은 매우 많지만, Visual Studio에서 생성된 새 TableAdapter 개체를 사용하여 단순화할 수 있는 표준 작업이 몇 가지 있습니다. 이 페이지에서는 TableAdapter를 사용하여 이러한 표준 프로세스를 구현하는 방법을 설명합니다. 이 방법은 사용자 지정 개체를 만들기 위한 지침은 아닙니다. 예를 들어, 개체의 특정 구현이나 응용 프로그램의 논리에 관계없이 다음 표준 작업을 수행하게 됩니다.

  • 개체에 데이터 로드(보통 데이터베이스로부터)

  • 개체의 형식화된 컬렉션 만들기

  • 컬렉션에 개체 추가 및 컬렉션에서 개체 제거

  • 폼에서 사용자에게 개체 데이터 표시

  • 개체의 데이터 변경/편집

  • 개체의 데이터를 다시 데이터베이스로 저장

참고:

사용자의 이해를 돕고 이 페이지의 예제에 대한 컨텍스트를 제공하기 위해 다음 연습을 완료하는 것이 좋습니다. 연습: 개체의 데이터에 연결 이 연습에서는 이 도움말 페이지에 설명되어 있는 개체를 만듭니다.

개체에 데이터 로드

이 예제에서는 TableAdapter를 사용하여 개체에 데이터를 로드합니다. 기본적으로 TableAdapter는 데이터베이스에서 데이터를 페치하고 데이터 테이블을 채우는 두 종류의 메서드로 만들어집니다.

  • TableAdapter.Fill 메서드는 기존 데이터 테이블을 반환된 데이터로 채웁니다.

  • TableAdapter.GetData 메서드는 데이터로 채워진 새 데이터 테이블을 반환합니다.

데이터가 있는 사용자 지정 개체를 로드하는 가장 쉬운 방법은 TableAdapter.GetData 메서드를 호출하고 반환된 데이터 테이블의 행 컬렉션을 순환 검색한 다음 각 개체를 각 행의 값으로 채우는 것입니다. TableAdapter에 추가된 쿼리에 대해 채워진 데이터 테이블을 반환하는 GetData 메서드를 만들 수 있습니다.

참고:

Visual Studio는 TableAdapter 쿼리의 이름을 기본적으로 Fill과 GetData로 지정합니다. 그러나 이러한 이름은 임의의 유효한 메서드 이름으로 변경할 수 있습니다.

다음 예제에서는 데이터 테이블의 행을 순환 검색하고 개체를 데이터로 채우는 방법을 보여 줍니다.

자세한 코드 예제는 연습: 개체의 데이터에 연결을 참조하십시오.

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 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);
    }
}

개체의 형식화된 컬렉션 만들기

개체에 대해 컬렉션 클래스를 만들거나 BindingSource 구성 요소에 자동으로 제공되는 형식화된 컬렉션을 사용할 수 있습니다.

개체에 대해 사용자 지정 컬렉션 클래스를 만드는 경우 BindingList<T>에서 상속하는 것이 좋습니다. 이 일반적인 클래스는 컬렉션을 관리하는 기능뿐 아니라 Windows Forms의 데이터 바인딩 인프라에 알림을 보내는 이벤트를 발생시키는 기능을 제공합니다.

BindingSource의 자동으로 생성된 컬렉션은 해당 형식화된 컬렉션에 BindingList<T>을 사용합니다. 응용 프로그램에 추가 기능이 필요하지 않는 경우 BindingSource 내에서 컬렉션을 유지할 수 있습니다. 자세한 내용은 BindingSource 클래스의 List 속성을 참조하십시오.

참고:

컬렉션에 BindingList<T>의 기본 구현으로 제공되지 않는 기능이 필요한 경우 사용자 지정 컬렉션을 만들어야 필요한 경우 클래스에 추가할 수 있습니다.

다음 코드에서는 Order 개체의 강력한 형식의 컬렉션에 대한 클래스를 만드는 방법을 보여 줍니다.

''' <summary>
''' A collection of Orders
''' </summary>
Public Class Orders
    Inherits System.ComponentModel.BindingList(Of Order)

    ' Add any additional functionality required by your collection.

End Class
/// <summary>
/// A collection of Orders
/// </summary>
public class Orders: System.ComponentModel.BindingList<Order>
{
    // Add any additional functionality required by your collection.
}

컬렉션에 개체 추가

사용자 지정 컬렉션 클래스 또는 BindingSource의 Add 메서드를 호출하여 컬렉션에 개체를 추가합니다.

BindingSource를 사용하여 컬렉션에 추가하는 방법의 예제를 보려면 연습: 개체의 데이터에 연결의 LoadCustomers 메서드를 참조하십시오.

사용자 지정 컬렉션에 개체를 추가하는 방법의 예제를 보려면 연습: 개체의 데이터에 연결의 LoadOrders 메서드를 참조하십시오.

참고:

Add 메서드는 BindingList<T>에서 상속할 때 사용자 지정 컬렉션에 자동으로 제공됩니다.

다음 코드에서는 BindingSource의 형식화된 컬렉션에 개체를 추가하는 방법을 보여 줍니다.

Dim currentCustomer As New Customer()
CustomerBindingSource.Add(currentCustomer)
Customer currentCustomer = new Customer();
customerBindingSource.Add(currentCustomer);

다음 코드에서는 BindingList<T>에서 상속한 형식화된 컬렉션에 개체를 추가하는 방법을 보여 줍니다.

참고:

이 예제에서 Orders 컬렉션은 Customer 개체의 속성입니다.

Dim currentOrder As New Order()
currentCustomer.Orders.Add(currentOrder)
Order currentOrder = new Order();
currentCustomer.Orders.Add(currentOrder);

컬렉션에서 개체 제거

사용자 지정 컬렉션 클래스 또는 BindingSource의 Remove 또는 RemoveAt 메서드를 호출하여 컬렉션에서 개체를 제거합니다.

참고:

Remove 및 RemoveAt 메서드는 BindingList<T>에서 상속할 때 사용자 지정 컬렉션에 자동으로 제공됩니다.

다음 코드에서는 RemoveAt 메서드가 있는 BindingSource의 형식화된 컬렉션에서 개체를 찾고 제거하는 방법을 보여 줍니다.

Dim customerIndex As Integer = CustomerBindingSource.Find("CustomerID", "ALFKI")
CustomerBindingSource.RemoveAt(customerIndex)
int customerIndex = customerBindingSource.Find("CustomerID", "ALFKI");
customerBindingSource.RemoveAt(customerIndex);

사용자에게 개체 데이터 표시

개체의 데이터를 사용자에게 표시하려면 데이터 소스 구성 마법사를 사용하여 개체 데이터 소스를 만든 다음 데이터 소스 창에서 전체 개체 또는 개별 속성을 폼으로 끌어 놓습니다.

개체 데이터 소스 만들기에 대한 자세한 내용은 방법: 개체의 데이터에 연결을 참조하십시오.

Windows Forms에서 개체의 데이터 표시에 대한 자세한 내용은 Windows 응용 프로그램에서 폼에 데이터 표시를 참조하십시오.

개체에서 데이터 수정

Windows Forms 컨트롤에 데이터 바인딩된 사용자 지정 개체의 데이터를 편집하려면 바인딩된 컨트롤(또는 해당 개체의 속성에서 직접)에서 데이터를 간단히 편집하십시오. 데이터 바인딩 아키텍처가 개체의 데이터를 업데이트합니다.

응용 프로그램에서 변경 내용을 추적하고 제안된 변경 내용을 해당 원래 값으로 롤백해야 하는 경우 개체 모형에서 이 기능을 구현해야 합니다. 데이터 테이블에서 제안된 변경 내용을 추적하는 방법에 대한 예제를 보려면 DataRowState, HasChangesGetChanges를 참조하십시오.

개체의 데이터를 다시 데이터베이스로 저장

개체에서 TableAdapter의 DBDirect 메서드로 값을 전달하여 데이터를 다시 데이터베이스로 저장합니다.

Visual Studio에서는 데이터베이스에 대해 직접 실행할 수 있는 DBDirect 메서드를 만듭니다. 이러한 메서드에는 DataSet 또는 DataTable 개체가 필요하지 않습니다.

TableAdapter DBDirect 메서드

설명

TableAdapter.Insert

데이터베이스에 새 레코드를 추가하여 개별 열 값을 메서드 매개 변수로 전달할 수 있습니다.

TableAdapter.Update

데이터베이스의 기존 레코드를 업데이트합니다. Update 메서드는 원래 열 값과 새 열 값을 메서드 매개 변수로 사용합니다. 원래 값은 원래 레코드를 찾는 데 사용되고 새 값은 해당 레코드를 업데이트하는 데 사용됩니다.

또한 TableAdapter.Update 메서드를 사용하면 DataSet, DataTable, DataRow 또는 DataRow의 배열을 메서드 매개 변수로 사용하여 데이터 집합의 변경 내용을 다시 데이터베이스에 적용할 수도 있습니다.

TableAdapter.Delete

매서드 매개 변수로 전달된 원래 열 값을 기반으로 데이터베이스에서 기존 레코드를 삭제합니다.

개체 컬렉션의 데이터를 저장하려면 개체 컬렉션을 순환 검색하고(예: for-next 루프 사용) TableAdapter의 DBDirect 메서드를 사용하여 각 개체의 값을 데이터베이스로 보냅니다.

다음 예제에서는 TableAdapter.Insert DBDirect 메서드를 사용하여 새 고객을 데이터베이스에 직접 추가하는 방법을 보여 줍니다.

Private Sub AddNewCustomer(ByVal currentCustomer As Customer)

    CustomersTableAdapter.Insert( _
        currentCustomer.CustomerID, _
        currentCustomer.CompanyName, _
        currentCustomer.ContactName, _
        currentCustomer.ContactTitle, _
        currentCustomer.Address, _
        currentCustomer.City, _
        currentCustomer.Region, _
        currentCustomer.PostalCode, _
        currentCustomer.Country, _
        currentCustomer.Phone, _
        currentCustomer.Fax)
End Sub
private void AddNewCustomers(Customer currentCustomer)
{
    customersTableAdapter.Insert( 
        currentCustomer.CustomerID, 
        currentCustomer.CompanyName, 
        currentCustomer.ContactName, 
        currentCustomer.ContactTitle, 
        currentCustomer.Address, 
        currentCustomer.City, 
        currentCustomer.Region, 
        currentCustomer.PostalCode, 
        currentCustomer.Country, 
        currentCustomer.Phone, 
        currentCustomer.Fax);
}

참고 항목

작업

방법: 개체의 데이터에 연결

연습: 개체의 데이터에 연결

방법: 개체에서 데이터베이스로 데이터 저장

방법: TableAdapter를 사용하여 데이터베이스에 직접 액세스

연습: TableAdapter DBDirect 메서드를 사용하여 데이터 저장

기타 리소스

Windows 응용 프로그램에서 폼에 데이터 표시

TableAdapter

데이터 저장