You are on page 1of 28

ADO.

NET

UNIT-4
INTRODUCTION
• It is a model used by .NET applications to communicate with
a database for retrieving, accessing and updating data.
• It supports applications that need to communicate with a
database for retrieving data and presenting it in a
user-friendly format.
• All the ADO.NET classes are located
into System.Data.dll and integrated with XML classes
located into System.Xml.dll.
• ADO.NET has two main components that are used for
accessing and manipulating data are the .NET Framework
data provider and the DataSet.
Features
• Disconnected Data Architecture
• Data cached in datasets
• Data Transfer in XML Format
• Interaction with database is done through
data commands.
ADO.Net Object Model
DATA PROVIDER

Accessing Establishes connection


Connection
Retrieved Data
Visual Basic .Net
Retrieves data in
Application read-only, forward only
mode
Data Reader

Executes command to
Command retrieve data
Accessing DATABASE
Retrieved Data
Transfers data to the
dataset and reflects
changes in the
database

Dataset Data Adapter


Filling
Dataset with
Data
Data Provider
• Used for connecting to a database, retrieving
data, reading the retrieved data and updating
the database.
• Two types of data providers
- OLE DB Data Provider
- SQL Server Data Provider
Connection
• Used to establish connection with a data
source.
• OleDbConnection and SqlConnection are two
common Connection Types
• Connection string and State are its two
properties
• Open() and Close() are its two methods.
Command
Different forms of command Execution

• ExecuteReader: Returns data to the client as rows. This would typically


be an SQL select statement or a Stored Procedure that contains one or
more select statements. This method returns a DataReader object that
can be used to fill a DataTable object or used directly for printing
reports and so forth.

• ExecuteNonQuery: Executes a command that changes the data in the


database, such as an update, delete, or insert statement, or a Stored
Procedure that contains one or more of these statements. This method
returns an integer that is the number of rows affected by the query.

• ExecuteScalar: This method only returns a single value. This kind of


query returns a count of rows or a calculated value.

• ExecuteXMLReader: Returns an XML Reader object.


Data Reader Properties
Data Reader Methods
Data Adapter
Dataset
The DataSet is the heart of ADO.NET. The DataSet is
essentially a collection of DataTable objects. In turn
each object contains a collection of DataColumn and
DataRow objects. The DataSet also contains a
Relations collection that can be used to define
relations among Data Table Objects.
Data set Properties
Dataset Methods
Data Binding
• After the data is retrieved from the
database, it has to be bound to a control on
a Windows Form to be displayed in a
customized format. This is called Data
Binding
• It is of Two types
– Simple Data Binding
– Complex Data Binding
Simple Data Binding
• It is the process of binding a control, such as
textbox, label to a value in a dataset.
– Text property under Data Bindings is set to the
column name of the database to be displayed
Complex Data Binding
• It is the process of binding a component such
as a Data Grid or ListBox to display multiple
values for a column from the data set rows.
• We need to set two properties
– Data Source – set the dataset
– Data Member- set specific table
Navigation Among Records
WINDOWS FORM
CONTROL1
CONTROL2
CONTROL3

DATASOURC
CURRENCYMANAGER1 E1

BINDINGCONTEXT CURRENCYMANAGER2 DATASOURC


OBJECT E2

CURRENCYMANAGER3 DATASOURC
E3
CODE TO NAVIGATE
Private Sub Next_Click(sender As Object, e As EventArgs) Handles Button2.Click
bm.Position += 1
TextBox1.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(0)
TextBox2.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(1)
End Sub
Private Sub Previous_Click(sender As Object, e As EventArgs) Handles Button3.Click
bm.Position = bm.position -1
TextBox1.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(0)
TextBox2.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(1)
End Sub
Private Sub First_Click(sender As Object, e As EventArgs) Handles Button1.Click
bm.Position = 0
TextBox1.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(0)
TextBox2.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(1)
End Sub

Private Sub Last_Click(sender As Object, e As EventArgs) Handles Button4.Click


bm.Position = bm.Count - 1
TextBox1.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(0)
TextBox2.Text = EMP1DataSet1.Tables("emp").Rows(bm.Position).Item(1)
End Sub
Navigating between Records
• CurrencyManager handles the binding to the
data source by keeping a pointer to the
current item in the record list.
• The CurrencyManager class derived from
BindingManagerBase Class.
• BindingContext object is used to keep track of
existing CurrencyManager objects in a form.
FILTERING & SORTING DATA
• Data is filtered so as to display only the
desired records.
• There are two methods for displaying filtered
data
– Creating Parameterized Queries
– Filtering a dataset
Parameterized Queries
• Data can be filtered based on the criterion
entered by the user at run time.
• They are created while configuring data
adapters.
– Select id, name, state from authordetails where
state=‘delhi’;
Filtering a Dataset
• There are two options for filtering data in a
dataset
– Using Select() method
– Filtering a data view

• Select() method of DataTable class is used to


filter data. With this method order of the
records in the table does not change only the
data is presented in a filtered format.
Program
• Private sub FrmSelect_Load()
Dim dt as DataTable
OleDbDAProd.Fill(DSProd)
dt=DSProd.Tables(“Product”)
dim bcost as string
dim sort as string
bcost=“Base_Cost>2500”
sort=“PRODID DESC”
Dim result() as DataRow
result=dt.Select(bcost,sort)
Dim ctr as integer
for ctr=0 to (result.Length-1)
lbprodid.items.add(result(ctr)(“ProdId”).ToString)
Next ctr
End Sub
Data View
• A Data view object creates a fixed customized
view of a given DataTable object.
• It can be considered as a mask over a
DataTable object that allows to filter, sort and
navigate the contents of a table
• Every table has a DataView object attached to
it.
Data View
FILTER2
DATAVIEW1

WINDOWS
APPLICATION DATAVIEW2 DATA TABLE

DATAVIEW3

FILTER1
Properties of Data View
• Table- to specify the data table to which data view
would refer
• Sort- to specify expression on which records will
be sorted
• RowFilter- to specify expression to filter the
records
• RowStateFilter- to specify the version of the
record that is current or original
• AllowNew, AllowDelete, AllowEdit – whether the
records can be added, deleted or edited.
Program
• Private sub FrmSelect_Load()
OleDbDADataview.Fill(DSDataview)
Dim dv as DataView= new
DataView(DSDataview.Tables(“Product”))
dv.rowfilter=“Base_cost>2500”
dv.sort=“ProdId DESC”
lbprodid.Datasource=dv
lbprodid.DisplayMember=“ProdId”
End Sub

You might also like