Accessing Data in ADO. Net With ASP. Net
Accessing Data in ADO. Net With ASP. Net
NET
• Data Set
• Data Provider
Data Set
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)
• 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
• 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
.
• SqlConnection Class
• SqlCommand Class
• SqlDataReader
• SqlDataAdapter
Example
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
• 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