Professional Documents
Culture Documents
Module 8: Using
Module 8: Using
Module 8: Using
ADO.NET
Overview
ADO.NET Overview
.NET Data Providers
The DataSet Object
Data Designers and Data Binding
XML Integration
ADO.NET Overview
Introduction to ADO.NET
Benefits of ADO.NET
Introduction to ADO.NET
Class Framework
System Services
Benefits of ADO.NET
Similar to ADO
Designed for disconnected data
Intrinsic to the .NET Framework
Supports XML
.NET Data Providers
SqlConnection
Dim
Dim conSQL
conSQL As
As New
New SqlClient.SqlConnection(
SqlClient.SqlConnection( ))
conSQL.ConnectionString
conSQL.ConnectionString == "Integrated
"Integrated Security=True;"
Security=True;" && __
"Data
"Data Source=LocalHost;Initial
Source=LocalHost;Initial Catalog=Pubs;"
Catalog=Pubs;"
conSQL.Open(
conSQL.Open( ))
Using the Command Object
Reading data
Dim
Dim commSQL
commSQL AsAs New
New SqlClient.SqlCommand(
SqlClient.SqlCommand( ))
commSQL.Connection
commSQL.Connection == conSQL
conSQL
commSQL.CommandText
commSQL.CommandText ="Select
="Select au_lname,au_fname
au_lname,au_fname from
from authors"
authors"
Dim datRead As SqlClient.SqlDataReader
Dim datRead As SqlClient.SqlDataReader
datRead
datRead == commSQL.ExecuteReader(
commSQL.ExecuteReader( ))
Do
Do Until
Until datRead.Read
datRead.Read == False
False
MessageBox.Show(datRead.GetString(1)
MessageBox.Show(datRead.GetString(1) && "" "" __
&& datRead.GetString(0))
datRead.GetString(0))
Loop
Loop
datRead.Close(
datRead.Close( ))
Retrieving data
Returning multiple result sets
Using the DataAdapter Object
Dim
Dim adaptSQL
adaptSQL As
As New
New SqlClient.SqlDataAdapter(
SqlClient.SqlDataAdapter( __
"Select
"Select ** from
from authors",
authors", conSQL)
conSQL)
Dim
Dim datPubs
datPubs As
As New
New DataSet(
DataSet( ))
adaptSQL.Fill(datPubs,
adaptSQL.Fill(datPubs, "NewTable")
"NewTable")
'' Manipulate
Manipulate the
the data
data locally
locally
adaptSQL.Update
adaptSQL.Update (datPubs,
(datPubs, "NewTable")
"NewTable")
Demonstration: Retrieving Data Using ADO.NET
The DataSet Object
DAO
Connected environment
RDO
Disconnected environment
ADO
Connected and
disconnected environments
COM Marshalling
ADO.NET
Disconnected architecture
XML format
The DataSet Object
DataSet Object
Collection
Tables Table
Columns Column
Constraints Constraint
Rows Row
Relations Relation
Populating DataSets
Dim
Dim datPubs
datPubs As
As DataSet
DataSet == New
New DataSet(
DataSet( ))
adaptSQL.Fill(datPubs,
adaptSQL.Fill(datPubs, "NewTable")
"NewTable")
Creating relationships
Dim
Dim relPubsTitle
relPubsTitle As
As New
New DataRelation("PubsTitles",
DataRelation("PubsTitles", __
datPubs.Tables("Publishers").Columns("pub_id"),
datPubs.Tables("Publishers").Columns("pub_id"), __
datPubs.Tables("Titles").Columns("pub_id"))
datPubs.Tables("Titles").Columns("pub_id"))
datPubs.Relations.Add(relPubsTitle)
datPubs.Relations.Add(relPubsTitle)
PubRow
PubRow == datPubs.Tables("Publishers").Rows(0)
datPubs.Tables("Publishers").Rows(0)
TitleRows
TitleRows == PubRow.GetChildRows("PubsTitles")
PubRow.GetChildRows("PubsTitles")
Using Constraints
Adding rows
Dim
Dim drNewRow
drNewRow As
As DataRow
DataRow == datPubs.Tables("Titles").NewRow
datPubs.Tables("Titles").NewRow
'Populate
'Populate columns
columns
datPubs.Tables("Titles").Rows.Add(drNewRow)
datPubs.Tables("Titles").Rows.Add(drNewRow)
Editing rows
drChangeRow.BeginEdit(
drChangeRow.BeginEdit( ))
drChangeRow("Title")
drChangeRow("Title") == drChangeRow("Title").ToString
drChangeRow("Title").ToString && "" 1"
1"
drChangeRow.EndEdit(
drChangeRow.EndEdit( ))
Deleting data
datPubs.Tables("Titles").Rows.Remove(drDelRow)
datPubs.Tables("Titles").Rows.Remove(drDelRow)
Updating Data at the Source
Designing DataSets
Data Form Wizard
Data Binding in Windows Forms
Data Binding in Web Forms
Designing DataSets
Information required:
Name of DataSet
Connection to be used
Which tables or views, and which columns within them
How to display the data
Which buttons to create
Demonstration: Using the Data Form Wizard
Data Binding in Windows Forms
Simple binding
da
da == New
New SqlClient.SqlDataAdapter("Select
SqlClient.SqlDataAdapter("Select au_lname,
au_lname, "" && __
"au_fname
"au_fname from
from authors",
authors", sqlconn)
sqlconn)
da.Fill(ds,
da.Fill(ds, "authors")
"authors")
TextBox1.DataBindings.Add("Text",
TextBox1.DataBindings.Add("Text", __
ds.Tables("authors"),
ds.Tables("authors"), "au_fname")
"au_fname")
Complex binding
da
da == New
New SqlClient.SqlDataAdapter("Select
SqlClient.SqlDataAdapter("Select au_lname,
au_lname, "" && __
"au_fname
"au_fname from
from authors",
authors", sqlconn)
sqlconn)
da.Fill(ds,
da.Fill(ds, "authors")
"authors")
DataGrid1.DataSource
DataGrid1.DataSource == ds.Tables("authors")
ds.Tables("authors")
Data Binding in Web Forms
Use impersonation
<identity
<identity impersonate="true"
impersonate="true" userName="Karen"
userName="Karen"
password="Password"/>
password="Password"/>
sync
XmlDataDocument DataSet
ADO.NET Overview
.NET Data Providers
The DataSet Object
Data Designers and Data Binding
XML Integration