Registration open for ASP.NET Core and Angular instructor-led online courses. Courses conducted by Bipin Joshi on weekends. Click here for more details.

Introduction to ADO.NET

Introduction

This is a set of questions and answers related to
ADO.NET. All the code is in Beta 2. The questions by no means
cover the entire capabilities of ADO.NET. Specifically, I have tried to answer
following questions :

What is ADO.NET?

Is ADO.NET different than ADO?

What are managed providers?

What are the data access namespaces in .NET?

What are main objects in ADO.NET ?

How do I establish connection with a database?

How do I use Command object to execute action queries?

How do I use command object to execute Select queries?

How do I use command object to execute stored procedure?

What is DataAdapter and Dataset?

How do i populate a dataset?

How do I navigate dataset?

How do I add new record?

How do I change data ?

How do I delete a row?

How do I filter data once arrived in data set?

How do I handle errors ?

How do I handle transactions?

What is ADO.NET?

ADO.NET is next generation of data access technology from Microsoft target at
.NET platform. ADO.NET is built with
distributed and internet applications in mind. ADO.NET provides strong support for XML and
disconnected data processing.

Is ADO.NET different than ADO?

ADO.NET is much different than ADO. In order to achieve
disconnected data access programmers have to use different techniques
like disconnected recordsets, RDS etc. ADO object model is very small
as compared to ADO.NET. ADO.NET provides number of specialized objects to handle very specific tasks. Microsoft has taken care to
closely map properties and methods of ADO.NET objects with existing ADO counterparts. As per
Microsoft ADO.NET is not a replacement for ADO but an enhancement in the overall data access
technology. You can use both ADO and ADO.NET in your application.

What are managed providers?

A managed provider is analogous to
ODBC driver or OLEDB provider. It performs operation
of communicating with the database. ADO.NET currently provides two distinct managed providers. The SQL Server managed provider is used
with SQL server and is a very efficient way of communicating with SQL Server. OLEDB managed
provider is used to communicate with any OLEDB compliant database like Access or Oracle. The data
access APIs for both the providers are found in separate namespaces.

What are the data access namespaces in .NET?

Following are the most common data access namespaces :

System.Data

System.Data.OleDb

System.Data.SQLClient

System.Data.SQLTypes

System.Data.XML

What are main objects in ADO.NET?

Following are the main objects in ADO.NET :

OleDbConnection / SQLConnection

OleDbCommand / SQLCommand

OleDbDataReader / SQLDataReader

OleDbDataAdapter / SQLDataAdapter

OleDbParameter / SQLParameter

DataSet

DataTable

DataView

DataRow

DataColumn

Most of the objects mentioned above can be created as
'stand-alone' objects via code. In the discussion below we will see all the
examples with OLEDB objects. The usage for SQL server objects is same except
name changes. Also, for simplicity I have omitted the repetitive code
(like opening a database connection) in later examples.

How do I establish connection with a database?

The way you connect to a database is very similar to that used in ADO. You use
connection object to accomplish this. Consider following code fragment :

p=new OleDbParameter("name",type,width)
p.value="myvalue"cmd.parameters.Add(p)'add all the parameters in the same way
cmd.ExecuteNonQuery()

What is DataAdapter and Dataset?

Dataset is an in-memory disconnected representation of data
from actual database. Dataset can be thought of as a collection of recordsets. Each
such recordset is called as a DataTable. Note that data table can be
based on JOIN queries. Dataset is much more powerful than a simple
collection of DataTables. You can also put relations and constraints
within various datatables of a dataset. DataSet and its constituent parts like DataTable, DataRow can also be created programmatically.

Since dataset is totally disconnected from the database
there must be some means of communication between the dataset and the database.
DataAdapter is used for that purpose. The main functions performed by DataAdapter are :

Populate the dataset by fetching data from database

Updating changes made to the dataset back to the database

Changes made to the dataset are not written to the database unless explicitly updated via DataAdapter.

How do i populate a dataset?

To populate a dataset you will use DataAdapter's Fill
method. One DataAdapter can be used to populate several tables of one or many datasets.

How do I add new record to a dataTable?

Here, note that you are issuing update on data adapter rather than dataset itself
because dataset is actually a disconnected view of data and you must have valid connection
information to update changes to original database.

How do I change data from a DataTable?

Dim row as DataRow

'i can be any number in below statementrow= ds.Tables("data_table_name").Rows(i)
row("field1")="value1"
row("field1")="value1"
dscmd.update(ds,"data_table_name")

In above code you can set row level error messages depending on certain business validation. This
will allow us to use common error handling routines. Also, note that the RowError property
is also set by ADO.NET if there is any real database error.

How do I handle transactions?

ADO.NET offers OleDbTransaction object to control a transaction. This object is returned by BeginTransaction
method of OleDbConnection.

Bipin Joshi is a software consultant, trainer, author and yoga mentor having 22+ years of experience in software development. He also conducts online courses on ASP.NET MVC, ASP.NET Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.