Upload
jai-deep
View
217
Download
0
Embed Size (px)
Citation preview
7/30/2019 09 Ddcauado.net Cg Session04
1/20
Instructor InputsSess
ion4
7/30/2019 09 Ddcauado.net Cg Session04
2/20
7/30/2019 09 Ddcauado.net Cg Session04
3/20
NIIT Instructor Inputs 4.3
This session covers Chapter 3.
To understand the concepts covered in the session, the students should be aware of the
following concepts/skills:
Implementing data binding to display values on the controls of a Windows form Filtering data to display the selected records Understanding the ADO.NET architecture Managing connections in ADO.NET Working knowledge of SQL Server 2005 Understanding the application of command objects Working knowledge of the ADO.NET connected environment
This session discusses the connected and disconnected environment in ADO.NET. In
addition, it discusses the command objects used to access data from a data source in a
connected ADO.NET environment. To enable students to better understand the concepts,encourage them to provide their own examples, wherever required. Use the animated
slides, wherever provided, to conduct the session effectively.
Session Overview
Prerequisite Knowledge for the Session
Tips for Handling the Session
7/30/2019 09 Ddcauado.net Cg Session04
4/20
4.4 Instructor Inputs NIIT
Slide 1
Slide 1 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
In this session, you will learn to:
Identify the connected and disconnected environment in
ADO.NET
Working in a connected environment
Objectives
Start the session by sharing the session objectives with the students.
Slide 2
Slide 2 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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.
Introducing Connected and Disconnected Environment
Initiate a discussion in the class by asking students what happens when multiple users
simultaneously access a data-centric application to retrieve and update data. Lead the
discussion towards explaining a connected environment. In addition, state its advantagesand disadvantages.
7/30/2019 09 Ddcauado.net Cg Session04
5/20
NIIT Instructor Inputs 4.5
Slide 3
Slide 3 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
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.
Introducing Connected and Disconnected Environment (Contd.)
Move the discussion towards a disconnected environment. Ask the students whether it is
possible for users to perform modifications on the data without being directly connected
to a data source.
Lead the discussion towards explaining disconnected environment. In addition, state its
advantages and disadvantages, with the help of an example given in the SG.
Slide 4
Slide 4 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
The following figure shows the connected and disconnected
environment in ADO.NET.
Introducing Connected and Disconnected Environment (Contd.)
XML Data Source
7/30/2019 09 Ddcauado.net Cg Session04
6/20
4.6 Instructor Inputs NIIT
Use the animated slide to explain the ADO.NET disconnected and connected classes.
Slide 5
Slide 5 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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.
Working with Command Objects
Inform the students that in this session, the focus will be on learning about the connected
environment. Explain to the students that in a connected environment, since users have tobe continuously connected to a data source to retrieve the data, various command objects
are used that provides data to the application in a read-only and forward-only mode.
Explain command objects, and list the two types of operations performed by a command
object to retrieve and modify data in a data source, synchronous and asynchronousoperations.
7/30/2019 09 Ddcauado.net Cg Session04
7/20
NIIT Instructor Inputs 4.7
Slide 6
Slide 6 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
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
Working with Command Objects (Contd.)
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
Explain how command objects are executed synchronously with the help of an examplegiven in the SG. Use this animated slide to explain the various command objects that are
used to perform synchronous operations.
Discuss the code given in the SG. Explain that the ExecuteNonQuery() method returns
-1 when a SELECT statement is executed. On the other hand, if an INSERT, DELETE, or
UPDATE statement is executed, this method returns the number of rows affected.
7/30/2019 09 Ddcauado.net Cg Session04
8/20
4.8 Instructor Inputs NIIT
Slide 7
Slide 7 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
Which method of the DbCommand object executes a DML
statement against a connection and returns the number of
rows affected?
1. ExecuteReader()
2. CreateObjRef()
3. ExecureNonQuery()
4. ExecuteScalar()
Just a minute
Answer:
3. ExecuteNonQuery()
The objective of this JAM is to revise the concepts related to command objects covered inthe preceding slides.
Slide 8
Slide 8 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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 inthe SQLCommand class.
Working with Command Objects (Contd.)
Move the discussion to explain how command objects are executed asynchronously.
7/30/2019 09 Ddcauado.net Cg Session04
9/20
NIIT Instructor Inputs 4.9
Slide 9
Slide 9 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
The SQLCommand class provides the following methods for
asynchronous execution of commands:
BeginExecuteNonQuery()
BeginExecuteReader()
BeginExecuteXmlReader()
Working with Command Objects (Contd.)
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 theSqlCommand and returns the result as
an XMLReader object
Use this slide and the next slide to explain the various methods, provided in the
SqlCommand class, to execute command objects asynchronously.
Slide 10
Slide 10 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
EndExecuteNonQuery()
EndExecuteReader()
EndExecuteXmlReader()
Working with Command Objects (Contd.)
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 therequested SqlDataReader
Completes the asynchronous execution
of a Transact-SQL statement or a stored
procedure, thereby, returning XML data
7/30/2019 09 Ddcauado.net Cg Session04
10/20
4.10 Instructor Inputs NIIT
Slide 11
Slide 11 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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";
Working with Command Objects (Contd.)
Enabling MARS property
to True
Link this topic with asynchronous operations by telling the students that another way ofretrieving data asynchronously is by using Multiple Active Result Sets (MARS).
Explain MARS with the help of examples provided in the SG. Describe to the students
how MARS is enabled in a Windows application.
Slide 12
Slide 12 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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 anXmlReader object
3. Asynchronously executes a stored procedure that does not
return rows
4. Executes the stored procedure and returns the result as anSqlDataReader object
Just a minute
Answer:
4. Executes the stored procedure and returns the result as anSqlDataReader object
7/30/2019 09 Ddcauado.net Cg Session04
11/20
NIIT Instructor Inputs 4.11
The objective of this JAM is to revise the concepts related to asynchronous operationscovered in the preceding slides.
Slide 13
Slide 13 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
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.
Working with Data Adapters
Before starting with this session, conduct a brief recap by asking students the following
questions:
What is a dataset?
What are dataviews?
Initiate a discussion in the class on data adapters. Explain data adapters and their role in a
connected environment.
7/30/2019 09 Ddcauado.net Cg Session04
12/20
4.12 Instructor Inputs NIIT
Slide 14
Slide 14 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
Following are the data adapters that can be configured to
connect to a database:
SqlDataAdapter
OleDbDataAdapter
OdbcDataAdapter
OracleDataAdapter
Working with Data Adapters (Contd.)
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
Accesses data from a database that is
supported by an Oracle data provider
Use the animated slide to list the various dataadpaters that can be configured to connect toa database in Visual Studio .NET.
Slide 15
Slide 15 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
The following properties and methods of a data adapter can
be used to perform various operations on a database:
SelectCommand
InsertCommand
UpdateCommand
DeleteCommand
Fill()
Update()
Working with Data Adapters (Contd.)
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
Refers to a data command to delete data from a
database
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
Use the animated slide to explain the properties and methods of a data adapter for
performing various operations on a database.
7/30/2019 09 Ddcauado.net Cg Session04
13/20
NIIT Instructor Inputs 4.13
Slide 16
Slide 16 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
Consider the following code snippet of creating aDataAdapter object and using the SelectCommand
property of the object:
SqlConnection cn = new
SqlConnection();
cn.ConnectionString = "Data
source=SQLSERVER01;Initial
catalog=HR;User id=sa;
Password=niit#1234";
DataSet DataSet1 = new DataSet();
SqlDataAdapter da = new
SqlDataAdapter();
SqlCommand cmd=new SqlCommand
("Select * from Employees", cn);
da.SelectCommand = cmd;
da.Fill(DataSet1);
Working with Data Adapters (Contd.)
Set a connection string
Creating a DataSet object
Creating a SqlDataAdapter
object
Passing the SQL query to thecommand object
Retrieving records from theEmployees tableFilling the dataset with records
from the Employees table
Use the animated slide to explain a code snippet of creating and using a data adapter
object. You can also use the code snippet given in the SG for updating and saving the data
modifications in a datatable.
Slide 17
Slide 17 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
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 ofDatatableMapping objects that is used for
mapping between the database table and the DataTable
object in the dataset.
Working with Data Adapters (Contd.)
Lead the discussion towards the TableMapping property of data adapters. Explain the
process of table mapping, which is the process of data transfer between the database and
the dataset through its properties and methods.
7/30/2019 09 Ddcauado.net Cg Session04
14/20
4.14 Instructor Inputs NIIT
Slide 18
Slide 18 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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.
Working with Data Adapters (Contd.)
Inform the students that after making the changes in the datatable through a data adapter,
you need to ensure that the changes are committed and updated in the datatable.
Otherwise, it leads to data concurrency conflicts. Initiate a discussion in the class on how
persisting changes to a data source can create several challenges. Explain these challenges
with the help of the example given in the SG.
Slide 19
Slide 19 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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
Working with Data Adapters (Contd.)
Lead the discussion to explain how data conflicts in a data source can be resolved.
7/30/2019 09 Ddcauado.net Cg Session04
15/20
NIIT Instructor Inputs 4.15
Slide 20
Slide 20 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
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 theUpdateBatchSize 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 thedatabase 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.
Working with Data Adapters (Contd.)
Inform the students that a significant way to increase the performance of data updates is to
update and send the changes to the database in batches. Explain batch updates with the
help of the code given in the SG.
Slide 21
Slide 21 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
ADO.NET provides support for classes that can create any
provider-specific objects. These classes are known as theDbProviderFactories 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.
Working with Data Adapters (Contd.)
Move the discussion towards factory classes. Tell the students that this is a new feature of
ADO.NET. Explain the use of factory classes with the help of the example given in the
SG.
7/30/2019 09 Ddcauado.net Cg Session04
16/20
4.16 Instructor Inputs NIIT
Slide 22
Slide 22 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
_______ class can create any data provider-specific object.
1. DbCommand
2. DbProviderFactories
3. DbParameter
4. DbConnection
Just a minute
Answer:
2. DbProviderFactories
The objective of this JAM is to revise the concepts related to data adapters covered in the
preceding slides.
Slide 23
Slide 23 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
Problem Statement:
The HR Manager of Tebisco needs to update the number of
vacancies, for those positions where the number of vacanciesis 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 thebasis of the current position code of employees.
You need to develop an application that can execute bothqueries on a single database connection.
Hint: You need to access the InternalJobPosting and
Employee tables of the HR database.
Demo: Manipulating Data in a Connected Environment
The objective of this activity is to see the advantage of MARS.
Use this slide to conduct the activity.
7/30/2019 09 Ddcauado.net Cg Session04
17/20
NIIT Instructor Inputs 4.17
You can access the solution of this activity from the following path in the TIRM CD:
Datafiles_For_Faculty\ADO.NET\Chapter 01\Activity1.
Slide 24
Slide 24 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
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 arelinked to each other.
Synchronous operations are performed with the help of the
following command objects:
DbCommand object
DbParameters object
DbDataReader object
Summary
Use this slide and the following slides to summarize the key points of the session.
Slide 25
Slide 25 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session4
ADO.NET supports asynchronous execution of commands to
enable parallelism by allowing the client application to executecommands 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.
Summary (Contd.)
7/30/2019 09 Ddcauado.net Cg Session04
18/20
4.18 Instructor Inputs NIIT
Slide 26
Slide 26 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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
Summary (Contd.)
Slide 27
Slide 27 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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.
Summary (Contd.)
7/30/2019 09 Ddcauado.net Cg Session04
19/20
NIIT Instructor Inputs 4.19
Slide 28
Slide 28 of 28Ver. 1.0
Developing Data-Centric Applications Using ADO.NET
Session 4
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 updatesis 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 asDbProviderFactories classes.
Summary (Contd.)
The following questions can be asked by the student in this session:
1. What is the advantage ofStringBuilder overString?Ans. While assigning a value to a string object, a new object is created with every
assignment. There is a slight overhead when this is done repeatedly.
The StringBuilder class provides an efficient way to repeatedly add bits on to a string.
You provide a string with an initial capacity and can add more bits of strings to it without
the overhead of creating a new object.
The performance difference will be too small to measure if you are only doing
a few concatenations. However, if you are performing operations in a loop, such asbuilding the HTML code for a web page, then StringBuilder has a definite advantage.
2. What rule is followed while deciding the settings of the batch size whenusing SqlBulkCopy?
Ans. Increasing the batch size can improve performance of the application. However,
constructing the batch on the client and handling the results on the server also creates
overheads. Therefore, you should not set the batch size to zero (one batch).
FAQs
7/30/2019 09 Ddcauado.net Cg Session04
20/20
Usually batch size between 100 and 1000 rows can generate good performance result.However, you have to perform tests to derive the optimal batch size for a scenario.
3. What is DDL?Ans. Data Definition Language (DDL) is a language used by a database management
system that allows the users to define the database, specify data types, structures, andconstraints on the data. For example, CREATE TABLE, CREATE INDEX, ALTER, andDROP are DDL statements.
4. When is the InvalidOperationException raised?Ans. This exception is thrown when a method call is invalid for the object's current state.
5. What is the advantage of asynchronous operations?Ans. Some database operations, such as command executions, can take significant time tocomplete. In such a case, single-threaded applications must block other operations and
wait for the command to finish before they can continue their own operations. In contrast,
being able to assign the long-running operation to a background thread allows the
foreground thread to remain active throughout the operation. In a Windows application,
for example, delegating the long-running operation to a background thread allows the user
interface thread to remain responsive while the operation is executing.