|
As its name indicates, a table adapter is an object that
gives access to a database's table. To
create a table adapter, you can use the Data Source Configuration Wizard.
|
A Class for a Table Adapter |
|
We already know how to visually create a data set, by adding a new data
source from either the main menu under Data or the Data Sources window. We have
seen various ways of doing this using the Data Source Configuration Wizard.
When you wizard ends, a class is generated.
Remember that in the last page of the wizard, you select one
or more tables you would use from the database. When the wizard generates the
class, it creates a class for each table.
|
|
The class is based on the
TypedTableBase class from the System.Data namespace. Here is an example
of how such a class starts:
Partial Public Class EmployeesDataTable
Inherits Global.System.Data.TypedTableBase(Of EmployeesRow)
Inside the class for the table, a DataColumn
variable is declared for each column of the table. Inside the class for the new
data set, the wizard declares a variable for each table class.
Besides classes for the
table, the wizard also generates classes referred to as table adapters. A table
adapter class is generated for each table. The class is derived from Component.
Here is an example:
Partial Public Class EmployeesTableAdapter
Inherits Global.System.ComponentModel.Component
The class includes various properties and methods that can
be used for database management and maintenance.
Getting the Records of a Table Adapter |
|
|
When creating a table adapter, if its corresponding table
has records, it gets equipped with them and you can use them as you see fit. To
pass its records to a table, the generated table adapter is equipped with a
method named Fill. This method takes as argument the table class that was
generated, it fills it with the records, and returns it.
To assist you with getting the records, the table adapter
class is equipped with a method named GetData. This method
takes no argument and returns an object of the table class type that was
generated. Here is an example of calling this method:
|
Private Sub btnLoadCustomers_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLoadCustomers.Click
dgvCustomers.DataSource = TaCustomers.GetData()
End Sub
Remember that the GetData() method returns a table
adapter class that inherits from the DataTable. Thanks to this,
GetData() gives you access to regular operations performed on a
table.
Operations on a Table Adapter |
|
|
Among the characteristics that a table adapter provides is a
connection object. To get the connection that it is using, the generated class
is equipped with a property named Connection and that is of type
SqlConnection:
Private Sub btnConnection_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnConnection.Click
MsgBox(TaCustomers.Connection.ToString())
End Sub
|
You can use the connection object to get such information as
the name of the server or the connection string:
Private Sub btnConnection_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnConnection.Click
MsgBox(TaCustomers.Connection.ConnectionString)
End Sub
The generated table adapter class is equipped with a
property named Adapter. This property is of type SqlDataAdapter. It gives you
access to some operations that can be performed on a data adapter. Once a table contains records, you may be interested in
finding a value. To do this, you can use the Transact-SQL through the Adapter
property of the table class that was genereated. Remember that the Adapter
property is of type SqlDataAdapeter. This means that it is equipped with the
SelectCommand property that itself is of type
SqlCommand. The SqlCommand class is equipped with the
CommandText property. This is the property you would assign the Transact-SQL
statement to.
Locating a Record Based on a Column |
|
We mentioned that the table class generated by the wizard provides the normal characteristics of a DataTable.
This means that it gives you access to the records of the table. This is
possible because the class inherits the Rows property from its
ancestor. As you may recall, the DataTable.Rows property is of
type DataRowCollection.
To locate a record, you can use a column name. To support
this, the data row is equipped with an Item indexed property that takes a column
name as parameter.
Locating a Record Using the DataRowCollection's Find |
|
The table adapter provides many flexible methods and
properties that allow you to get various pieces of information about its table.
For example, you can use it to locate a record using various techniques such as
the index of a record followed by the name of a column. Here is an example that
gets to the second record and its column named FullName:
|
|
Private Sub btnConnection_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnConnection.Click
MsgBox(CStr(TaCustomers.GetData().Rows(2)("FullName")))
End Sub
The DataRowCollection class is equipped
with a method named Find. You can call this method through the
table adapter variable to locate a record. Here is an example:
Private Sub btnConnection_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnConnection.Click
MsgBox(CStr(TaCustomers.GetData().Rows.Find("608208")("AccountNumber")))
End Sub
One of the most fundamental operations you can perform on a
table adapter consists of creating a new record. To assist you with this, the table
adapter class that the wizard generates is equipped with a method named Insert.
This method takes one or more arguments depending on its table. This means that
it takes an argument for each column of its table. To create a record for the
table, call this method and pass the value of the columns observing the
following rules:
- You must pass a value for each column
- You can specify a value only for a column that exists on the table
- You must pass the values in the order they appear in the table
- If you don't have a value for a column, pass it as null
- You must pass a value in the appropriate type: int,
string, decimal, etc. Because this is a C#
class, the values must be passed in C# types
- Don't assign a value to a column whose records must be automatically
specified. This is the case for a primary key column with an identity
property
- Don't assign a value to a column whose records are specified by an
expression
- Observe all check constraints
- If a column has a UNIQUE characteristic, you must not
give it a value that exists already in the table
|
|
Here is an example:
Private Sub btnAddNewRecord_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnAddNewRecord.Click
TaCustomers.Insert("204680", "Arsene Forland", _
"(022) 197-0095", "Clint", _
"(048) 927-1153")
End Sub
Updating or Editing a Record |
|
Editing a record consists of changing one or more of its
values. To programmatically do this, you must first locate and open the record.
Then change the necessary value(s). After doing this, if you want to apply the
change to the table, you must update it. To assist you with this, the generated
table adapter is equipped with the Update() method. This method
is overloaded with four versions: one for a data set, one for a data table,
one for a record (a data row), and one for an array of records (a
DataRow() array). Therefore, after making the changes on either a
record, some records, or a table, call the appropriate version of the method to
apply the changes. Here is an example:
Private Sub btnUpdate_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnUpdate.Click
Dim Customer As DataRow = TaCustomers.GetData().Rows.Find("204680")
Customer("EmergencyName") = "Alexander"
Customer("EmergencyPhone") = "Balm"
TaCustomers.Update(Customer)
End Sub
As you should know already, before using a data set, you should
create an object of type DataSet. You can create it manually or visually.
To manually create a data set, you have two alternatives.
You can click the DataSet object from the Data section of the Toolbox and click
the form. You would specify it as an Untyped Dataset and click OK. An alternative
is to manually type the code used to create the data set, which is done by
declaring a variable of type DataSet. After manually creating
the data set, you must fill it with records to make it useful.
To visually create a data set, you can add a
new data source from either the main menu under Data or the Data Sources window.
Then use the Data Source Configuration Wizard. When the wizard ends, a class is
generated. The class is derived from the
DataSet class and holds the name you had specified for the data
set in the last page of the wizard. The class starts as follows:
Partial Public Class DsFunDS
Inherits Global.System.Data.DataSet
After
creating the data set, you can use it, along with its table(s) (DataTable
objects), its (their) columns (DataColumn objects), and its (their)
records (DataRow objects and DataRowCollection
lists).
|
|
Because a data set is tied to the database, it
provides all of its services. This means that a data set can be used for any
necessary maintenance assignment. There are many ways you can perform maintenance
on a data set, a table, a column, or a record. Each one of these items is
represented by one or more classes and those classes support various types of
maintenance operations.
Besides the means provided by the data set, the tables,
their columns, and their records, the table adapter that was generated by the
wizard is equipped with various methods.
Saving the Records of a Data Set |
|
Although the records of a database belong to a table, if you want to use them in an external application, you can save
them in an XML file. To support this, the DataSet class is
equipped with the WriteXml() method. Here is an example of
calling it:
Private Sub BtnSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnSave.Click
Using ScnDepartmentStore As SqlConnection = _
New SqlConnection("Data Source=(local);" & _
"Database='FunDS1';" & _
"Integrated Security=yes;")
Dim cmdStoreItems As SqlCommand = _
New SqlCommand("SELECT * FROM StoreItems; ", _
ScnDepartmentStore)
Dim sdaStoreItems As SqlDataAdapter = New SqlDataAdapter
Dim dsStoreItems As DataSet = New DataSet("StoreItems")
ScnDepartmentStore.Open()
sdaStoreItems.SelectCommand = cmdStoreItems
sdaStoreItems.Fill(dsStoreItems)
dsStoreItems.WriteXml("C:\Exercise\StoreItems.xml")
End Using
End Sub