Chapter 7
Chapter 7
NET
❖ Introduction of ADO.NET:
In this class library, the functionality provides to connect with data source, execute
commands in data source or store, manipulate or delete data into data source. ADO.NET
introduces the concept of Disconnected Architecture
❖ ADO.NET Architecture :
The ADO.NET Framework supports two models of Data Access Architecture, Connection
Oriented Data Access Architecture and Disconnected Data Access Architecture.
Page 1
Chapter 7 ADO.NET
Entity Framework
ADO.NET
DataProvider
Connection DataSet
DataTables
Command
DataRow
DataAdapter
DataColumn
DataReader
DataRelation
DataRelation
▪ Connected Architecture
In Connection Oriented Data Access Architecture the application make a connection to the
Data Source and then interact with it through SQL request using the same connection. In
these cases the application stays connected to the database system even when it is not using
any database operations.
In this architecture, when you read data from a database by using a DataReader object, an
open connection must be maintained between your application and the data source.
Page 2
Chapter 7 ADO.NET
▪ Disconnected Architecture
The DataSet is the central components in the ADO.NET Disconnected Data Access
Architecture. A DataSet is an in-memory data store that can hold multiple tables at the same
time. DataSet only hold data and do not interect with data source.
The DataSet is not connected directly to a data source through a connection object when you
populate it. It is the DataAdapter that manages connections between data source and DataSet
by fill the data from data source to the DataSet and giving a disconnected behavior to the
DataSet.
Page 3
Chapter 7 ADO.NET
❖ ADO.NET Classes:
The System.Data namespace is the core namespace of ADO.NET. It consists of the base
classes for the ADO.NET Architecture. All Data Provider use these classes. It defines classes
that represent table, columns, and rows datasets. Some common classes from this namespace
DataView, DataSet, DataTable, DataRow, DataColumn and DataRelation. To use these
classes in your applications, you need to add a reference to the System.Data namespace.
Each Data Provider in ADO.NET contains a connection class that inherits from the
System.Data.Common.DbConnection class. The DbConnection serves as the base class for
all the connection classes of different data providers. The following are the connection
classes for each data provider
▪ Basic Steps:
1) Create Database
2) Crate Connection Object
3) Set the provider
4) Specify the data source.
▪ Property:
ConnectionString: A ConnectionString has a group of semicolon (;) separated attributes.
Depends on the parameter specified in the ConnectionString, ADO.NET Connection
object connect to the specified database. For Example:
Page 4
Chapter 7 ADO.NET
▪ Methods:
Methods Description
Open() It opens the connection to our database.
Close() It closes the database connection.
Dispose() It releases the resources on the connection object.
It returns state of connection object. (Return 0 for not connected
State()
or 1 for connected).
❖ Connected Architecture:
➢ Command Class:
Each data provider has their command class which is used to execute SQL commands or
stored procedures to the database. Each command class inherits from the
System.Data.Common.DbCommand base class. The following are the different flavors of
the command class for each data provider. Command object will return single value or
one or more sets of rows or no values at all.
Data Provider Connection Class
SQL Server SqlCommand
OLEDB OledbCommand
ODBC OdbcCommand
▪ Property:
Property Description
This property contains data about the connection string. It must
be set on the SqlCommand object before it executed. For the
Connection
command to execute properly, a connection must be open at the
time of execution
It specifies the SQL command on stored procedure or a name of
CommandText
a table.
▪ Methods:
Methods Description
This method executes the command and returns the number of
ExecuteNonQuery() rows that were affected. It often used with record UPDATA,
DELETE, or INSERT statement.
This method executes the command and retrieves a single
ExecuteScaler() value. It is used with aggregate functions. It return the first
column of the first row of a ResultSet.
This method returns DataReader and executes commands that
ExecuteReader()
return rows.
Page 5
Chapter 7 ADO.NET
➢ DataReader Class:
DataReader is a Read-Only, Forward-Only retrieval of query result from the data sources,
which do not update the data. DataReader requires a continuous connection with the data
source.
The DataReader cannot be created directly from code, they can created only by calling
the ExecuteReader() of a command object. For Example.
A connection object can contains only one DataReader at a time and the connection in the
DataReader remains open, also it cannot be used for any other purpose while data is
being accessed. When we started to read from a DataReader it should always be open and
positioned to the first record.
▪ Property:
Property Description
FieldCount It returns number of columns in a row.
IsClosed It indicated whether a DataReader is closed.
RecordsAffected The number of row affected after a transaction.
▪ Methods:
Methods Description
Close() It closes a DataReader object
Read() It reads next record in the DataReader
❖ Disconnected Architecture
➢ DataSet:
The System.Data.DataSet class holds data that are retrieved from the database. The
DataSet class allows you to hold disconnected data. It means you can work with the
contents of the database while disconnected to the actual database. This is because, the
contents that you will be working on already exists inside the DataSet. You can consider
the DataSet class as a mini Database Management System inside the memory.
DataSet is tabular representation of data. Tabular representation means, it represents data
into row and column format. The DataSet class has a Table property which is a collection
of System.Data.DataTable objects. A database can contain multiple tables. Just like a
database, a DataSet can contain multiple tables as well.
Records retrieved from the actual database are stored as a DataTable and added to the
Tables property of the DataSet. The DataTable also has properties named Columns and
Rows which are collection of DataColumn and DataRow respectively. These properties
contain the individual columns and rows of the table from the database.
Page 6
Chapter 7 ADO.NET
▪ Property:
Property Description
DataSetName It get or set the name of the current DataSet.
HasErrors It indicates if there is an error or not.
Tables It returns the collection of DataTable object.
▪ Methods:
Methods Description
Clear() This method is used to clear data.
ReadXml() It reads XML schema and data into a DataSet.
WriteXml() It write XML schema and data from a DataSet.
➢ Differentiate DataReader V/S DataSet
Sr.
DataReader DataSet
No
1 It is directly connected to the database. It is used disconnected architecture.
2 It can hold on table at a time. It can hold multiple tables at a time.
It provides the both direction forward and
It is forward only and read only
3 backward to navigate the data from the
mechanism for reading the data.
database.
DataReader is a faster mechanism than DataSet is slower than DataReader as it is a
4 DataSet as it is a connected mechanism disconnected mechanism to access
to the database. Database.
DataReader provides less security
DataSet provides a security as it is a
5 because the connection is always open
disconnected architecture from the database.
with the database.
DataReader can have only one row in DataSet can have more than one row or
6
the memory at a time. tables in the memory at a time.
➢ DataAdapter:
The DataAdapter is the class at the core of ADO.NET disconnected data access. It works
as a bridge to provide the communication between database and a DataSet.
The DataAdapter provides a set of methods and properties to retrieve and save data
between a DataSet and database. The DataAdapter is used Fill() method to fill a
DataTable or DataSet.
When the DataAdapter fill a DataSet, it will create necessary tables and columns for the
returned data. DataAdapter is capable of executing ‘Select’ statement on a data source
and transferring the ResultSet into DataSet or DataTable object. It is also capable of
executing ‘Insert’, ‘Update’ and ‘Delete’ Statement.
Page 7
Chapter 7 ADO.NET
▪ Property:
Property Description
It specifies the command object with the SQL command to be
InsertCommand
used for inserting a new record to the database.
It specifies the command object with the SQL command to be
UpdataCommand
used for updating an existing record on the database.
It specifies the command object with the SQL command to be
DeleteCommand
used for deleting a record from the database.
It specifies the command object with the SQL command to be
SelectCommand
used for retrieving record.
▪ Methods:
Methods Description
This method executes the SelectCommand property and fills a
Fill()
DataSet that is provided.
This method calls the Insert, Update or Delete command of this
Updata() DataAdapter and updates the data source using the specified
commands.
➢ DataTable:
A DataTable is a class of disconnected architecture in the .NET framework. A DataSet
contains a collection of DataTable. They are tabularized representation of your data. The
DataTables are the same as tables in your data source but they are added to the DataSet.
Once the DataTable are in the DataSet, you can define the properties such as
DataRelation, primary key, foreign key and so on. You can create the DataTable
programmatically or retrieve them from a database through DataAdapter and using Fill()
method. After you fill the DataSet or DataTable, you can access these tables by using the
index number.
The collection uses zero base index number so that the first table is at the index number
zero for ex:
ds.Tables[0]; or
ds.Tables[“employee”];
▪ Property:
Property Description
Columns It returns the column collection.
DataSet It returns the parent DataSet.
DefaultView It returns a view of the table.
Rows It returns the rows collection.
Page 8
Chapter 7 ADO.NET
▪ Methods:
Methods Description
This method saves changes which made with records in a
AcceptChanges()
DataTable.
Clear() This method clears all the data from DataTable.
This method copies the structure of DataTable without any
Clone()
records.
This method copies the whole records with structure of
Copy()
DataTable.
NewRow() This method creates a new row in DataTable.
➢ DataRow:
A DataRow represent a row of data in DataTable. You can add data to the DataTable
using DataRow object. DataRow contain not only the data but also the error information
for the row.
▪ Property:
Property Description
This property takes the Boolean value indicating whether any
HasError
column in the row contains an error or not.
Item It gets or sets an item of a row.
ItemArray It set or returns all values in a row.
It indicates the current state of the row. Possible values are
RowState
added, modified, unchanged or deleted.
Table It returns the DataTable to which this row is attached.
▪ Methods:
Methods Description
AcceptChanges() It commits all the changes made to this row.
RejectChanges() It rejects all the changes made since last AcceptChanges.
This method sets the RowState to be deleted. The row is not
Delete()
removed until the AcceptChanges() method is called.
BeginEdit() It starts an edit operation on a row.
CancelEdit() It cancels the current edit on a row.
EndEdit() It ends the current edit on a row.
➢ DataColumn:
A DataColumn is the same as the column in a table. The DataColumn class is used to
store the information about each column. This class includes properties such as the Data
types, column name and data size. Each DataTable contains a collection of DataColumn
objects accessible through the column property.
Page 9
Chapter 7 ADO.NET
▪ Property:
Property Description
This property takes Boolean value to determine whether the
AllowDBNull
column will allow null values or not.
It gets or sets a value that indicates whether the column
AutoIncrement automatically increments the value of the column for new rows
added to the table.
Caption It gets or sets the caption for the column.
DataType It gets or sets the type of data stored in the column.
It gets or sets the default value for the column when you are
DefaultValue
creating new rows.
MaxLength It gets or sets the maximum length of a text column.
It gets or sets a value that indicates whether the values in each
Unique
row of the column must be unique.
▪ Methods:
Methods Description
It returns the existing column expression and converts the value
ToString()
in string type.
Dispose() It releases all the resources.
➢ DataView:
The DataView provides different views of the data stored in a DataTable. DataView can
be used to sort, filter, and search in a DataTable; additionally we can add new rows and
modify the content in a DataTable. DataViews can be created and configured both design
time and run time. Changes made to a DataView affect the underlying DataTable
automatically, and changes made to the underlying DataTable automatically affect any
DataView objects that are viewing the DataTable.
▪ Property:
Property Description
AllowDelete Sets or gets a value that indicates whether deletes are allowed.
AllowEdit Gets or sets a value that indicates whether edits are allowed.
Gets or sets a value that indicates whether the new rows can be
AllowNew
added by using the AddNew() method.
Gets or sets a value that indicates whether to use the default
ApplyDefaultSort sort. The default sort is (ascending) by all primary keys as
specified by Primary Key.
Gets the number of records in
Count the DataView after RowFilter and RowStateFilter have been
applied.
Gets or sets the expression used to filter which rows are viewed
RowFilter
in the DataView.
RowStateFilter Gets or sets the row state filter used in the DataView.
Page 10
Chapter 7 ADO.NET
Gets or sets the sort column or columns, and sort order for
Sort
the DataView.
Table Gets or sets the source DataTable.
▪ Methods:
Methods Description
AddNew() Adds a new row to the DataView.
Close() Closes the DataView.
Open() Opens a DataView.
❖ DataGridView:
The DataGridView control provides a powerful and flexible way to display data in a tabular
format. You can use the DataGridView control to show read-only views of a small amount of
data, or you can scale it to show editable views of very large sets of data.
With the DataGridView control, you can display and edit tabular data from many different
kinds of data sources. Binding data to the DataGridView control is straightforward and
intuitive, and in many cases it is as simple as setting the DataSource property. When you
bind to a data source that contains multiple lists or tables, set the DataMember property to a
string that specifies the list or table to bind to.
Page 11