ADONET Objects Part II

Download Report

Transcript ADONET Objects Part II

ADO.NET Objects Data
Adapters
Dr. Ron Eaglin
Agenda
• Builds on Information in Part I
• Should have working knowledge of
creating a database connection
• Continuation of working with Database
Command objects
• Working with Data Adapters
• Working with Data Sets
Data Adapter
Database
Data
Adapter
Dataset
Data Adapter is the glue between the database and a dataset.
Data Adapter
• Data Adapters encapsulate code and
functionality to
– Select
– Update
– Insert
– delete.
Design Time Data Adapter
• If SQLDataAdapter is not in your toolbox,
add it by using “Choose items …”
• Drag the SQLDataAdapter onto your form.
Design Time Data Adapter
Design Time Data Adapter
Design Time Data Adapter
We will have the adapter
Wizard generate stored
Procedures.
Design Time Data Adapter
Design Time Data Adapter
Design Time Data Adapter
Name the stored procedures to fit
The naming convention that you are
Using in your database
You may review the SQL script that
Will be executed to create the stored
Procedures.
Design Time DataAdapter
This SQL Script will be run to create the
Stored procedures used by the data
Adapter.
Design Time Data Adapters
SQL Server
Note the additional stored
Procedures are now added to
The list of stored procedures
In the database.
SQLDataAdapter properties
Note that the Data Adapter also created four command
objects
Create the Dataset
• A Dataset is now necessary to handle the
actual data.
• A Dataset is an in-memory object that
“mirrors” the database.
• A dataset can contain tables, columns,
fields.
Create the Dataset
The Dataset can also be generated by
A wizard. Click on Generate Dataset when
The Data Adapter is selected in the form.
Created Dataset
The Dataset now exists in the project
Add Code to Project
Add button to project
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Me.SqlDataAdapter1.Fill(Me.PersonAddressDataset1)
Dim i, j As Integer
Dim TN, CN As String
For i = 1 To Me.PersonAddressDataset1.Tables.Count
TN = Me.PersonAddressDataset1.Tables(i - 1).TableName
For j = 1 To PersonAddressDataset1.Tables(i - 1).Columns.Count
CN = PersonAddressDataset1.Tables(i - 1).Columns(j - 1).ColumnName
ListBox1.Items.Add(TN + " : " + CN)
Next j
Next i
End Sub
Results of Code
Using Bindable Objects
Add DataGridView
Setting Datagrid Properties
Using the properties window use the drop-down
On the DataSource property
Note the addition of the Binding Source
Binding Source
• In the DataGridView properties select the
DataMember property and select
SelectPersonAddress
Run Application
When the Dataset is filled
The bound grid automatically
Populates.
Updating database
Add button to application
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Me.SqlDataAdapter1.Update(Me.PersonAddressDataset1)
End Sub
Make changes
Test the Update
• If update was successful the record will be
changed.
• Close and re-open project and load
dataset – was change persistent?
Review
•
•
•
•
•
•
Creating a data adapter
Using wizards to configure adapter
Creating a Dataset
Filling set from adapter
Displaying data and table info
Updating from bound object