Tutorial 5 above showed how to use data methods directly on
the SuppliersTableAdapter to manipulate individual rows within our database.
Alternatively, developers can also work to perform these operations using the
SuppliersDataTable and SuppliersDataRow objects. These are particularly useful
when adding/updating/deleting multiple rows at a time (these updates can
optionally be batched to the database in one database call using this
approach).
The below example demonstrates how to retrieve all of the
suppliers in the US with a single database call, then conditionally update some
of the supplier’s zip-codes, and then add a new supplier. We’ll then update
the database with all of the additions/changes:
Dim supplierAdapter As NewNorthwindTableAdapters.SuppliersTableAdapter
Dim suppliers As Northwind.SuppliersDataTable
Dim supplier As Northwind.SuppliersRow
' Obtain all Suppliers in the US
suppliers =supplierAdapter.GetSuppliersByCountry("USA")
' Loop through all suppliers and update any 98042postcodes to 98004
For Each supplier In suppliers
If supplier.PostalCode = "98052" Then
supplier.PostalCode = "98004"
End If
Next
' Create a New Supplier Just for Fun
supplier = suppliers.NewSuppliersRow()
' Set new data properties on supplier2 row
With supplier
.CompanyName = "Microsoft"
.ContactName = "ScottGu"
.Address = "One Microsoft Way"
.ContactTitle = "General Manager"
.City = "Redmond"
.PostalCode = "98052"
.Country = "USA"
.Phone = "425-555-1212"
End With
' Add New Supplier to SuppliersDataTable
suppliers.AddSuppliersRow(supplier)
' Update Database with all changes (updates + additions)
supplierAdapter.Update(suppliers)
Note that there are fancier ways to-do filtering (and
sorting) operations that I’m not using above, but I wanted to keep this
tutorial simple as opposed to overload it with too many concepts.