Architecture of
Architecture of
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
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.
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.
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()
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
VB.Net
Dim reader As SqlDataReader = cmd.ExecuteReader
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
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
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)