Share via


如何:将数据从对象保存到数据库

更新:2007 年 11 月

通过将对象中的值传递到 TableAdapter 的 DBDirect 方法之一(例如,TableAdapter.Insert),可将对象中的数据保存到数据库中。有关更多信息,请参见 TableAdapter 概述

若要保存对象集合中的数据,请循环通过对象集合(例如,for-next 循环),然后使用 TableAdapter 的 DBDirect 方法之一将每个对象的值发送到数据库中。

默认情况下,DBDirect 方法在 TableAdapter 上创建,能够直接对数据库执行操作。这些方法可以直接调用,它们不要求 DataSetDataTable 对象来协调更改即可将更新发送到数据库。

说明:

配置 TableAdapter 时,主查询必须提供足够的信息,才能创建 DBDirect 方法。例如,如果将 TableAdapter 配置为从未定义主键列的表中查询数据,它将不会生成 DBDirect 方法。

TableAdapter DBDirect 方法

说明

TableAdapter.Insert

向数据库中添加新记录,此方法允许将值作为方法参数传入各个列。

TableAdapter.Update

更新数据库中的现有记录。Update 方法将原始列值和新列值用作方法参数。原始值用于定位原始记录,新值用于更新该记录。

通过将 DataSetDataTableDataRow、或 DataRow 数组用作方法参数,TableAdapter.Update 方法还可用于将数据集中的更改协调回数据库。

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 方法并传入新值可更新记录,而传入原始值可定位记录。

    说明:

    对象需要保留其原始值,才能将它们传递给 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 方法并传入原始值定位记录,可删除记录。

    说明:

    对象需要保留其原始值,才能将它们传递给 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 应用程序中的窗体上显示数据

在应用程序中编辑数据

验证数据

保存数据