语言

将数据从对象保存到 .NET Framework 应用程序中的数据库

注释

此类 DataSet 和相关类是 2000 年代初的旧版 .NET Framework 技术,使应用程序能够在应用与数据库断开连接时处理内存中的数据。 这些技术对于使用户能够修改数据并将更改保留回数据库的应用特别有用。 尽管数据集是经过证实的成功技术,但新 .NET 应用程序的建议方法是使用 Entity Framework Core。 Entity Framework 提供了一种更自然的方式来将表格数据用作对象模型,并且具有更简单的编程接口。

通过将对象中的值传递给 TableAdapter 的 DBDirect 方法之一(例如, TableAdapter.Insert),可以将对象中的数据保存到数据库中。 有关详细信息,请参阅 TableAdapter。

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

默认情况下, DBDirect 在可直接针对数据库运行的 TableAdapter 上创建方法。 可以直接调用这些方法,不需要 DataSet 或 DataTable 对象来协调更改,以便将更新发送到数据库。

注释

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

TableAdapter DBDirect 方法 DESCRIPTION
TableAdapter.Insert 将新记录添加到数据库,并使你能够以方法参数的形式传入各个列值。
TableAdapter.Update 更新数据库中的现有记录。 该方法 Update 采用原始列值和新列值作为方法参数。 原始值用于查找原始记录,新值用于更新该记录。

该TableAdapter.Update方法还用于通过将DataSet、DataTable、DataRow或DataRow数组作为方法参数来将数据集中的更改协调回数据库。
TableAdapter.Delete 根据作为方法参数传入的原始列值从数据库中删除现有记录。

将新记录从对象保存到数据库

  • 通过将值 TableAdapter.Insert 传递给方法来创建记录。

    以下示例通过将对象TableAdapter.Insert中的CustomerscurrentCustomer值传递给方法,在表中创建新的客户记录。

    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 的属性来存储原始值。

    以下示例通过将对象中的CustomersCustomer新值和原始值传递给TableAdapter.Update方法来更新表中的现有记录。

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

.NET 安全性

必须获得权限才能在数据库中对表执行选定的INSERT、UPDATE或DELETE。