You are on page 1of 28

Developing Data-Centric Applications Using ADO.

NET
Objectives

In this session, you will learn to:


Identify the connected and disconnected environment in ADO.NET Working in a connected environment

Ver. 1.0

Session 4

Slide 1 of 28

Developing Data-Centric Applications Using ADO.NET


Introducing Connected and Disconnected Environment

A connected environment is one in which a user or an application is constantly connected to a data source. A connected environment provides the following advantages:
Data concurrency issues are easier to control. Data is current and updated.

A connected environment has the following disadvantages:


A constant network connection may, sometimes, lead to network traffic logging. Scalability and performance issues in applications.

Let us now discuss the disconnected environment.

Ver. 1.0

Session 4

Slide 2 of 28

Developing Data-Centric Applications Using ADO.NET


Introducing Connected and Disconnected Environment (Contd.)

In a disconnected environment is one in which a user or an application is not directly connected to a data source. A disconnected environment provides the following advantages:
Allows multiple applications to simultaneously interact with the data source. Improves the scalability and performance of applications.

A disconnected environment has the following disadvantages:


Data is not always up to date as no proper connection is established with the data source. Data concurrency issues can occur when multiple users are updating the data to the data source.

Ver. 1.0

Session 4

Slide 3 of 28

Developing Data-Centric Applications Using ADO.NET


Introducing Connected and Disconnected Environment (Contd.)

The following figure shows the connected and disconnected environment in ADO.NET.

XML

Data Source

Ver. 1.0

Session 4

Slide 4 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Command Objects

A Command object is a specific command that is used to manipulate data in a data source in a connected environment. A Command object represents a DML statement or a stored procedure that is used to retrieve, insert, delete, or modify data in a data source. The two types of operations performed by a command object to retrieve and modify data in a data source are:
Synchronous operations Asynchronous operations

Let us discuss these operations in detail.

Ver. 1.0

Session 4

Slide 5 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Command Objects (Contd.)

During synchronous operations, the command objects are linked to each other. Executing command objects synchronously results in a sequential execution, where each database command must complete before the next command is executed. Synchronous operations are performed with the help of the following command objects:
DbCommand object DbParameters object DbDataReader object
Executes a command in a data source against a valid open connection Assigns parameterized values to stored procedures Retrieves a forward-only, read-only data from the data source

Ver. 1.0

Session 4

Slide 6 of 28

Developing Data-Centric Applications Using ADO.NET


Just a minute Which method of the DbCommand object executes a DML statement against a connection and returns the number of rows affected?
1. 2. 3. 4. ExecuteReader() CreateObjRef() ExecureNonQuery() ExecuteScalar()

Answer:
3. ExecuteNonQuery()

Ver. 1.0

Session 4

Slide 7 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Command Objects (Contd.)

Asynchronous execution of commands enhances the overall performance and responsiveness of the client application. ADO.NET supports asynchronous execution of commands to enable parallelism by allowing the client application to execute commands while waiting for the response from a previously issued command. The asynchronous execution of commands is carried out in the SQLCommand class.

Ver. 1.0

Session 4

Slide 8 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Command Objects (Contd.) The SQLCommand class provides the following methods for asynchronous execution of commands:
BeginExecuteNonQuery()
Starts the process of asynchronously executing a Transact-SQL statement or stored procedure that does not return rows Starts the process of asynchronously executing a Transact-SQL statement or stored procedure that returns rows Initiates the asynchronous execution of the Transact-SQL statement or stored procedure that is described by the SqlCommand and returns the result as an XMLReader object

BeginExecuteReader()

BeginExecuteXmlReader()

Ver. 1.0

Session 4

Slide 9 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Command Objects (Contd.)
EndExecuteNonQuery()

Completes the asynchronous execution of a Transact-SQL statement or stored procedure Completes the asynchronous execution of a Transact-SQL statement or a stored procedure, thereby, returning the requested SqlDataReader Completes the asynchronous execution of a Transact-SQL statement or a stored procedure, thereby, returning XML data

EndExecuteReader()

EndExecuteXmlReader()

Ver. 1.0

Session 4

Slide 10 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Command Objects (Contd.)

Another way of retrieving data asynchronously is by using Multiple Active Result Sets (MARS). MARS allows the execution of multiple data readers on a single connection. By default, MARS feature is disabled in an application. You can enable MARS in the connection string, as shown in the following code snippet:
String connectionString = "Data Source=SQLSERVER01;Initial Catalog=AdventureWorks;User id=sa;Password=niit#1234; MultipleActiveResultSets=True";

Enabling MARS property to True

Ver. 1.0

Session 4

Slide 11 of 28

Developing Data-Centric Applications Using ADO.NET


Just a minute What is the function of the EndExecuteReader() method of the SqlCommand class?
1. Asynchronously executes a stored procedure that returns rows 2. Executes the stored procedure and returns the result as an XmlReader object
3. Asynchronously executes a stored procedure that does not return rows 4. Executes the stored procedure and returns the result as an SqlDataReader object

Answer:
4. Executes the stored procedure and returns the result as an SqlDataReader object

Ver. 1.0

Session 4

Slide 12 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters

A data adapter is integral to the working of ADO.NET because data is transferred to and from a database through a data adapter. A data adapter retrieves data from a database into a dataset. The data adapter first compares the data in the dataset with that in the database and then updates the database. Data from a database can be accessed by configuring a data adapter.

Ver. 1.0

Session 4

Slide 13 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

Following are the data adapters that can be configured to connect to a database:
SqlDataAdapter OleDbDataAdapter
Accesses data specifically from Microsoft SQL Server Accesses data from a database that is supported by an OLE DB data provider Accesses data from a database that is supported by an ODBC data provider

OdbcDataAdapter

OracleDataAdapter Accesses data from a database that is


supported by an Oracle data provider

Ver. 1.0

Session 4

Slide 14 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

The following properties and methods of a data adapter can be used to perform various operations on a database:
SelectCommand InsertCommand
Refers to a DML statement or a stored procedure to retrieve data from a database Refers to a data command to insert data into a database Refers to a data command to update a database

UpdateCommand
DeleteCommand

Refers to a data command to delete data from a database

Fill()
Update()

Fills the dataset with the records from a database


Executes the corresponding Insert, Update, or Delete commands for each inserted, modified, or deleted row to reflect the changes in a database
Session 4 Slide 15 of 28

Ver. 1.0

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

Consider the following code snippet of creating a DataAdapter object and using the SelectCommand property of the object:
SqlConnection cn = new SqlConnection(); Set a connection string cn.ConnectionString = "Data source=SQLSERVER01;Initial catalog=HR;User id=sa; Password=niit#1234"; DataSet DataSet1 = new DataSet(); Creating a DataSet object SqlDataAdapter da = new Creating a SqlDataAdapter SqlDataAdapter(); object SqlCommand cmd=new SqlCommand Passing the SQL query to the ("Select * from Employees", cn); command object da.SelectCommand = cmd; Retrieving records from the Filling the dataset Employees table with records da.Fill(DataSet1); from the Employees table
Ver. 1.0

Session 4

Slide 16 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

A data adapter handles data transfer between the database and the dataset through its properties and methods, and displays data through the process of table mapping. After a dataset has been created, the data adapter uses the process of table mapping to map the columns in the database table with the dataset columns. A data adapter uses the TableMappings property, a collection of DatatableMapping objects that is used for mapping between the database table and the DataTable object in the dataset.

Ver. 1.0

Session 4

Slide 17 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

A major challenge related to data access is that more than one user might need to simultaneously access data in a database. Another challenge is more than one user might need to access the data anytime, anywhere. This challenge can be overcome by implementing database locking while a transaction is executing. However, if database locking is not implemented, it can lead to data concurrency conflicts that arise from multiple updates being performed on the database.

Ver. 1.0

Session 4

Slide 18 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

Resolving data concurrency conflicts is a business decision, with the following choices:
Prioritized on time; first update wins Prioritized on time; last update wins Prioritized on role Prioritized on location User resolves the conflict

Ver. 1.0

Session 4

Slide 19 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

A significant way to increase the performance of data updates is to update and send the changes to the database in batches. This is known as batch updates. Batch updates are performed by using the UpdateBatchSize property of the SqlDataAdapter object. By default, the UpdateBatchSize property is set to 1. One way to confirm that the changes are being sent to the database server in batches is to add a RowUpdated event to the SqlDataAdapter object. This event will show the number of rows affected in the last batch.

Ver. 1.0

Session 4

Slide 20 of 28

Developing Data-Centric Applications Using ADO.NET


Working with Data Adapters (Contd.)

ADO.NET provides support for classes that can create any provider-specific objects. These classes are known as the DbProviderFactories classes. The DbProviderFactories class contains a method called GetFactoryClasses that returns a data table, which is populated with data from various providers, as shown in the following figure.

Ver. 1.0

Session 4

Slide 21 of 28

Developing Data-Centric Applications Using ADO.NET


Just a minute

_______ class can create any data provider-specific object.


1. 2. 3. 4. DbCommand DbProviderFactories DbParameter DbConnection

Answer:
2. DbProviderFactories

Ver. 1.0

Session 4

Slide 22 of 28

Developing Data-Centric Applications Using ADO.NET


Demo: Manipulating Data in a Connected Environment

Problem Statement:
The HR Manager of Tebisco needs to update the number of vacancies, for those positions where the number of vacancies is not more than 10. This updation will be based on the current position code of employees. As a part of the development team, you need to execute two queries, one to retrieve the number of vacancies per position code, and the other to update the number of vacancies on the basis of the current position code of employees. You need to develop an application that can execute both queries on a single database connection. Hint: You need to access the InternalJobPosting and Employee tables of the HR database.

Ver. 1.0

Session 4

Slide 23 of 28

Developing Data-Centric Applications Using ADO.NET


Summary

In this session, you learned that:


A connected environment is one in which a user or an application is constantly connected to a data source. A disconnected environment is one in which a user or an application is not directly connected to a data source. The two types of operations performed by a command object to retrieve and modify data in a data source are:
Synchronous operations Asynchronous operations

During synchronous operations, the command objects are linked to each other. Synchronous operations are performed with the help of the following command objects:
DbCommand object DbParameters object DbDataReader object
Ver. 1.0

Session 4

Slide 24 of 28

Developing Data-Centric Applications Using ADO.NET


Summary (Contd.)

ADO.NET supports asynchronous execution of commands to enable parallelism by allowing the client application to execute commands while waiting for the response from a previously issued command. The SqlCommand class provides the following methods for asynchronous operations:
BeginExecuteNonQuery() BeginExecuteReader() BeginExecuteXmlReader() EndExecuteNonQuery() EndExecuteReader() EndExecuteXmlReader()

MARS allows the execution of multiple data readers on a single connection.

Ver. 1.0

Session 4

Slide 25 of 28

Developing Data-Centric Applications Using ADO.NET


Summary (Contd.)

A data adapter, which is a part of the connected environment, retrieves data from a database into a dataset. The data adapters that can be configured to connect to a database in Visual Studio .NET are:
SqlDataAdapter OleDbDataAdapter OdbcDataAdapter OracleDataAdapter

Ver. 1.0

Session 4

Slide 26 of 28

Developing Data-Centric Applications Using ADO.NET


Summary (Contd.)

The following properties and methods of a data adapter can be used to perform the various operations on a database:
SelectCommand InsertCommand UpdateCommand DeleteCommand Fill() Update()

A data adapter handles data transfer between the database and the dataset through its properties and methods, and displays the data through the process of table mapping.

Ver. 1.0

Session 4

Slide 27 of 28

Developing Data-Centric Applications Using ADO.NET


Summary (Contd.)

Resolving data concurrency conflicts is a business decision, with the following choices:
Prioritized on time; first update wins Prioritized on time; last update wins Prioritized on role Prioritized on location User resolves the conflict

A significant way to increase the performance of data updates is to update and send the changes to the database in batches. This is known as batch updates. ADO.NET provides support for classes that can create any provider-specific objects, such as SqlClient, Odbc, OracleClient, and OleDb. These classes are known as DbProviderFactories classes.

Ver. 1.0

Session 4

Slide 28 of 28

You might also like