VB.NET — Study Notes

Unit 05

ADO.NET & Database Connectivity

6 Questions 6 Marks Each VB.NET
01
6 Marks Question

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

Features
  • Provides access to multiple data sources — SQL Server, Oracle, MySQL, XML
  • Supports both connected and disconnected architecture
  • Uses namespaces like System.Data and System.Xml
  • Efficient and scalable for data-driven applications
  • Supports XML integration

ADO.NET Architecture

ADO.NET works mainly with two components:

Connected Architecture — .NET Data Providers

Used to connect directly to the database. Requires an active connection. Faster but connection must remain open.

Connection
Establishes connection with the database.
Command
Executes SQL queries on the database.
DataReader
Reads data in forward-only, read-only mode.
DataAdapter
Acts as a bridge between database and DataSet.
Disconnected Architecture — DataSet

Works without continuous connection to the database. Stores data in memory.

DataTable
Represents a table of data.
DataRow
Represents a single row in a table.
DataColumn
Represents columns in a table.
DataRelation
Defines relationships between tables.
DataView
Used for sorting and filtering data.

Advantages of ADO.NET

  • High performance and scalability
  • Supports multiple data sources
  • Enables offline data processing
  • Easy data manipulation and integration with XML
Conclusion ADO.NET is a powerful data access technology in .NET that provides efficient communication between applications and data sources. Its support for both connected and disconnected architectures makes it flexible and widely used.
02
6 Marks Question

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.

Part 1 — .NET Data Providers
Connection Establishes connection with the data source SqlConnection, OleDbConnection

Opens and manages the connection to the database. Must be opened before any operation and closed after.

Command Executes SQL queries and stored procedures SELECT, INSERT, UPDATE, DELETE

Can execute SQL statements or stored procedures. Works with the Connection object to perform operations on the database.

DataReader Retrieves data in forward-only, read-only mode Fast & efficient

Fast and efficient for reading large result sets. Requires an open connection throughout reading. Cannot modify data.

DataAdapter Bridge between DataSet and database Fill() · Update()

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 DataSet
  • Update() — Saves DataSet changes back to database
Part 2 — DataSet Components

A DataSet is an in-memory representation of data that allows working without an active database connection.

DataTable
Represents a table of data in memory.
DataRow
Represents a single row in a DataTable.
DataColumn
Represents a column in a DataTable.
DataRelation
Defines relationships between tables.
DataView
Used for sorting and filtering data.

Working of ADO.NET Object Model

1
Fetch Data
Data is fetched from database using the DataProvider.
2
Fill DataSet
DataAdapter fills the DataSet with the fetched data.
3
Work Offline
DataSet stores data in memory for offline manipulation.
4
Update Back
Changes made in DataSet are pushed back to the database via DataAdapter.
Conclusion The ADO.NET Object Model provides a structured way to handle data using both connected and disconnected approaches. It ensures efficient data access, manipulation, and management in .NET applications.
03
6 Marks Question

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.

Connected Architecture

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

Disconnected Architecture

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

How ADO.NET Achieves Both

Connected Architecture Flow

1
Open Connection
Use Connection object to open a database connection.
2
Execute Command
Use Command object to run SQL queries.
3
Read Data
Use DataReader to read data row by row.
4
Close Connection
Close DataReader and Connection when done.

Disconnected Architecture Flow

1
Open Connection
Temporarily open a connection to the database.
2
Fill DataSet
DataAdapter fetches data and fills the DataSet.
3
Close Connection
Connection is closed after data is loaded into memory.
4
Work Offline & Update
Manipulate data in DataSet, then update database later using DataAdapter.

Comparison Table

BasisConnectedDisconnected
ConnectionRequires continuous connectionNo continuous connection required
Component UsedDataReaderDataSet
PerformanceFaster for real-time dataSuitable for offline processing
Network UsageHigh network usageLow network usage
FlexibilityLess flexibleMore flexible
Conclusion ADO.NET provides both architectures to ensure flexibility. Connected architecture is useful for fast, real-time operations while disconnected architecture is ideal for efficient and scalable applications.
04
6 Marks Question

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

DataAdapter acts as a bridge between the database and DataSet. It executes SQL commands to fetch and save data.

Fill()
Loads data from the database into a DataSet.
Update()
Saves changes from the DataSet back to the database.
DataSet

DataSet is an in-memory collection of data. It can hold multiple tables, supports relations, filtering, and sorting, and allows offline data manipulation.

DataTable
Represents a table in memory.
DataRow
Represents a single row.
DataColumn
Represents a column.
DataRelation
Defines table relationships.

Working of DataAdapter and DataSet

1
DataAdapter opens connection
Connects to database using OleDbConnection or SqlConnection.
2
Fetches data
Executes SELECT query to retrieve records.
3
Fills DataSet
Calls da.Fill(ds, "TableName") to load data into memory.
4
Connection closed
User works with data offline in the DataSet.
5
Changes sent back
Calls da.Update(ds, "TableName") to save changes to database.

Example — Fetching Data using OLEDB

VB.NET Code
Imports 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")
Conclusion Together, DataAdapter and DataSet enable efficient disconnected data access, reducing database load and improving performance.
  • DataAdapter → Connects DataSet with database
  • DataSet → Stores and manages data in memory
05
6 Marks Question

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

Uses
  • Executes SQL statements — SELECT, INSERT, UPDATE, DELETE
  • Can execute stored procedures
  • Can return results or perform actions without returning data
  • Works with the Connection object

Types of Command Execution

Query Commands
Return data — used with SELECT.
Non-Query Commands
Modify data — INSERT, UPDATE, DELETE.
Scalar Commands
Return a single value — e.g., COUNT, SUM.

Methods of Command Object

01 ExecuteReader() Retrieves multiple rows

Executes a query and returns data in the form of a DataReader. Used for retrieving multiple rows. Requires an open connection.

Example
Dim cmd As New OleDbCommand("SELECT * FROM Student", con)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
02 ExecuteNonQuery() INSERT, UPDATE, DELETE

Executes commands that do not return data. Used for INSERT, UPDATE, DELETE. Returns the number of affected rows.

Example
Dim cmd As New OleDbCommand("UPDATE Student SET Name='Ali' WHERE ID=1", con)
cmd.ExecuteNonQuery()
03 ExecuteScalar() Returns single value

Returns a single value (first row, first column). Useful for aggregate functions like COUNT, SUM.

Example
Dim cmd As New OleDbCommand("SELECT COUNT(*) FROM Student", con)
Dim count As Integer = cmd.ExecuteScalar()
04 ExecuteXmlReader() Returns XML result

Returns the result in XML format. Used when working with XML data sources.

Conclusion The Command object is essential in ADO.NET for executing SQL operations. Its methods provide different ways to interact with the database efficiently.
  • ExecuteReader() → Retrieves multiple rows as DataReader
  • ExecuteNonQuery() → Executes INSERT / UPDATE / DELETE
  • ExecuteScalar() → Returns a single aggregate value
  • ExecuteXmlReader() → Returns data in XML format
06
6 Marks Question

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

Key Characteristics
  • Works with the Command object
  • 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

1
Create a Connection
Create an OleDbConnection with the connection string.
2
Open the Connection
Call con.Open() to establish the database connection.
3
Create a Command
Create an OleDbCommand with the SQL query.
4
Execute Reader
Call cmd.ExecuteReader() to get the DataReader.
5
Read Data
Loop using While dr.Read() to read rows one by one.
6
Close DataReader and Connection
Call dr.Close() and con.Close() when done.

Example using OLEDB

VB.NET Code
Imports 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

✓ Advantages
  • High speed and efficiency
  • Minimal memory usage
  • Suitable for large datasets
✗ Disadvantages
  • Cannot modify data
  • Requires open connection
  • Not suitable for offline operations
Conclusion Database connectivity using DataReader is ideal for fast data retrieval in connected architecture. It is efficient for reading large amounts of data but lacks the flexibility of DataSet.