0% found this document useful (0 votes)
63 views14 pages

Architecture of

The document provides an overview of ADO.NET architecture including its key components like data providers and DataSet. It describes various ADO.NET objects like connection, command, data adapter and data reader and how they are used to connect to data sources and perform data operations.

Uploaded by

Rajendran Sheeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views14 pages

Architecture of

The document provides an overview of ADO.NET architecture including its key components like data providers and DataSet. It describes various ADO.NET objects like connection, command, data adapter and data reader and how they are used to connect to data sources and perform data operations.

Uploaded by

Rajendran Sheeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ARCHITECTURE OF ADO.

NET
ADO.NET consist of a set of Objects that expose data access services to the .NET
environment. It is a data access technology from Microsoft .Net Framework, which provides
communication between relational and non-relational systems through a common set of
components .

System.Data namespace is the core of ADO.NET and it contains classes used by all
data providers. ADO.NET is designed to be easy to use, and Visual Studio provides several
wizards and other features that you can use to generate ADO.NET data access code.

Data Providers and DataSet


The two key components of ADO.NET are Data Providers and DataSet. The Data
Provider classes are meant to work with different kinds of data sources. They are used to
perform all data-management operations on specific databases. DataSet class provides
mechanisms for managing data when it is disconnected from the data source.

Data Providers
The .Net Framework includes mainly three Data Providers for ADO.NET. They are the
Microsoft SQL Server Data Provider , OLEDB Data Provider and ODBC Data Provider
. SQL Server uses the SqlConnection object , OLEDB uses the OleDbConnection Object and
ODBC uses OdbcConnection Object respectively.

A data provider contains Connection, Command, DataAdapter, and DataReader


objects. These four objects provides the functionality of Data Providers in the ADO.NET.

Connection
The Connection Object provides physical connection to the Data Source. Connection
object needs the necessary information to recognize the data source and to log on to it properly,
this information is provided through a connection string.
The Connection Object is a part of ADO.NET Data Provider and it is a unique session
with the Data Source. The Connection Object provides physical connection to the Data Source.
Connection object needs the necessary information to recognize the data source and to log on
to it properly, this information is provided through a connection string.
The Connection Object connect to the specified Data Source and open a connection
between the application and the Data Source, depends on the parameter specified in the
Connection String . When the connection is established , SQL Commands will execute with
the help of the Command Object and retrieve or manipulate data in the Data Source. Once the
Database activity is over , Connection should be closed and release the Data Source resources
. The type of the Connection is depend on which Data Source system you are working with.
The following are the commonly used Data Providers for ADO.NET applications.

Sql Server Connection


The SqlConnection Object is Handling the part of physical communication between the
ASP.NET application and the SQL Server Database . An instance of the SqlConnection class
in ASP.NET is supported the Data Provider for SQL Server Database.

VB.Net
Dim connectionString As String
connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString

When the connection is established , SQL Commands will execute with the help of the
Command Object and retrieve or manipulate the data in the database. Once the Database
activities is over , Connection should be closed and release the Data Source resources .
The Close() method in SqlConnection Class is used to close the Database Connection.
The Close method rolls back any pending transactions and releases the Connection from the
SQL Server Database.

Command
The Command Object uses to perform SQL statement or stored procedure to be
executed at the Data Source. The command object provides a number of Execute methods that
can be used to perform the SQL queries in a variety of fashions.
The Command Object requires an instance of an ASP.NET Connection Object for
executing the SQL statements . The Command Object in ADO.NET executes SQL statements
and Stored Procedures against the data source specified in the ASP.NET Connection Object.
The Command Object has a property called CommandText , which contains a String
value that represents the command that will be executed against the Data Source. When the
CommandType property is set to StoredProcedure, the CommandText property should be set
to the name of the stored procedure.

ExecuteNonQuery
ExecuteNonQuery executes a Transact-SQL statement against the connection and
returns the number of rows affected.
VB.Net
command.ExecuteNonQuery()

The ExecuteNonQuery() performs Data Definition tasks as well as Data Manipulation


tasks also. The Data Definition tasks like creating Stored Procedures ,Views etc. perform by
the ExecuteNonQuery() . Also Data Manipulation tasks like Insert , Update , Delete etc. also
perform by the ExecuteNonQuery() of SqlCommand Object.

Example: ExecuteNonQuery
default.aspx.cs
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

default.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class _Default


Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim connection As SqlConnection
connectionString =
ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString
connection = New SqlConnection(connectionString)
Dim sql As String = "insert into discounts values('New
Discont',8042,1000,1000,5.25)"
Try
connection.Open()
Dim cmd As New SqlCommand(sql, connection)
cmd.ExecuteNonQuery()
connection.Close()
Label1.Text = "Successfully Inserted !!"
Catch ex As Exception
Label1.Text = "Error inserting data" & ex.ToString
End Try
End Sub
End Class
ExecuteReader
The ExecuteReader() in SqlCommand Object sends the SQL statements to the
Connection Object and populate a SqlDataReader Object based on the SQL statement. When
the ExecuteReader method in SqlCommand Object execute , it will instantiate a
SqlClient.SqlDataReader Object.

VB.Net
Dim reader As SqlDataReader = cmd.ExecuteReader

The SqlDataReader Object is a stream-based , forward-only, read-only retrieval of


query results from the Data Source, which do not update the data it contains. The
SqlDataReader cannot be created directly from code, they can created only by calling the
ExecuteReader method of a Command Object.

Example: ExecuteReader
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
<br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
default.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class _Default


Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim connection As SqlConnection
connectionString =
ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString
connection = New SqlConnection(connectionString)
Dim sql As String = "select au_lname,au_fname from authors"
Try
connection.Open()
Dim cmd As New SqlCommand(sql, connection)
Dim reader As SqlDataReader = cmd.ExecuteReader
While reader.Read()
ListBox1.Items.Add(reader.Item(0) & " " &
reader.Item(1))
End While
connection.Close()
Catch ex As Exception
Label1.Text = "Error in ExecuteReader " & ex.ToString
End Try
End Sub
End Class
ExecuteScalar
ExecuteScalar method uses to retrieve a single value from a database. The
ExecuteScalar() executes SQL statements as well as Stored Procedure and returns the first
column of the first row in the result set, or a null reference if the result set is empty.

VB.Net
Dim result As Int32 = Convert.ToInt32(cmd.ExecuteScalar)

It is very useful to use with aggregate functions like Count(*) or Sum() etc. When
compare to ExecuteReader() , ExecuteScalar() uses fewer System resources.

Example: ExecuteScalar
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

default.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim connection As SqlConnection
connectionString =
ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString
connection = New SqlConnection(connectionString)
Dim sql As String = "select count(*) from authors"
Try
connection.Open()
Dim cmd As New SqlCommand(sql, connection)
Dim result As Int32 = Convert.ToInt32(cmd.ExecuteScalar)
connection.Close()
Label1.Text = "Number of rows in author table - " & result
Catch ex As Exception
Label1.Text = "Error in ExecuteScalar " & ex.ToString
End Try
End Sub
End Class

DataReader
The DataReader Object is a stream-based , forward-only, read-only retrieval of query
results from the Data Source, which do not update the data. DataReader requires a live
connection with the databse and provides a very intelligent way of consuming all or part of the
result set.
DataReader Object in ADO.NET is a stream-based , forward-only, read-only retrieval
of query results from the Data Sources , which do not update the data. The DataReader cannot
be created directly from code, they can created only by calling the ExecuteReader method of a
Command Object.
After creating an instance of the Command object, you have to create a DataReader by
calling Command.ExecuteReader to retrieve rows from a data source.
VB.Net
Dim reader As SqlDataReader = cmd.ExecuteReader
When the ExecuteReader method in the SqlCommand Object execute , it will instantiate
a SqlClient.SqlDataReader Object. When we started to read from a DataReader it should
always be open and positioned prior to the first record. The Read() method in the DataReader
is used to read the rows from DataReader and it always moves forward to a new valid row, if
any row exist .

Example: DataReader
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
<br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

default.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim connection As SqlConnection
connectionString =
ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString
connection = New SqlConnection(connectionString)
Dim sql As String = "select pub_id,pub_name from publishers"
Try
connection.Open()
Dim cmd As New SqlCommand(sql, connection)
Dim reader As SqlDataReader = cmd.ExecuteReader
While reader.Read()
ListBox1.Items.Add(reader.Item(0) & " - " &
reader.Item(1))
End While
reader.Close()
connection.Close()
Catch ex As Exception
Label1.Text = "Error in SqlDataReader " & ex.ToString
End Try
End Sub
End Class

DataAdapter
DataAdapter Object populate a Dataset Object with results from a Data Source . It is a
special class whose purpose is to bridge the gap between the disconnected Dataset objects and
the physical data source.
DataAdapter serves as a bridge between a DataSet and SQL Server for retrieving and
saving data. We can use SqlDataAdapter Object in combination with Dataset Object.
DataAdapter provides this combination by mapping Fill method, which changes the data in the
DataSet to match the data in the data source, and Update, which changes the data in the data
source to match the data in the DataSet.
VB.Net
Dim adapter As New SqlDataAdapter(sql, connection)
adapter.Fill(ds)
The SqlDataAdapter Object and DataSet objects are combine to perform both data
access and data manipulation operations in the SQL Server Database. When the user perform
the SQL operations like Select , Insert etc. in the data containing in the Dataset Object , it wont
directly affect the Database, until the user invoke the Update method in the SqlDataAdapter.
The DataAdapter can perform Select , Insert , Update and Delete SQL operations in the
Data Source. The SelectCommand property of the DataAdapter is a Command Object that
retrieves data from the data source. Other command properties of the DataAdapter are
Command objects that manage updates to the data in the data source according to modifications
made to the data in the DataSet.

Example: DataAdapter
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
<br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
default.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim connection As SqlConnection
Dim ds As New DataSet
Dim i As Integer
connectionString =
ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString
connection = New SqlConnection(connectionString)
Dim sql As String = "select pub_name from publishers"
Try
connection.Open()
Dim adapter As New SqlDataAdapter(sql, connection)
adapter.Fill(ds)
For i = 0 To ds.Tables(0).Rows.Count - 1
ListBox1.Items.Add(ds.Tables(0).Rows(i).Item(0))
Next
connection.Close()
Catch ex As Exception
Label1.Text = "Error in execution " & ex.ToString
End Try
End Sub
End Class

DataSet
DataSet provides a disconnected representation of result sets from the Data Source, and
it is completely independent from the Data Source. DataSet provides much greater flexibility
when dealing with related Result Sets.
DataSet contains rows, columns,primary keys, constraints, and relations with other
DataTable objects. It consists of a collection of DataTable objects that you can relate to each
other with DataRelation objects. The DataAdapter Object provides a bridge between the
DataSet and the Data Source.

Sample Questions:
1. Write the syntax for creating connection object. (2 Marks)
2. Write the syntax for SQL insert in ADO.NET. (2 Marks)
3. Describe the ADO.NET object model. (5 Marks or 10 Marks)
(Illustrate the design of ADO.Net object model and its data organization.)
4. Explain Executescalar() method with its syntax. (5 Marks)
5. Enlighten the architecture ADO.Net with its approach of handling the data. (10 Marks)
6. Explain with syntax of ExcuteNonQuery ( ) method. (5 Marks)
7. Describe the characteristics of ADO.NET. (10 Marks)

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