次の方法で共有


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

 

公開日: 2016年4月

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

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

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

注意

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

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

 TableAdapter.Update メソッドも DataSetDataTableDataRow、または 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 フォーム)
方法 : TableAdapter で直接データベースにアクセスする
Visual Studio でのデータへの Windows フォーム コントロールのバインド
Visual Studio でのデータへの接続
アプリケーションでデータを受け取る準備
アプリケーションへのデータのフェッチ
Visual Studio でのデータへのコントロールのバインド
アプリケーションでのデータ編集
データの検証
データの保存