Sunteți pe pagina 1din 38

Introduction to ADO.Net, VB.

Database Tools and Data Binding
ISYS 512

Database Processing
Querying database
Updating database:
Insertion, deletion, modification

Steps to Retrieve Data

Establishes a connection to the database.
Executes commands against the database.
SQL Select commands

Store data results.

A Simplified View of ADO.Net

Data Provider


SQL Updates

Data Consumer



Results of SQL Selects

ADO.NET Objects
Connection Object: Represent a connection to the
Command Object: The command object allows us
to execute a SQL statement or a stored procedure.
DataReader: It is a read-only and forward-only
pointer into a table to retrieve records.
DataSet Object: A DataSet object can hold several
tables and relationships between tables.
DataAdapter: This the object used to pass data
between the database and the dataset.

Data Providers
ODBC Provider
Open Database Connectivity
A driver manager
Used for relational databases

OLE DB Provider
OLE DB interfaces provide applications with uniform access to
data stored in diverse information sources, or data stores.

SQL Server Provider

Oracle Provider
Microsofts .NET data provider for Oracle


Using ODBC
Windows XP:
Control Panel /Administrative Tools/DataSource(ODBC)

Three types of data source names

User DSN: usable only by you and only on the machine
currently using.
System DSN: Any one using the machine can use.
File DSN: Can be copied and used by other computers
with the same driver installed.

Excel: Data/Get External Data/From other sources
Access: File/Get External Data/Import, then select
ODBC data source

Demo: SAS Reading ODBC Data

proc sql;
connect to odbc (dsn=MySalesDB2007);
Create table sasuser.TEST
As select * from connection to ODBC
(select * from customer);
disconnect from odbc;
proc sql;
select * from sasuser.test;

VB.NET Database Tools

Add a new connection to database:
Database connection:
Tools/Connect to database
Add Connection

Server Explorer
Data connections: Right click data connection
Add Connection

Tables, Views

Create new SQL Server Database

Add a new data source:

Data/Add New Data Source

Types of Data Source

Working with SQL Server

Starting SQL Server:
Control Panel/Administrative

SQL Server database file extension: mdf

Default database folder:
A SQL Server file: testSQL08.mdf
C:\Program Files\Microsoft SQL

Creating SQL Server Database

From Server Explorer, right click data connection
and choose:
Create new SQL Server Database

Server name:

Add new table: Right click Tables and choose Add

New Table
Define tables fields

Add rows: Right click the table name and choose

Show table data.
Note: After creating the database, you may create
an ODBC DSN to connect to it.

Create a New SQL Server

Database: EmployeeDB
Add a new table: EmployeeTable


Enter data

Char 10
Char 30
Char 1
Numeric (10,2)

How to create ADO.Net objects?

Automatically generated when creating
data bound form.
Form wizard

Using Data Adapter Wizard

Using code:
dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source
= c:\sales2k.mdb"
dim objConn as new OledbConnection(strConn)

Data Binding
Connect a control or property to one or more data
Simple binding: Use simple binding to display a
field value in controls that show Data Bindings in
the property window, such as text box or label.
Complex binding: Use complex binding to bind
more than one field to controls such as DataGrid
and list box. Use the controls Data Source and
Data Member to bind the data.

Creating Data Bound Form

Creating a form with ADO.Net objects and databound controls to display and update information in
a dataset.
Add data source:
Data/Add New Data Source
Data/Show Data Source

Click the dropdown list next to the tables name:

Datagrid view

Drag the table to form.

Items Added to the Form

Table Adapter: click smart tag
Add query
Preview data

Edit in dataset designer

Binding Source
Add query: Add a new tool strip.
Preview data

Binding navigator
Code view: Form load event

Generated Code
Private Sub CUSTOMERBindingNavigatorSaveItem_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles

End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the
'SalesDBDataSet.CUSTOMER' table. You can move, or remove it, as
End Sub

Other Data Form Demos

DataGrid View
Add /Modify/Delete records.
Read only form:
Delete AddNew, Delete, Save buttons from
navigator bar.

Hierarchical Forms
Parent table/Child table
Add parent table and child table to Data Source
Drag the parent table and the child table to the form.
Parent table uses detail view and child table uses
dataGrid view
Click Dataset objects smart tag to choose Edit in
Dataset Designer
With the designer, right click the parent table and
choose Add/Relation
Change dataGrids DataSource property to the relation.

Detail Form with Bound ListBox

Example: Customer table form with CID listbox
and displays selected customer information in
Choose detail view for the customer table.
Click the dropdown list next to the CID field and click
Drag the Customer table to the form.
Bind the CID field to the BindingSource:
Activate the Property window and click the listbox
Set the DataSOurce property to BindingSource
Set the Display Member property to CID

Creating A Database Application

Without Programming
Creating a database application to display
information and update database.
A main form with buttons to open data

Enter New

Data Adapter Wizard 2nd Level

of Using ADO.Net
Configure Data Adapter and generating a dataset:
From the Data tab of the ToolBox, Drag
OledbDataAdapter to the form.
Use the Data Adapter Wizard to configure the Adapter.
Right Click the Adapter to preview data and create

Note: If OledbDataAdapter is not listed in

Toolboxs Data tab , right click ToolBox Data
Tab and select Choose Item; then select
OleDbDataAdapter from .Net Framework components.

Creating a Form with Bound DataGridView

Configure Adapter and generate dataset.
Bind DataGridView control to the table:
Data Source property:

Data Member property

A table in the dataset

In the Form Load event, use Adapters Fill

method to load the dataset:

Note 1: No navigation capability

Note 2: View adapters properties and see
the SQL commands.

Creating a Form with Bound

Configure Adapter and generate dataset.
Select textboxs Data Bindings property:
Text: choose field

In the Form Load event, use Adapters Fill

method to load the dataset:

Note: No navigation capability

Using BindingContext to Navigate

BindingContext: It is an object that manages a
collection of data sources used for binding.
CurrencyManager: It is an object that keeps track
of position (the current row) of a data source. Two
useful properties:
Position property: is the index of the current row. The
index is a 0-based index, the first record has a position
of 0.
Count property: The number of rows in the data source.

MoveNext and MoveLast Example


Me.BindingContext(DataSet21, "customer").Position += 1

Me.BindingContext(DataSet21, "customer").Position =
Me.BindingContext(DataSet21, "customer").Count -1

How to MovePrevious and MoveFirst?

Note: The Position property takes care of the end of file
Note: Me.BindingContext(DataSet21, "customer") returns
a CurrencyManager object.

Using BindingSource Object to Do

the Binding and Navigation
It is an object that keeps track of position (the current row)
of a data source.
Useful properties:
Position property: is the index of the current row. The index is a 0based index, the first record has a position of 0.


MoveFirst, MoveLast, MoveNext, MovePrevious


Bind Controls to BindingSource

Configure Adapter and generate dataset.
Add BindingSource object to the form
Use BindingSource objects property window to
set its DataSource and Data Member properties to
the dataset object and table object.
Set controls DataBinding property to the
BindingSource object.

Use BindingSource Objects

MoveNext, MovePrevious Methods
Add a MoveNext button

Add a MovePrevious button:


Adding AddNew and Save Button

AddNew button: Use BindingSource AddNew Method:
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
End Sub

Save button: Use BindingSource EndEdit method and

Adapters Update method:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

End Sub

Binding ListBox
Example: Bind Customer Tables CID field
to a listbox.
Create a Adapter for Customer table , and
generate the dataset.
Add ListBox and set binding properties:
Data Source: Customer table
Display Member: Field to display in the listbox.
Value Member: the actual values for items in the list

Display Selected Record

Bound textbox (same data source as the listbox):
If the Listbox and the textbox are bound to the same
BindingSource object, the textbox will automatically
displays the record of the selected listbox item.

Unbound textbox
To display the ValueMember
Textbox1.text = ListBox1.SelectedValue

To display other fields:

Textbox1.text = ListBox1.SelectedItem(Cname)
Can we use TextBox1.text=ListBox1.SelectedItem?

ListBox SelectedItem Property

How to display the selected record in unbound
After binding to a data source, this property return a
DataRowView object.
What is DataRowView?
Object Browser:

Item property is the default property

To retrieve a column from a DataRowView object

(use 0-based index to identity a column):
Or: ListBox1.SelectedItem(1)
Or: ListBox1.SelectedItem(Cname)

Using Object Browser

View/Object Browser
DataSet object model:

Use Object Browser to study objects properties,

methods. Ex. System.Data.OleDbDataAdapter

Working with SQL Server Data

From the Data tab:
Choose: SQLDataAdapter
(If you dont see it, right-click the Data tab and
select Choose Item. Then select it from the
.Net components)

DataSet and Data Adapter

DataSet Object: A DataSet object can hold several
tables and relationships between tables.
DataAdapter: This the object used to pass data
between the database and the dataset.
It is an object with SQL Select, Insert, Update and
Delete commands.

Creating Parent/Child Form Using

DataAdapter Wizard
Add two DataAdapters to the form: one for the parent
table and one for the child table.
Use the parent tables DataAdapter to generate a
dataset; then use the child tables DataAdapter to
generate the child table into the same dataset.
Open datasets designer and right-click the parent table
and choose Add/Relation to add a relation.
Add and bind textboxes to the parent table; add
dataGridView and bind its datasource property to the
Add navigation buttons using BindingSource object.