次の方法で共有


方法 : オブジェクトからデータベースにデータを保存する

更新 : 2007 年 11 月

オブジェクトのデータは、オブジェクトから値を TableAdapter の DBDirect のメソッドの 1 つ (TableAdapter.Insert など) に渡すことによってデータベースに保存できます。詳細については、「TableAdapter の概要」を参照してください。

オブジェクトのコレクションからデータを保存するには、for-next ループなどを使用してオブジェクトのコレクションを反復処理し、TableAdapter の DBDirect のメソッドの 1 つを使用して各オブジェクトの値をデータベースに送ります。

DBDirect のメソッドは既定で TableAdapter に作成され、データベースに対して直接実行できます。この一連のメソッドは直接呼び出すことができ、変更内容を反映してデータベースに送るために、DataSet オブジェクトまたは DataTable オブジェクトを必要としません。

ms233816.alert_note(ja-jp,VS.90).gifメモ :

TableAdapter を構成する際に、メイン クエリは DBDirect のメソッドを作成するために十分な情報を提供する必要があります。たとえば、主キー列が定義されていないテーブルのデータを照会するように TableAdapter が構成されている場合、DBDirect のメソッドは生成されません。

TableAdapter DBDirect のメソッド

説明

TableAdapter.Insert

個々の列値をメソッド パラメータとして渡して、新しいレコードをデータベースに追加します。

TableAdapter.Update

データベースの既存のレコードを更新します。Update メソッドは、元の列値と新しい列値をメソッド パラメータとして受け取ります。元の値は元のレコードを探すために使用し、新しい値はレコードを更新するために使用します。

TableAdapter.Update メソッドも DataSetDataTableDataRow、または DataRow の配列をメソッド パラメータとして受け取って、データセットの変更内容をデータベースに反映して戻すために使用します。

TableAdapter.Delete

メソッド パラメータとして渡された元の列値に基づいてデータベースから既存のレコードを削除します。

オブジェクトからデータベースに新規レコードを保存するには

  • TableAdapter.Insert メソッドに値を渡してレコードを作成します。

    currentCustomer オブジェクトの値を TableAdapter.Insert メソッドに渡して、Customers テーブルに顧客レコードを新規作成する例を次に示します。

    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.Update メソッドを呼び出し、元の値を渡してレコードの場所を探し、新しい値を渡してレコードを更新してレコードを変更します。

    ms233816.alert_note(ja-jp,VS.90).gifメモ :

    オブジェクトは、Update メソッドに渡すために元の値を維持する必要があります。この例では、orig プリフィックスを付けたプロパティを使用して元の値を格納します。

    Customer オブジェクトの新しい値と元の値を TableAdapter.Update メソッドに渡して、Customers テーブルの既存のレコードを更新する例を次に示します。

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

データベースから既存のレコードを削除するには

  • TableAdapter.Delete メソッドを呼び出し、元の値を渡してレコードを探して削除します。

    ms233816.alert_note(ja-jp,VS.90).gifメモ :

    オブジェクトは、Delete メソッドに渡すために元の値を維持する必要があります。この例では、orig プリフィックスを付けたプロパティを使用して元の値を格納します。

    Customer オブジェクトの元の値を TableAdapter.Delete メソッドに渡して、Customers テーブルからレコードを削除する例を次に示します。

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

セキュリティ

データベースのテーブルで INSERT、UPDATE、または DELETE を実行するアクセス許可が必要です。

参照

処理手順

方法 : オブジェクトのデータに接続する

チュートリアル : オブジェクトのデータへの接続

方法 : TableAdapter で直接データベースにアクセスする

概念

Visual Studio におけるオブジェクトのバインド

データの表示の概要

その他の技術情報

Visual Studio でのデータへの接続

アプリケーションでデータを受け取る準備

アプリケーションへのデータのフェッチ

Windows アプリケーションのフォームでのデータの表示

アプリケーションでのデータ編集

データの検証

データの保存