Crea il tuo assistente AI con lo Stregatto (open source python framework)
Ado.net session10
1. Developing Database Applications Using ADO.NET and XML
Objectives
In this session, you will learn to:
Manage local transactions
Manage distributed transactions
Ver. 1.0 Session 10 Slide 1 of 31
2. Developing Database Applications Using ADO.NET and XML
Managing Local Transactions
A transaction can be defined as a sequence of operations
that are performed together as a single logical unit of work.
If a transaction is successful, all the data modifications
performed in the database will be committed and saved.
If a transaction fails or an error occurs, then the transaction
is rolled back to undo the data modifications done in the
database.
Ver. 1.0 Session 10 Slide 2 of 31
3. Developing Database Applications Using ADO.NET and XML
Properties of a Transaction
For a transaction to commit successfully within a database,
it should possess the following four properties:
Atomicity States that either all the modifications
are performed or none of them are
performed
Consistency States that data is in a consistent state
after a transaction is completed
successfully to maintain the integrity of
data
Isolation States that any data modification made
by one transaction must be isolated from
the modifications made by the other
transaction
Durability States that any change in data by a
completed transaction remains
permanently in effect in the database
Ver. 1.0 Session 10 Slide 3 of 31
4. Developing Database Applications Using ADO.NET and XML
Types of Transaction
ADO.NET provides support for two types of transactions:
Local transactions A local transaction performs on a single
data source. Because local transactions
are performed on a single data source,
these transactions are efficient to
operate and easy to manage.
A distributed transaction performs on
Distributed transactions multiple data sources. Distributed
transactions enable you to incorporate
several distinct transactional operations
into an atomic unit that either succeed or
fail completely.
Ver. 1.0 Session 10 Slide 4 of 31
5. Developing Database Applications Using ADO.NET and XML
Performing Local Transactions
• ADO.NET has an interface, IDbTransaction that
contains methods for creating and performing local
transactions against a single data source.
• The following table lists the transaction classes available in
the .NET Framework 2.0.
Types of Transaction Classes Description
System.Data.SqlClient.SqlTransaction Transaction class for .NET framework
data provider for SQL Server
System.Data.OleDb.OleDbTransaction Transaction class for .NET framework
data provider for OLE DB
System.Data.Odbc.OdbcTransaction Transaction class for .NET framework
data provider for ODBC
System.Data.OracleClient.OracleTransaction Transaction class for .NET framework
data provider for Oracle
Ver. 1.0 Session 10 Slide 5 of 31
6. Developing Database Applications Using ADO.NET and XML
Performing Local Transactions (Contd.)
Let us understand how to create a local transaction.
string connectString = Creating a connection to the
data source
"Initial Catalog=AdventureWorks;
Data Source=SQLSERVER01;
User id=sa;Password=niit#1234";
SqlConnection cn = new SqlConnection();
cn.Open();
cn = connectString;
SqlTransaction tran = null;
Ver. 1.0 Session 10 Slide 6 of 31
7. Developing Database Applications Using ADO.NET and XML
Performing Local Transactions (Contd.)
try
{
tran = cn.BeginTransaction(); Calling the BeginTransaction()
SqlCommand cmd = new method
Creating the command object and
SqlCommand("INSERT INTO
passing the SQL command
empdetails(ccode,cname,
caddress,cstate,ccountry,
cDesignation,cDepartment)
VALUES(1101,'Linda Taylor',
'Oxfordshire','London',
'UK','Manager','Finance')", cn, tran);
Ver. 1.0 Session 10 Slide 7 of 31
8. Developing Database Applications Using ADO.NET and XML
Performing Local Transactions (Contd.)
cmd.ExecuteNonQuery();
Committing the transaction if
the command succeeds
tran.Commit();
Console.WriteLine("Transaction
Committedn)";
}
catch (SqlException ex)
{ Rolling back the transaction if the
tran.Rollback(); command fails
Console.WriteLine("Error
- TRANSACTION ROLLED BACKn"
+ ex.Message);
}
Ver. 1.0 Session 10 Slide 8 of 31
9. Developing Database Applications Using ADO.NET and XML
Performing Local Transactions (Contd.)
catch (System.Exception ex)
{
Console.WriteLine("System Errorn" +
ex.Message);
}
finally
{
cn.Close();
}
Console.ReadLine();
}
}
}
Ver. 1.0 Session 10 Slide 9 of 31
10. Developing Database Applications Using ADO.NET and XML
Just a minute
Which method is called when a transaction succeeds in its
operation?
1. Rollback()
2. Commit()
3. BeginTransaction()
4. ExecuteNonQuery()
Answer:
2. Commit()
Ver. 1.0 Session 10 Slide 10 of 31
11. Developing Database Applications Using ADO.NET and XML
Demo: Managing Local Transactions
Problem Statement:
John Howard, the HR executive has joined as a head of the
Finance department at Texas. Therefore, his details need to be
added to the HR database. The Department code for the
Finance department is D002. The user name that needs to be
assigned to him is JohnH and the password is howard.
As a part of the development team, you need to create a
transaction that will allow you to add these details in the
HRusers and Department tables.
Ver. 1.0 Session 10 Slide 11 of 31
12. Developing Database Applications Using ADO.NET and XML
Managing Distributed Transactions
• Distributed transactions are performed on multiple data
sources or multiple connections within a data source.
• Distributed transactions are created in the
System.Transaction namespace.
• The System.Transaction namespace has a
TransactionScope class, which enables a developer to
create and manage distributed transactions.
• To create a distributed transaction, a TransactionScope
object is created in a using block.
• The TransactionScope object decides whether to create
a local transaction or a distributed transaction. This is
known as transaction promotion.
Ver. 1.0 Session 10 Slide 12 of 31
13. Developing Database Applications Using ADO.NET and XML
Managing Distributed Transactions (Contd.)
Let us understand how to create a distributed transaction.
using (TransactionScope ts Creating the TransactionScope
= new TransactionScope()) object
{
using(SqlConnection cn Creating a connection to the data
= new SqlConnection("Initial source
Catalog=HR;Data
Source=SQLSERVER01;User
id=sa;Password=niit#1234"))
{
cn.Open();
Ver. 1.0 Session 10 Slide 13 of 31
14. Developing Database Applications Using ADO.NET and XML
Managing Distributed Transactions (Contd.)
using(SqlCommand cmd = new Creating a SqlCommand object
to insert a record in the
SqlCommand("INSERT INTO
HRusers table
HRusers(cUserName,cPassword)
VALUES('Darren','Cooper')", cn))
{
int rowsUpdated =
cmd.ExecuteNonQuery();
if (rowsUpdated > 0)
{
Ver. 1.0 Session 10 Slide 14 of 31
15. Developing Database Applications Using ADO.NET and XML
Managing Distributed Transactions (Contd.)
SqlConnection cn1 = new Creating another connection to the
SqlConnection("Initial same data source
Catalog=HR;Data
Source=SQLSERVER01;User
id=sa;Password=niit#1234"))
{
cn1.Open();
using (SqlCommand cmd1 = Creating another SqlCommand
object to delete a record in the
new SqlCommand("DELETE
Department table
Department WHERE
cDepartmentCode=1111", cn1))
{
int rowsUpdated1 =
cmd1.ExecuteNonQuery();
Ver. 1.0 Session 10 Slide 15 of 31
16. Developing Database Applications Using ADO.NET and XML
Managing Distributed Transactions (Contd.)
if (rowsUpdated1 > 0)
{
ts.Complete(); Calling the Complete() method
to commit the transactions
Console.WriteLine
("Transaction Committedn");
cn1.Close();
}
}}}
cn.Close();
}}
Console.ReadLine();
}}}
Ver. 1.0 Session 10 Slide 16 of 31
17. Developing Database Applications Using ADO.NET and XML
Just a minute
The _______ method is invoked to commit the distributed
transaction.
1. Commit()
2. BeginTransaction()
3. Complete()
4. Rollback()
Answer:
3. Complete()
Ver. 1.0 Session 10 Slide 17 of 31
18. Developing Database Applications Using ADO.NET and XML
Performing Bulk Copy Operations in a Transaction
• Bulk copy operations can be performed as an isolated
operation or as a part of a transaction.
• By default, a bulk copy operation is its own transaction.
• To perform a bulk copy operation, you need to create a new
instance of BulkCopy class with a connection string.
• The bulk copy operation creates, and then, commits or rolls
back the transaction.
Ver. 1.0 Session 10 Slide 18 of 31
19. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction
An isolation level determines the effect a transaction has on
other transactions that are currently running, and vice versa.
By default, all transactions are completely isolated and run
concurrently without impacting each other.
The isolation level of a transaction specifies the locking
strategy used by the connection running the transaction to
prevent concurrency problems when multiple transactions
access the same data.
Ver. 1.0 Session 10 Slide 19 of 31
20. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
The following table describes the concurrency errors that
can occur if multiple transactions access the same data at
the same time.
Concurrency Error Description
Dirty read A transaction reads the data that has not been
committed by the other transaction. This can
create problem if a transaction that has added
the data is rolled back.
Nonrepeatable read A transaction reads the same row more than
once and a different transaction modifies the row
between the reads.
Phantom read A transaction reads a rowset more than once
and a different transaction inserts or deletes
rows between the first transaction’s reads.
Ver. 1.0 Session 10 Slide 20 of 31
21. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
The various types of isolation levels of a transaction are:
Read Uncommitted
Read Committed with Locks
Read Committed with Snapshots
Repeatable Read
Snapshot
Serializable
Ver. 1.0 Session 10 Slide 21 of 31
22. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
The following table describes the various isolation levels
and the corresponding concurrency errors for a transaction.
Isolation Level Dirty Read Nonrepeatable Read Phantom Read
Read Uncommitted Yes Yes Yes
Read Committed No Yes Yes
with Locks
Read Committed No Yes Yes
with Snapshots
Repeatable Read No No Yes
Snapshot No No No
Serializable No No No
Ver. 1.0 Session 10 Slide 22 of 31
23. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
Let us understand how to set the isolation level of a
transaction to Read Committed.
TransactionOptions options = new Initializing the
TransactionOptions
TransactionOptions(); object
options.IsolationLevel = Setting the Isolation level of
System.Transactions.IsolationLevel. both transactions to Read
Committed
ReadC ommitted;
using(TransactionScope ts = new
TransactionScope
(TransactionScopeOption.
Required, options)){
using (SqlConnection cn = new Creating a connection to
a data source
SqlConnection("Initial Catalog=
HR;Data Source=SQLSERVER01;
User id=sa;Password=niit#1234;")){
cn.Open();
Ver. 1.0 Session 10 Slide 23 of 31
24. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
using(SqlCommand cmd = new Creating a SqlCommand
object to insert a record in the
SqlCommand("INSERT INTO Department table
Department(cDepartmentCode
,vDepartmentName,vDepartmentHead,
vLocation) VALUES(2013,'IT','Lara
King','Houston')", cn)){
int rowsUpdated =
cmd.ExecuteNonQuery();
if (rowsUpdated > 0)
{
Ver. 1.0 Session 10 Slide 24 of 31
25. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
using (SqlConnection cn1 = Creating another connection to
the same data source
new SqlConnection("Initial
Catalog=HR;Data
Source=SQLSERVER01;User
id=sa;Password=niit#1234"))
{
cn1.Open();
using(SqlCommand cmd = new Creating another command
SqlCommand("INSERT INTO object to insert a record in the
HRusers table
HRusers(cUserName,cPassword
) VALUES('Hansel','Lord')",
cn1))
Ver. 1.0 Session 10 Slide 25 of 31
26. Developing Database Applications Using ADO.NET and XML
Specifying Isolation Levels of a Transaction (Contd.)
{
int rowsUpdated1 =
cmd1.ExecuteNonQuery();
if (rowsUpdated1 > 0)
{
ts.Complete(); Calling the Complete() method
to commit both transactions
}}}}
Ver. 1.0 Session 10 Slide 26 of 31
27. Developing Database Applications Using ADO.NET and XML
Just a minute
Which isolation level supports the occurrence of Dirty read,
Nonrepeatable read, and Phantom read?
1. Read Committed with Locks
2. Serializable
3. Read Committed with Snapshots
4. Read Uncommitted
Answer:
4. Read Uncommitted
Ver. 1.0 Session 10 Slide 27 of 31
28. Developing Database Applications Using ADO.NET and XML
Demo: Managing Distributed Transactions
Problem Statement:
Pamela Cruz has joined as an HR executive in Tebisco.
Therefore, her details that includes user name and password
needs to be inserted in the HRusers table. In addition, the
details about her position needs to be added in the Position
table. Both the operations need to be performed
simultaneously in both the tables. As a part of the development
team, you need to add the required details for Pamela in the
HR database.
Note: To enable execution of the distributed transactions, you
need to ensure that the MSDTC services are running on your
system.
Ver. 1.0 Session 10 Slide 28 of 31
29. Developing Database Applications Using ADO.NET and XML
Summary
In this session, you learned that:
A transaction is a logical unit of work that must be completed to
maintain the consistency and integrity of a database.
A transaction has the following properties:
Atomicity
Consistency
Isolation
Durability
The two types of transaction are:
Local transactions
Distributed transactions
Ver. 1.0 Session 10 Slide 29 of 31
30. Developing Database Applications Using ADO.NET and XML
Summary (Contd.)
A local transaction performs on a single data source. The
IDbTransaction interface contains methods for creating and
performing local transactions against a data source.
A distributed transaction performs on multiple data sources. A
distributed transaction enables you to incorporate several
distinct transactional operations into an atomic unit that either
succeed or fail completely.
Bulk copy operations can be performed as an isolated
operations or as a part of a transaction. By default, a bulk copy
operation is its own transaction.
An isolation level determines the effect a transaction has on
other transactions that are currently running, and vice versa.
Ver. 1.0 Session 10 Slide 30 of 31
31. Developing Database Applications Using ADO.NET and XML
Summary (Contd.)
The various concurrency errors that can occur when multiple
transactions access the same data at the same time are:
Dirty read
Nonrepeatable read
Phantom read
The various types of isolation levels are for a transaction are:
Read Uncommitted
Read Committed with Locks
Read Committed with Snapshots
Repeatable Read
Snapshot
Serializable
Ver. 1.0 Session 10 Slide 31 of 31
Notes de l'éditeur
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
Introduce the students to the different types of threats that systems face by: Asking the students to give examples of what they think are environmental and human threats. Asking the students to give instances of what they think are malicious and non-malicious threats. Conclude the discussion on the different types of threats by giving additional examples of malicious and non malicious threats.
In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
While explaining the definition of system forensics, ask the students to note the following key words in the definition: Identify Extract Process Analyze Digital and hardware evidence Tell the students that these form an integral aspect of system forensics and would be discussed in detail. Before moving on to the next slide, hold a brief discussion on why is it important for organizations to take the help of system forensics. The discussion should be focused on: The role that system forensics plays in organizations having an IT set up. This discussion will serve as a precursor to the next slide.