Partilhar via


Como: Salvar dados de um objeto a um banco de dados

You can save data in objects to a database by passing the values from your object to one of the TableAdapter's DBDirect methods (for example, TableAdapter.Insert).Para obter mais informações, consulte Visão geral do TableAdapter.

To save data from a collection of objects, loop through the collection of objects (for example, a for-next loop) and send the values for each object to the database using one of the TableAdapter's DBDirect methods.

By default, DBDirect methods are created on a TableAdapter that can be executed directly against the database.These methods can be called directly and do not require DataSet or DataTable objects to reconcile changes in order to send updates to a database.

Observação:

Quando você estiver configurando um TableAdapter, a consulta principal deve fornecer informações suficientes para que o DBDirect métodos a serem criados.Por exemplo, se um TableAdapter está configurado para consultar dados de uma tabela que não tenha uma coluna de chave primária definida, ele não gerará DBDirect métodos.

Método DBDirect do TableAdapter

Descrição

TableAdapter.Insert

Adds new records into a database allowing you to pass in individual column values as method parameters.

TableAdapter.Update

Atualizações de registros existentes em um banco de dados.The Update method takes original and new column values as method parameters.Os valores originais são usados para localizar o registro original, e os novos valores são usados para atualizar esse registro.

The TableAdapter.Update method is also used to reconcile changes in a dataset back to the database by taking a DataSet, DataTable, DataRow, or array of DataRows as method parameters.

TableAdapter.Delete

Exclui registros existentes do banco de dados com base nos valores originais da coluna que foram passados como parâmetros do método.

To save new records from an object to a database

  • Create the records by passing the values to the TableAdapter.Insert method.

    The following example creates a new customer record in the Customers table by passing the values in the currentCustomer object to the TableAdapter.Insert method.

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

To update existing records from an object to a database

  • Modify the records by calling the TableAdapter.Update method and passing in the new values to update the record and passing in the original values to locate the record.

    Observação:

    O objeto precisa manter os valores originais para passá-las para o Update método. This example uses properties with an orig prefix to store the original values.

    The following example updates an existing record in the Customers table by passing the new and original values in the Customer object to the TableAdapter.Update method.

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

To delete existing records from a database

  • Delete the records by calling the TableAdapter.Delete method and passing in the original values to locate the record.

    Observação:

    O objeto precisa manter os valores originais para passá-las para o Delete método. This example uses properties with an orig prefix to store the original values.

    The following example deletes a record from the Customers table by passing the original values in the Customer object to the TableAdapter.Delete method.

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

Segurança

You must have permission to perform the selected INSERT, UPDATE, or DELETE on the table in the database.

Consulte também

Tarefas

Como: Conectar-se a dados in an objeto

Demonstra Passo a passo: Conectando-se a dados in Objects

Como: Acessar diretamente o banco de dados com um TableAdapter

Conceitos

Vinculação de objeto no Visual Studio

Exibindo visão geral de dados

Outros recursos

Conectando-se a Dados no Visual Studio

Preparando seu aplicativo para receber dados

Buscando dados em seu aplicativo

Exibindo dados em formulários em aplicativos do Windows

Editar dados no seu aplicativo

Validando Dados

Salvando dados