0% found this document useful (0 votes)
131 views

Accessing Data in ADO. Net With ASP. Net

ADO.NET provides a standard way to access and manipulate data from various data sources like SQL Server, Oracle, and OLE DB. It uses two main components - the DataSet, which is an in-memory representation of data, and the Data Provider, which maintains the connection to the database. Common ADO.NET classes include Connection, Command, DataReader, and DataAdapter. These classes can be used to perform basic data operations like selecting, inserting, updating and deleting records from a database.

Uploaded by

thuva
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Accessing Data in ADO. Net With ASP. Net

ADO.NET provides a standard way to access and manipulate data from various data sources like SQL Server, Oracle, and OLE DB. It uses two main components - the DataSet, which is an in-memory representation of data, and the Data Provider, which maintains the connection to the database. Common ADO.NET classes include Connection, Command, DataReader, and DataAdapter. These classes can be used to perform basic data operations like selecting, inserting, updating and deleting records from a database.

Uploaded by

thuva
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 80

ADO.

NET

Accessing Data in ADO. Net with ASP. Net


Understanding ADO. Net
Visual Studio .NET provides access to databases through
the set of tools and namespaces collectively referred to as
Microsoft ADO.NET. Data access in ADO.NET is
standardized to be mostly independent of the source of the
data—once you’ve established a connection to a database,
you use a consistent set of objects, properties, and
methods, regardless of the type of database you are using.
ADO.NET Data Architecture

Data Access in ADO.NET relies on two components

• Data Set
• Data Provider
Data Set

The dataset is a disconnected, in-memory


representation of data. It can be considered as a
local copy of the relevant portions of the database.
The DataSet is persisted in memory and the data
in it can be manipulated and updated independent
of the database.
Data Provider

The Data Provider is responsible for providing and


maintaining the connection to the database. A
DataProvider is a set of related components that
work together to provide data in an efficient and
performance driven manner.
Component Classes
The Connection object which provides a connection to the
database

The Command object which is used to execute a command

The DataReader object which provides a forward-only,


read only, connected recordset

The DataAdapter object which populates a disconnected


DataSet with data and performs update
Connection and Data Adapter
Object
ADO. Net Namespaces

Namespace Provides
System.Data Classes, types, and services for creating
and accessing data sets and their
subordinate objects
System.Data.SqlClient Classes and types for accessing
Microsoft SQL Server databases
System.Data.Oracle.Client Classes and types for accessing Oracle
databases (Microsoft .NET Framework
version 1.1 and later)

System.Data.OleDb Classes and types for accessing other


databases
Visual Basic .NET Code

• Imports System.Data
• ' For Microsoft SQL Server database connections.
• Imports System.Data.SqlClient
• ' For Oracle database connections.
• Imports System.Data.OracleClient
• ' For other database connections.
• Imports System.Data.OleDb
Accessing Data with ADO.Net
objects
• Create a connection to the database using a connection
object.
• Invoke a command to create a DataSet object using an
adapter object.
• Use the DataSet object in code to display data or to
change items in the database.
• Invoke a command to update the database from the
DataSet object using an adapter object.
• Close the database connection if you explicitly opened it
in step 2 using the Open method. Invoking commands
without first invoking the Open method implicitly opens
and closes the connection with each request.
Using Server Explorer
• To connect to a database in the Visual Studio
design environment, follow these steps:
• From the View menu, choose Server Explorer.
Visual Studio displays the Server Explorer
window.
• In the Server Explorer, click Connect To Database
on the toolbar. Visual Studio .NET displays the
DataLink Properties dialog box
Data Link Dialog Box
Connection Tab
Server Explorer
Clicking the plus signs
in the Server Explorer
expands items. To
view the tables in a
data connection,
expand the items
under the data
connection, and then
expand the items
under Tables.
Expanded Items

To add a data item to


your application, drag
the item from the
Server Explorer onto
your Web form.
Connection and Adapter
Objects
Creating a DataSet
• To create a data set in Design mode, follow
these steps:
• Right-click the data adapter object, and select
Generate Dataset from the shortcut menu.
Visual Studio displays the Generate Dataset
dialog box
• Select the Products table to add to the data set,
and click OK. Visual Studio creates a new
DataSet object and adds it to the Web form.
Data Set
To view the data in the
data set in Design
mode, right-click the
DataSet object, and
then select View
Schema from the
shortcut menu. Visual
Studio displays the
data set in the XML
Designer window
Dataset Schema
Displaying a Dataset
• To display the data set on the Web form at run
time, follow these steps:
• Add a control to the Web form to display the data.
For instance, add a DataGrid control to the Web
form.
• Set the data set as the data source for the control.
For instance, for the DataGrid control, click the
Property Builder link in the Properties window,
set the DataSource property to the DataSet object,
and set the DataMember property to a table in the
data set .
Data Grid Properties
Data Grid Properties
contd..

Set the columns to display


in the control. For the
DataGrid control, click the
Columns item in the
Properties dialog box,
clear the Create Columns
Automatically At Run Time
check box, and then add
the columns to display
from the Available
Columns list
Code
Add code to the Web form’s Page_Load event procedure to
fill the data set from the data adapter and to bind the data
from the DataSet object to the control
Private Sub Page_Load(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles MyBase.Load
    ' Fill the data set.
    SqlDataAdapter1.Fill(DataSet1)
    ' Update the DataGrid.
    DataGrid1.DataBind()
End Sub
Output
System.Data.OleDb

• OleDbConnection class
• OleDbCommand class
• OleDbDataReader
• OleDbDataAdapter
• DataSet 
Example
Imports System.Data.OleDb
'namespace to be imported
Public Class WebForm5 Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
#End Region
Dim myConn As OleDbConnection
Dim myComm As OleDbCommand
Dim dr As OleDbDataReader
Private Sub Select_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Select.Click
Try
myConn = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=D:\suganyavb.net\student.mdb")
Example
contd..
myConn.Open()
myComm = New OleDbCommand("Select* from csc",
myConn)
dr = myComm.ExecuteReader
Do While dr.Read
'reading from the datareader
Response.Write(dr(0) & " ")
Response.Write(dr(1) & " ")
Response.Write(dr(2) & "<br>")
'displaying data from the table
'html break is used to display data in a tabular format
Loop
Catch
End Try
End Sub
End Class
Output
Insert Command
Add a Button control to the Web Forms page
.

Private Sub Insert_Click(ByVal sender As System.Object, ByVal e As_


System.EventArgs) Handles Insert.Click
Try
myConn = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=D:\suganyavb.net\student.mdb")
myConn.Open()
myComm = New OleDbCommand("Insert into
csc(rollno,name,department) values
(906486,’Swathi’,’csc’)", myConn)
Dim sa As Integer = myComm.ExecuteNonQuery()
Response.Write("Records inserted" & " " & sa)
myConn.Close()
Catch
End Try
End Sub
Output
Update Command
Add a Button control to the Web Forms page
Dim con As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
Dim cmd As New OleDb.OleDbCommand

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
con = New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\suganyavb.net\studentdb.mdb")
If con.State = ConnectionState.Closed Then con.Open()
End Sub
Update Command
contd..
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Try
cmd = New OleDbCommand("update csc set
name='abirami' where rollno=906471", con)
Dim sa As Integer = cmd.ExecuteNonQuery()
Response.Write("Records Updated" & " " & sa)
con.Close()
Catch
End Try
End Sub
Output
Delete Command
Private Sub Delete_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Delete.Click
Try
myConn = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=D:\suganyavb.net\studentdb.mdb")
myConn.Open()
myComm = New OleDbCommand("Delete from csc where
rollno=906489", myConn)
Dim sa As Integer = myComm.ExecuteNonQuery()
Response.Write("Records deleted" & " " & sa)
Catch
End Try
End Sub
Output
System.Data.SqlClient

• SqlConnection Class
• SqlCommand Class
• SqlDataReader
• SqlDataAdapter
Example

Open a new Web Forms page, add a Button to it


Imports System.Data.SqlClient

Dim myConnection As SqlConnection


Dim myCommand As SqlCommand
Example
contd..
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Button1.Click
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=master")
'establishing connection. you need to provide password for sql
server
Try
myConnection.Open()
'opening the connection
myCommand = New SqlCommand("Select * from emp",
myConnection)
Dim dr As SqlDataReader = myCommand.ExecuteReader()
While dr.Read()
'reading from the datareader
Example
contd..
Response.Write(dr(0).ToString() + "<br>")
Response.Write(dr(1).ToString() + "<br>")
'displaying data from the table
End While
dr.Close()
myConnection.Close()
Catch
End Try
End Sub
Insert Command
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e_
As System.EventArgs) Handles Button2.Click
Dim ra As Integer
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=mast
er")
'you need to provide password for sql server
myConnection.Open()
myCommand = New SqlCommand("Insert into
emp(number,name) values (106,‘Barathy‘)", myConnection)
ra= myCommand.ExecuteNonQuery()
'Since no value is returned we use ExecuteNonQuery
Response.Write("Records Inserted" & ra)
myConnection.Close()
End Sub
Delete Command
Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Button3.Click
Dim ss As Integer
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=m
aster")
myConnection.Open()
myCommand = New SqlCommand("Delete from emp
where name=‘Barathy' ", myConnection)
ss = myCommand.ExecuteNonQuery()
Response.Write("Records affected" & ss)
myConnection.Close()
End Sub
Update Command
Private Sub Button4_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Button4.Click
Dim sss As Integer
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
myConnection.Open()
myCommand = New SqlCommand("Update emp Set
name=‘Balamani' where number=104 ",_ myConnection)
sss = myCommand.ExecuteNonQuery()
Response.Write("Records affected" & sss)
myConnection.Close()
End Sub
Data Adapter Configuration
Wizard
The DataAdapter Configuration wizard let's you customize
your data adapter as you want, like displaying the whole
table or displaying selected columns from the table and so
on.

To start creating your own DataAdapter, open a blank form


and add a button (Button1) and a DataGrid control to it
from the toolbox. Our intention here is to display the table
or some columns in the table which we created in Access
in the DataGrid control when Button1 is clicked.
Data Adapter Configuration Wizard
contd..

To do that, click on the Data tab in the toolbox and double-


click OleDbDataAdapter object. We are using
OleDbDataAdapter here as we are working with an OleDb
data source. After you select OleDbDataAdapter from the
data tab in the toolbox it gets added to the component tray
beneath the Web Forms designer and opens the Data
Adapter Configuration wizard dialog box 
Data Adapter Wizard
Data Connection

Click the Next> button


in the Data Adapter
Configuration wizard
to select the data
connection you want
to use.
Data Link Properties
Query Type
Sql Statement
Query Builder
View Wizard
Preview
Once you finish
configuring your data
adapter you can preview
the data that will be
displayed on the Web
Forms page. To preview
your data select Data-
>Preview Data from the
main menu which opens
the Data Adapter Preview
window
Creating Dataset
Auto Format
Property Builder

The Property Builder tool


is another way of
customizing the data grid.
To open Property Builder,
right-click on the data grid
and select property builder
from the menu.
Data Form Wizard

The Data Form Wizard is the easiest and fastest way to


develop database applications without writing a single line
of code.

Data Form Wizard select Project->Add New Item->Data


Form Wizard from the main menu.
Data Form Wizard
Dialog Box
Select Data Form Wizard,
type a name for it and
click Open. Once
you click open, a
new dialog opens up
which is the Data Form
wizard
Data Set

you need to specify


the name for your
DataSet.
Data Connection
we need to establish a
connection to the
database. Click on the
"New Connection" button
which opens up the "Data
Link Properties" dialog.
Set a connection to the
database in the Data Link
properties dialog.
Tables

This dialog box displays
all the tables available
in your  database. Select
the table you want to work
with and add it using the
forward arrow button.
Master Relation Ship
This dialog box allows us to
display columns from more
than one table by
establishing a master-detail
relationship and providing a
name for that
relation. A master-detail
relationship can only
be established if you have a
common column in both the
tables. Since we are working
with one table, click next on
this dialog box.
Columns
This dialog allows you to select
the columns you want to
display. Select the columns you
wish to display and click finish.
That finishes configuring the
Data Form Wizard. You will
notice a Load button and the
columns you selected being
added to the Web Forms
designer. Run the
DataWebForm and click the
load button. The data will be
dsiplayed on the Web page.
Design Screen
Ouptut
Data Binding

Data Binding is binding controls to data from


databases. With data binding we can bind a
control to a particular column in a table from the
database or we can bind the whole table to the data
grid.
Features

• Simple, Convenient
• Powerful way to create read/write link
• Use of Data View
Comparison of VB.Net
• ASP.NET is slightly different to working with it in
VB .NET
• In VB .NET, there is BindingContext object to
handle
• In ASP.NET, there is no BindingContext object to
handle
• We bind the controls using data view and use the
RowFilter property of the data view to select the
record we want the bound control to display.
Example-Design Screen
DataTab

From the Data tab of


the toolbox drag a
OledbDataAdapter
object and configure it
generate the dataset by
selecting Data-
Generate DataSet from
the main menu.
Data View Property

Since we will use a


DataView instead of a
Dataset to navigate
through the record, drag a
DataView object on to the
Web Forms page and set
the Table property of the
data view to
Dataset11.Table1. 
Data Binding in Text Box

Select TextBox1, click on the


ellipse button for it's
DataBindings property to open
the DataBinding's dialog box.

In the Data Binding's dialog we


need to bind TextBox1 to the
Rollno field, TextBox2
to Name field and TextBox3 to
the Department field using the
DataView (DataView1). You
should not use DataSet as you
would in a Windows
Application.
Code-Page Load

Private Sub Page_Load(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
DataSet11.Clear()
OleDbDataAdapter1.Fill(DataSet11)
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub
Code-First
Private Sub First_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles First.Click
Dim abc As String
Dim index As Integer = 0
Me.ViewState("index") = index
'creating a variable named index to keep track of the record that is
currently being
'displayed and saving it's state across server round trips using the
ViewState property
abc = DataSet11.Tables(0).Rows(index).Item("rollno")
'selecting a record in the dataview using it's RowFilter property and
specifying
'the text value of a field
DataView1.RowFilter = "rollno='" & abc & " '"
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub
Code-Previous
Private Sub Previous_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Previous.Click
Dim abc As String
Dim index As Integer
index = Me.ViewState("index")
index -= 1
'checking the index value and setting it to a value less than the
current one so that
'it displays the previous record each time the previous button is
clicked
If index < 0 Then
Response.Write(“Already in First Record”)
Exit Sub
End If
Code-Previous
contd..
'if first record is reached it will display the first record each
time the previous
'button is clicked
Me.ViewState("index") = index
abc = DataSet11.Tables(0).Rows(index).Item("rollno")
DataView1.RowFilter = "rollno='" & abc & "'"
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub
Code-Next
contd..
Dim abc As String
Dim index As Integer
index = Me.ViewState("index")
index += 1
'checking the index value and setting it to a value one more
than the current one
'so that it displays next record each time the next button is
clicked
If index > DataSet11.Tables(0).Rows.Count - 1 Then
index = DataSet11.Tables(0).Rows.Count - 1
index = 0
End If
Code-Next
contd..
'if last record is reached it displays the last record each time the
next button is
'clicked. the code counts for the number of rows in the table
and sets the last row to the
'index variable once the last row is reached
Me.ViewState("index") = index
abc = DataSet11.Tables(0).Rows(index).Item("rollno")
DataView1.RowFilter = "rollno='" & abc & "'"
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub
Code-Last
Private Sub Last_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Last.Click
Dim abc As String
Dim index As Integer
index = DataSet11.Tables(0).Rows.Count - 1
'the code counts for the number of rows in the table and sets the last row
to the
'index variable so that last record is displayed each time the last button is
clicked
Me.ViewState("index") = index
abc = DataSet11.Tables(0).Rows(index).Item("rollno")
DataView1.RowFilter = "rollno='" & abc & "'"
TextBox1.DataBind()
TextBox2.DataBind()
TextBox3.DataBind()
End Sub
Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy