What is ADO.NET? Explain in Detail
Introduction
ADO.NET (ActiveX Data Objects .NET) is a data access technology provided by Microsoft as part of the .NET Framework. It is used to connect applications with various data sources such as databases and XML files.
Definition: ADO.NET is a set of classes that allows a .NET application to connect to data sources, retrieve data, insert/update/delete data (CRUD operations), and manage data efficiently.
Features of ADO.NET
- Provides access to multiple data sources — SQL Server, Oracle, MySQL, XML
- Supports both connected and disconnected architecture
- Uses namespaces like
System.DataandSystem.Xml - Efficient and scalable for data-driven applications
- Supports XML integration
ADO.NET Architecture
ADO.NET works mainly with two components:
Used to connect directly to the database. Requires an active connection. Faster but connection must remain open.
Works without continuous connection to the database. Stores data in memory.
Advantages of ADO.NET
- High performance and scalability
- Supports multiple data sources
- Enables offline data processing
- Easy data manipulation and integration with XML
ADO.NET Object Model
Introduction
The ADO.NET Object Model defines the structure used to access and manage data in .NET applications. It provides a set of classes under the System.Data namespace to interact with data sources efficiently. It is divided into two parts: .NET Data Providers and DataSet.
Opens and manages the connection to the database. Must be opened before any operation and closed after.
Can execute SQL statements or stored procedures. Works with the Connection object to perform operations on the database.
Fast and efficient for reading large result sets. Requires an open connection throughout reading. Cannot modify data.
Acts as a mediator. Uses SQL commands (Select, Insert, Update, Delete) to fetch data into DataSet and push changes back to the database.
Fill()— Loads data from database into DataSetUpdate()— Saves DataSet changes back to database
A DataSet is an in-memory representation of data that allows working without an active database connection.
Working of ADO.NET Object Model
DataProvider.DataAdapter fills the DataSet with the fetched data.Connected and Disconnected Architecture in ADO.NET
Introduction
ADO.NET supports two types of data access approaches — Connected and Disconnected Architecture — which define how an application interacts with a database.
The application maintains a continuous connection with the database while accessing data.
- Connection remains open during operations
- Faster for real-time data access
- Uses more network resources
Components: Connection · Command · DataReader
Data is retrieved once and stored in memory. The connection is then closed.
- No need to keep connection open
- Works offline
- Reduces network load
- More flexible
Components: DataAdapter · DataSet
Connected Architecture Flow
Connection object to open a database connection.Command object to run SQL queries.DataReader to read data row by row.Disconnected Architecture Flow
DataAdapter fetches data and fills the DataSet.Comparison Table
| Basis | Connected | Disconnected |
|---|---|---|
| Connection | Requires continuous connection | No continuous connection required |
| Component Used | DataReader | DataSet |
| Performance | Faster for real-time data | Suitable for offline processing |
| Network Usage | High network usage | Low network usage |
| Flexibility | Less flexible | More flexible |
DataAdapter and DataSet with Examples
Introduction
In ADO.NET, DataAdapter and DataSet are used to implement disconnected architecture. They allow applications to retrieve data, work with it in memory, and update the database later.
DataAdapter acts as a bridge between the database and DataSet. It executes SQL commands to fetch and save data.
DataSet is an in-memory collection of data. It can hold multiple tables, supports relations, filtering, and sorting, and allows offline data manipulation.
Working of DataAdapter and DataSet
da.Fill(ds, "TableName") to load data into memory.da.Update(ds, "TableName") to save changes to database.Example — Fetching Data using OLEDB
VB.NET CodeImports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Step 1: Create connection
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=student.accdb")
' Step 2: Create DataAdapter
Dim da As New OleDbDataAdapter("SELECT * FROM Student", con)
' Step 3: Create DataSet
Dim ds As New DataSet()
' Step 4: Fill DataSet
da.Fill(ds, "Student")
' Step 5: Display first row, Name column
MsgBox(ds.Tables("Student").Rows(0)("Name").ToString())
End Sub
End Class
Updating Data
VB.NET Code' Modify data in DataSet
ds.Tables("Student").Rows(0)("Name") = "Updated Name"
' Push changes back to database
da.Update(ds, "Student")
DataAdapter→ Connects DataSet with databaseDataSet→ Stores and manages data in memory
Command Object and its Methods
Introduction
The Command object in ADO.NET is used to execute SQL queries or commands on a data source. It acts as a bridge between the application and the database for performing operations. It works together with the Connection object.
Use of Command Object
- Executes SQL statements —
SELECT,INSERT,UPDATE,DELETE - Can execute stored procedures
- Can return results or perform actions without returning data
- Works with the
Connectionobject
Types of Command Execution
SELECT.INSERT, UPDATE, DELETE.Methods of Command Object
Executes a query and returns data in the form of a DataReader. Used for retrieving multiple rows. Requires an open connection.
Dim cmd As New OleDbCommand("SELECT * FROM Student", con)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
Executes commands that do not return data. Used for INSERT, UPDATE, DELETE. Returns the number of affected rows.
ExampleDim cmd As New OleDbCommand("UPDATE Student SET Name='Ali' WHERE ID=1", con)
cmd.ExecuteNonQuery()
Returns a single value (first row, first column). Useful for aggregate functions like COUNT, SUM.
ExampleDim cmd As New OleDbCommand("SELECT COUNT(*) FROM Student", con)
Dim count As Integer = cmd.ExecuteScalar()
Returns the result in XML format. Used when working with XML data sources.
ExecuteReader()→ Retrieves multiple rows as DataReaderExecuteNonQuery()→ Executes INSERT / UPDATE / DELETEExecuteScalar()→ Returns a single aggregate valueExecuteXmlReader()→ Returns data in XML format
Database Connectivity using DataReader
Introduction
In ADO.NET, DataReader is used to retrieve data from a database in a connected architecture. It provides a fast and efficient way to read data in a forward-only and read-only manner.
DataReader Overview
- Works with the
Commandobject - Requires an open connection throughout reading
- Reads one row at a time — very memory efficient
- Cannot modify data — read-only
- Forward-only — cannot go backward
- Very fast and lightweight
Steps for Database Connectivity using DataReader
OleDbConnection with the connection string.con.Open() to establish the database connection.OleDbCommand with the SQL query.cmd.ExecuteReader() to get the DataReader.While dr.Read() to read rows one by one.dr.Close() and con.Close() when done.Example using OLEDB
VB.NET CodeImports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Step 1: Create connection
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=student.accdb")
' Step 2: Open connection
con.Open()
' Step 3: Create command
Dim cmd As New OleDbCommand("SELECT * FROM Student", con)
' Step 4: Execute reader
Dim dr As OleDbDataReader = cmd.ExecuteReader()
' Step 5: Read data row by row
While dr.Read()
MsgBox(dr("Name").ToString())
End While
' Step 6: Close reader and connection
dr.Close()
con.Close()
End Sub
End Class
Advantages and Disadvantages
- High speed and efficiency
- Minimal memory usage
- Suitable for large datasets
- Cannot modify data
- Requires open connection
- Not suitable for offline operations