다음을 통해 공유


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

 

게시 날짜: 2016년 4월

개체에서 TableAdapter의 DBDirect 메서드(예: TableAdapter.Insert) 중 하나로 값을 전달하여 개체의 데이터를 데이터베이스에 저장할 수 있습니다. 자세한 내용은 TableAdapter 개요를 참조하십시오.

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

기본적으로 DBDirect 메서드는 데이터베이스에 대해 직접 실행할 수 있는 TableAdapter에서 만들어집니다. 이러한 메서드는 직접 호출될 수 있으며 업데이트를 데이터베이스로 보내기 위해 변경 내용을 조정하는 DataSet 또는 DataTable 개체를 필요로 하지 않습니다.

참고

TableAdapter를 구성할 때 주 쿼리에서는 DBDirect 메서드를 만들 수 있는 충분한 정보를 제공해야 합니다. 예를 들어, TableAdapter가 정의된 기본 키 열이 없는 테이블의 데이터를 쿼리하도록 구성되어 있는 경우 DBDirect 메서드는 생성되지 않습니다.

TableAdapter DBDirect 메서드 설명
TableAdapter.Insert 데이터베이스에 새 레코드를 추가하여 개별 열 값을 메서드 매개 변수로 전달할 수 있습니다.
TableAdapter.Update 데이터베이스의 기존 레코드를 업데이트합니다. Update 메서드는 원래 열 값과 새 열 값을 메서드 매개 변수로 사용합니다. 원래 값은 원래 레코드를 찾는 데 사용되고 새 값은 해당 레코드를 업데이트하는 데 사용됩니다.

또한 TableAdapter.Update 메서드를 사용하면 DataSet, DataTable, DataRow 또는 DataRow의 배열을 메서드 매개 변수로 사용하여 데이터 집합의 변경 내용을 다시 데이터베이스에 적용할 수도 있습니다.
TableAdapter.Delete 매서드 매개 변수로 전달된 원래 열 값을 기반으로 데이터베이스에서 기존 레코드를 삭제합니다.

개체의 새 레코드를 데이터베이스에 저장하려면

  • 값을 TableAdapter.Insert 메서드에 전달하여 레코드를 만듭니다.

    다음 예제에서는 currentCustomer 개체의 값을 TableAdapter.Insert 메서드에 전달하여 Customers 테이블에 새 고객 레코드를 만듭니다.

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

개체의 기존 레코드를 데이터베이스로 업데이트하려면

  • TableAdapter.Update 메서드를 호출하고 레코드를 업데이트할 새 값과 해당 레코드를 찾을 원래 값을 전달하여 레코드를 수정합니다.

    참고

    개체는 Update 메서드에 전달할 수 있도록 원래 값을 유지해야 합니다. 이 예제에서는 orig 접두사가 있는 속성을 사용하여 원래 값을 저장합니다.

    다음 예제에서는 Customer 개체의 새 값과 원래 값을 TableAdapter.Update 메서드에 전달하여 Customers 테이블의 기존 레코드를 업데이트합니다.

            private void UpdateCustomer(Customer cust)
            {
                customersTableAdapter.Update(
                    cust.CustomerID,
                    cust.CompanyName,
                    cust.ContactName,
                    cust.ContactTitle,
                    cust.Address,
                    cust.City,
                    cust.Region,
                    cust.PostalCode,
                    cust.Country,
                    cust.Phone,
                    cust.Fax,
                    cust.origCustomerID,
                    cust.origCompanyName,
                    cust.origContactName,
                    cust.origContactTitle,
                    cust.origAddress,
                    cust.origCity,
                    cust.origRegion,
                    cust.origPostalCode,
                    cust.origCountry,
                    cust.origPhone,
                    cust.origFax);
            }
    
        Private Sub UpdateCustomer(ByVal cust As Customer)
    
                CustomersTableAdapter.Update(
                cust.CustomerID,
                cust.CompanyName,
                cust.ContactName,
                cust.ContactTitle,
                cust.Address,
                cust.City,
                cust.Region,
                cust.PostalCode,
                cust.Country,
                cust.Phone,
                cust.Fax,
                cust.origCustomerID,
                cust.origCompanyName,
                cust.origContactName,
                cust.origContactTitle,
                cust.origAddress,
                cust.origCity,
                cust.origRegion,
                cust.origPostalCode,
                cust.origCountry,
                cust.origPhone,
                cust.origFax)
        End Sub
    

데이터베이스에서 기존 레코드를 삭제하려면

  • TableAdapter.Delete 메서드를 호출하고 레코드를 찾을 원래 값을 전달하여 해당 레코드를 삭제합니다.

    참고

    개체는 Delete 메서드에 전달할 수 있도록 원래 값을 유지해야 합니다. 이 예제에서는 orig 접두사가 있는 속성을 사용하여 원래 값을 저장합니다.

    다음 예제에서는 Customer 개체의 원래 값을 TableAdapter.Delete 메서드에 전달하여 Customers 테이블에서 레코드를 삭제합니다.

            private void DeleteCustomer(Customer cust)
            {
                customersTableAdapter.Delete(
                    cust.origCustomerID,
                    cust.origCompanyName,
                    cust.origContactName,
                    cust.origContactTitle,
                    cust.origAddress,
                    cust.origCity,
                    cust.origRegion,
                    cust.origPostalCode,
                    cust.origCountry,
                    cust.origPhone,
                    cust.origFax);
            }
    
        Private Sub DeleteCustomer(ByVal cust As Customer)
    
            CustomersTableAdapter.Delete(
                cust.origCustomerID,
                cust.origCompanyName,
                cust.origContactName,
                cust.origContactTitle,
                cust.origAddress,
                cust.origCity,
                cust.origRegion,
                cust.origPostalCode,
                cust.origCountry,
                cust.origPhone,
                cust.origFax)
        End Sub
    

.NET Framework 보안

데이터베이스의 테이블에서 선택한 INSERT, UPDATE 또는 DELETE 작업을 수행할 수 있는 권한이 있어야 합니다.

참고 항목

Visual Studio에서 개체 바인딩
방법: 개체의 데이터에 연결
연습: 개체의 데이터에 연결(Windows Forms)
방법: TableAdapter를 사용하여 데이터베이스에 직접 액세스
Visual Studio에서 데이터에 Windows Forms 컨트롤 바인딩
Visual Studio에서 데이터에 연결
데이터를 받기 위해 응용 프로그램 준비
데이터를 응용 프로그램으로 페치
Visual Studio에서 데이터에 컨트롤 바인딩
응용 프로그램에서 데이터 편집
데이터 유효성 검사
데이터 저장