15 C Data
15 C Data
• ADO.NET overview
• ADO.NET architecture
• CRUD in ADO.NET
• Data providers
• Transactions
• Disconnected ADO.NET
• Data Adapter
CONFIDENTIAL 2
ADO.NET
CONFIDENTIAL 3
ADO.NET Overview
CONFIDENTIAL 4
ADO.NET Components
Connection
Command
DataReader
DataSet
DataTable
DataView
DataAdapter
etc.
CONFIDENTIAL 5
ADO.NET Data Providers
OLE DB ODBC
provider provider
CONFIDENTIAL 6
Core Objects of .NET Framework Data Providers
Object Description
DataAdapter Populates a DataSet and resolves updates with the data source.
CONFIDENTIAL 7
Auxiliary Objects of .NET Framework Data Providers
Object Description
A helper object that provides a simple way to create and manage the
ConnectionStringBuilder
contents of connection strings
CONFIDENTIAL 8
Auxiliary Objects of .NET Framework Data Providers
Object Description
Parameter Defines input, output, and return value parameters for commands and stored
procedures
Exception (DbException)
Returned when an error is encountered at the data source
Error
Exposes the information from a warning or error returned by a data source
ClientPermission Provided for .NET Framework data provider code access security attributes
CONFIDENTIAL 9
ADO.NET Components
DataSet
DataRelationCollection
ExtendedProperties
DataTableCollection
DataTable
DataRowCollection
DataView DataRow
ChildRelations
ParentRelations
Constraints
DataColumnCollection
Constraints
DataColumn DataColumnCollection
PrimaryKey
CONFIDENTIAL 10
ADO.NET Components
Create a SqlConnection
to the database
Set up a Command
CONFIDENTIAL 11
ADO.NET Components
CONFIDENTIAL 12
Connection, Command,
DataReader
CONFIDENTIAL 13
Connecting to a Data Source
Just for the sake of the example, we’re using SqlConnection class:
Represents unique
SQL Server connection
CONFIDENTIAL 14
SqlConnection
SqlConnection constructors:
CONFIDENTIAL 15
Connection Strings
CONFIDENTIAL 16
Connection Strings
keyword1=value; keyword2=value;
Keywords are not case-sensitive. Values may be case-sensitive (depending on the data source)
The enclosing character may not occur within the value it encloses
Keyword='double"quotation;mark’;
Keyword="single'quotation;mark";
You can also escape the enclosing character by using two of them together
Keyword="double""quotation";
Keyword='single''quotation';
CONFIDENTIAL 17
Connection Strings
Data Source The name or network address of the instance of SQL Server to which to
connect. The port number can be specified after the server name:
Server
server=tcp:servername, portnumber
Addr
From .NET Framework 4.5 onward you can connect to LocalDB as
follows:
Network Address server=(localdb)\InstanceName
CONFIDENTIAL 18
Connection Strings
Initial Catalog
The name of the database. The database name can be 128 characters
-or- N/A
or less.
Database
Integrated Security When false, User ID and Password are specified in the connection.
-or- ‘false’ When true, the current Windows account credentials are used for
Trusted_Connection authentication.
Password
The password for the SQL Server account logging on.
-or- N/A
Not recommended!
PWD
User ID
The SQL Server login account.
-or- N/A
Not recommended!
UID
CONFIDENTIAL 19
Connection Strings
AttachDBFilename
-or-
The name of the primary database file, including the full path name of
Extended Properties N/A
an attachable database.
-or-
Initial File Name
Any newly created connection will be added to the pool when closed
Pooling ‘true’
by the application.
Connect Timeout
-or-
The length of time (in seconds) to wait for a connection to the server
Connection Timeout 15
before terminating the attempt and generating an error.
-or-
Timeout
CONFIDENTIAL 20
Connection Strings Builders
Connection String Injection Attacks
CONFIDENTIAL 21
Connection Strings Builders
Building Connection Strings from Configuration Files:
CONFIDENTIAL 22
Connection Strings Builders
Example:
CONFIDENTIAL 23
Connection Pooling
Connection
** returned
from the pool
Connection Pool
Application Database
Connection
New
connection
request Connection Physical
Connections
Application
…
New
connection Connection
request
Connection returned
from the pool
CONFIDENTIAL 24
Connection Pooling
Pool creation and assignment example:
Pool A is created
CONFIDENTIAL 25
Command
CONFIDENTIAL 26
Commands and Parameters
After establishing a connection you can execute commands on a data source
using DbCommand object
object
DbCommand
CONFIDENTIAL 27
Commands and Parameters
Execute commands (in this case – stored procedure):
CONFIDENTIAL 28
SqlCommand class
Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database.
SqlCommand constructors:
CONFIDENTIAL 29
SqlCommand class
SqlCommand Properties:
Gets or sets the Transact-SQL statement, table
CommandText name or stored procedure to execute at the data
source.
Gets or sets a value indicating how the
CommandType
CommandText property is to be interpreted.
Gets or sets the SqlConnection used by this
Connection
instance of the SqlCommand.
CONFIDENTIAL 30
SqlCommand class
Commonly used SqlCommand methods for executing commands at a SQL Server database:
CONFIDENTIAL 31
Command Types
Each strongly typed command object also supports a CommandType enumeration that specifies how
a command string is interpreted:
*TableDirect is only supported by the .NET Framework Data Provider for OLE DB. Multiple table access is not
supported when CommandType is set to TableDirect.
CONFIDENTIAL 32
CRUD in ADO.NET
CONFIDENTIAL 33
CRUD in ADO.NET
CRUD stands for CREATE, READ, UPDATE, DELETE or INSERT, SELECT, UPDATE, DELETE in SQL.
CONFIDENTIAL 34
CRUD in ADO.NET
CRUD stands for CREATE, READ, UPDATE, DELETE or INSERT, SELECT, UPDATE, DELETE in SQL.
CONFIDENTIAL 35
CRUD in ADO.NET
Update example:
CONFIDENTIAL 36
CRUD in ADO.NET
Delete example:
CONFIDENTIAL 37
CRUD in ADO.NET
Insert example:
CONFIDENTIAL 38
Data Reader
CONFIDENTIAL 39
DataReader
You can use the DataReader to retrieve a read-only, forward-only stream of data from a database:
CONFIDENTIAL 40
Closing the DataReader
• Always call the Close method when you have finished using the DataReader object
• If your Command contains output parameters or return values, those values are not available until
the DataReader is closed
• While a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot
execute any commands for the Connection, including creating another DataReader, until the
original DataReader is closed
• Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the
Finalize method of your class.
CONFIDENTIAL 41
DataReader and Multiple Result Sets
If the DataReader returns multiple result sets, call the NextResult method to iterate through the result
sets sequentially
CONFIDENTIAL 42
Stored Procedures
CONFIDENTIAL 43
Commands with Stored Procedures
Stored procedures benefits in .NET context, they:
• Improve security
CONFIDENTIAL 44
Executing a Stored Procedure
Using a stored procedure with ADO.NET is easy. You simply follow four steps:
CONFIDENTIAL 45
Executing a Stored Procedure
Generic generic update command:
CONFIDENTIAL 46
Updating a record with a stored procedure
CONFIDENTIAL 47
Stored Procedure with Output Parameters
CREATE Procedure CustomerAdd
(
@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
@CustomerID int OUTPUT
)
AS
INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
SELECT
@CustomerID = @@Identity
GO
CONFIDENTIAL 48
Using a stored procedure with an output parameter
CONFIDENTIAL 49
Transactions
CONFIDENTIAL 50
Transactions
Transaction
Database
Funds transfer
Update // Bob // -100 $
Single
unit of work
Insert // Transaction
Transfer 100$ from Bob
to Alice
CONFIDENTIAL 51
Manual Transactions
You can set a Command to run in a transaction either by setting its Transaction property:
or
CONFIDENTIAL 52
Manual Transactions
Once running in a Transaction, commands can be executed on the Command object within a
try/catch block:
CONFIDENTIAL 53
Transaction Isolation Levels
Condition Description
Two or more transactions select the same row and subsequently update the
Lost update
row.
Uncommitted dependency A second transaction selects a row that has been updated, but not committed,
(dirty read) by another transaction.
A second transaction reads different data each time the same row is read. The
Inconsistent analysis
second transaction reads data that has been changed and committed by
(nonrepeatable read)
another transaction between the reads.
CONFIDENTIAL 54
Isolation Level Enumeration
Name Description
No shared locks are issued, and exclusive locks aren't honored. A dirty read is
ReadUncommitted
possible.
Shared locks are held while data is read by the transaction. Dirty reads aren't
ReadCommitted possible, but nonrepeatable reads or phantom rows can occur because data can
be changed before it is committed.
Shared locks are placed on all data used in a query preventing other users from
RepeatableRead
updating the data.
A range lock, where the individual records and the ranges between records are
Serializable covered, is placed on the data preventing other users from updating or inserting
rows until the transaction is complete.
Pending changes from more highly isolated transactions can't be overwritten.
Chaos
Not supported by SQL Server.
A different isolation level than the one specified is being used, but that level
Unspecified
can't be determined.
CONFIDENTIAL 55
Transaction Isolation
CONFIDENTIAL 56
Disconnected ADO.NET
DataSet
CONFIDENTIAL 57
DataSets
CONFIDENTIAL 58
DataSets
DataSet
DataRow DataTable
DataRelation DataViewSettingsCollection
DataRowCollection
DataColumn
DataCoulmnCollection DataViewSetting
Constraint ConstraintCollection
DefaultView
DataView
CONFIDENTIAL 59
Creating an Untyped DataSet
There are several ways a DataSet can be created. Simplest way is to call a constructor:
Copy() method creates new DataSet with same schema and data:
Clone() method crates new DataSet with same schema, but none of the data of the original:
CONFIDENTIAL 60
Working with Tables in the DataSet
Tables are added to the DataSet using the Add( ) method of the DataTableCollection (Tables
property of DataSet):
The AddRange() method allows more than one table to be added to the DataSet in the same
statement:
CONFIDENTIAL 61
Working with Tables in the DataSet
A DataTable can also be created automatically in a DataSet when the Fill( ) or FillSchema( )
method of the DataAdapter is called:
CONFIDENTIAL 62
Working with Tables in the DataSet
The Count property returns the number of tables within the DataSet:
The Contains( ) method determines whether a table with a specified table name exists within a
DataSet:
CONFIDENTIAL 63
Working with Tables in the DataSet
The IndexOf( ) method returns the index of the table within the collection using either a
reference to the table object or the name of a table:
CONFIDENTIAL 64
Working with Tables in the DataSet
The Remove( ), RemoveAt( ), and Clear( ) methods remove tables from the DataSet:
The RemoveAt( ) method removes the table at the specified index from the DataTableCollection
object:
CONFIDENTIAL 65
Adding and Removing Relations
Use Remove( ) or Contains ( ) methods to remove or to determine if specific relation exists, respectively:
CONFIDENTIAL 66
DataTables
CONFIDENTIAL 67
Creating a DataTable
There are several ways a DataTable can be created. In the simplest case, a DataTable is
created using the new keyword.
A DataTable can also be created automatically in a DataSet when the Fill( ) or FillSchema( )
method of the DataAdapter is called specifying a table that doesn't already exist in the DataSet.
CONFIDENTIAL 68
Working with Columns
There are two methods that can add a column to a table. The Add( ) method optionally takes
arguments that specify the name, type, and expression of the column to be added:
The second method for adding columns is the AddRange( ) method, which allows more than one
column stored in a DataColumn array to be added to the table in a single statement:
CONFIDENTIAL 69
Constraints
To add a constraint to a table, the Add( ) method takes an argument specifying a reference to an
existing constraint or takes specific arguments if a unique or foreign-key constraint is added.
CONFIDENTIAL 70
Constraints
Two overloads of the Add( ) method create and add UniqueConstraint objects in one statement:
The other two overloads of the Add( ) method create and add a ForeignKeyConstraint, as follows:
CONFIDENTIAL 71
Primary Key
The primary key is a column or collection of columns that uniquely identify each row in the table. The
PrimaryKey property accesses one or more DataColumn objects that define the primary key of the
DataTable.
CONFIDENTIAL 72
Rows
There are two methods that can add a row to a table. The Add( ) method takes either a DataRow
argument or an object array of columns of the row to be added:
CONFIDENTIAL 73
DataAdapter
CONFIDENTIAL 74
DataAdapters
The DataAdapter class serves as a bridge between a disconnected ADO.NET objects and a data
source.
• Update – updates any changes made to the DataSet or DataTable back to the data source
CONFIDENTIAL 75
Creating DataAdapter Object
The overloaded constructor for the DataAdapter allows several different ways to create the data
аdapter. Commonly used of them are:
Or more convenient:
CONFIDENTIAL 76
Retrieving Data from the Data Source
The Fill( ) method of the DataAdapter retrieves data from the data source into a DataSet or a
DataTable
CONFIDENTIAL 77
Retrieving Data from the Data Source
CONFIDENTIAL 78
Updating the Data Source
The Update( ) method can submit DataSet changes back to the data source. It uses the statements
in the DeleteCommand, InsertCommand, and UpdateCommand objects to attempt to update the data
source with records that have been deleted, inserted, or updated in the DataSet.
Original
Fill ( )
DataSet
Modify DataAdapter
Modified Data
Update ( )
DataSet source
CONFIDENTIAL 79
Updating the Data Source
Example of an Update( ) method. For simplicity, a CommandBuilder generates the update logic:
CONFIDENTIAL 80
Mapping Tables and Columns
DataTableMappingCollection DataTableCollection
Source Dest
Table DataTable
Table Table
Name Name DataColumnCollection
Column
DataRowCollection
DataColumnMappingCollection ConstraintCollection
Source Dest
Table Table
Name Name
DataRelationCollectio
n
CONFIDENTIAL 81
Mapping Tables and Columns
CONFIDENTIAL 82
Strongly Typed DataSets
CONFIDENTIAL 83
Strongly Type DataSets
Strongly typed DataSets are a collection of classes that inherit from the DataSet, DataTable, and
DataRow classes, and provide additional properties, methods, and events based on the DataSet
schema.
Using a strongly typed DataSet has a number of advantages over using untyped DataSet:
• Type mismatch errors and errors resulting from either misspelled or out-of-bounds indexer
arguments to retrieve tables and columns can be detected during compilation rather than at
runtime.
CONFIDENTIAL 84
Creating a Strongly Typed DataSet
CONFIDENTIAL 85
Creating a Strongly Typed DataSet
CONFIDENTIAL 86
Creating a Strongly Typed DataSet
CONFIDENTIAL 87
Creating a Strongly Typed DataSet
CONFIDENTIAL 88
Creating a Strongly Typed DataSet
The other way would be to use the XML Schema Definition Tool (XSD.EXE) found in the .NET Framework SDK
bin directory.
To generate the class file from Northwind.xsd, issue the following command from the command prompt:
CONFIDENTIAL 89
Q&A
CONFIDENTIAL
CONFIDENTIAL 32