• No results found

2021/2022(1) IF Web Programming

N/A
N/A
Protected

Academic year: 2022

Share "2021/2022(1) IF Web Programming"

Copied!
105
0
0

Loading.... (view fulltext now)

Full text

(1)

2021/2022(1)

IF184504 Web Programming

Lecture #12

ADO.NET

Misbakhul Munir IRFAN SUBAKTI

司馬伊凡

Мисбакхул Мунир Ирфан Субакти

(2)

ADO.NET: What is that?

• Technology used .NET for accessing data structured, where this technology handling different data source into a single interface

• Uniform object oriented interface for different data sources

• Relational database

• XML data

• Any other data sources: text, CSV, etc.

• ADO.NET technology designed for web-based distributed applications

• Two kind of accessing data models

• Connection-oriented

• Connectionless

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 2

(3)

Universal data access: Data uniformity

• Object-Oriented Programming concept extended to data

• Data can be seen as an object → its source doesn’t matter anymore

• Need a bridge (like an Application Programming Interface – API, in

programming) to access data source via a provider

(4)

Application & data source: Relationship

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 4

(5)

Data providers: Enabling data access for app

• Microsoft’s layered architecture for data access

(6)

Universal data access (Microsoft): History of

• Prior o ADO.NET, it’s ADO (ActiveX Data Objects) technology

• It’s Microsoft’s technology for accessing data source

❖ ODBC

❖ OLE DB

❖ ADO (ActiveX Data Objects)

❖ ADO.NET

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 6

(7)

ADO vs ADO.NET: Comparison

ADO

• Connection-oriented

• Sequential access

• Only one table supported

• COM-marshalling

ADO.NET

• Connection-oriented + connectionless

• Main-memory representation with direct access

• More than one table supported

• XML-marshalling

(8)

ADO.NET: Architecture

• Data processing has traditionally relied primarily on a connection- based, two-tier model.

• As data processing increasingly uses multi-tier architectures,

programmers are switching to a disconnected approach to provide better scalability for their applications.

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 8

(9)

ADO.NET: Components

• The two main components of ADO.NET for accessing and manipulating data are

• .NET Framework data providers, and

• DataSet.

(10)

.NET Framework data providers

• The .NET Framework Data Providers are components that have been explicitly designed for data manipulation and fast, forward-only, read-only access to data.

• The Connection object provides connectivity to a data source.

• The Command object enables access to database commands to return data,

modify data, run stored procedures, and send or retrieve parameter information.

• The DataReader provides a high-performance stream of data from the data source.

• Finally, the DataAdapter provides the bridge between the DataSet object and the data source.

• The DataAdapter uses Command objects to execute SQL commands at the data source to both load the DataSet with data and reconcile changes that were made to the data in the DataSet back to the data source.

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 10

(11)

DataSet

• The ADO.NET DataSet is explicitly designed for data access independent of any data source.

• As a result, it can be used with multiple and differing data sources, used with XML data, or used to manage data local to the application.

• The DataSet contains a collection of one or more DataTable objects consisting of rows and columns of data, and also primary key, foreign key, constraint, and relation information about the data in the

DataTable objects.

(12)

.NET Framework data providers & DataSet

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 12

(13)

DataReader or DataSet?

• When you decide whether your application should use a DataReader or a DataSet, consider the type of functionality that your application requires. Use a DataSet to do the following:

• Cache data locally in your application so that you can manipulate it. If you only need to read the results of a query, the DataReader is the better choice.

• Remote data between tiers or from an XML Web service.

• Interact with data dynamically such as binding to a Windows Forms control or combining and relating data from multiple sources.

• Perform extensive processing on data without requiring an open connection to the data source, which frees the connection to be used by other clients.

• If you do not require the functionality provided by the DataSet, you can improve the performance of your application by using the DataReader to return your data in a forward-only, read-only

manner.

• Although the DataAdapter uses the DataReader to fill the contents of a DataSet, by using the

DataReader , you can boost performance because you will save memory that would be consumed

(14)

ADO.NET: Connection architecture

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 14

(15)

Connection-oriented vs Connectionless

• Connection-oriented

• Keeps the connection to the data base alive

• Intended for application with

• Short running transactions

• Only a few parallel accesses

• Up-to-date data

• Connectionless

• No permanent connection to the data source

• Data cached in main memory

• Changes in main memory  changes in data source

(16)

ADO.NET: Assembly & Namespaces

• ADO.NET access involved two kind of libraries

• Assembly

• System.Data.dll

• Namespaces

• System.Data General data types

• System.Data.Common Classes for implementing providers

• System.Data.OleDb OLE DB provider

• System.Data.SqlClient Microsoft SQL Server provider

• System.Data.SqlTypes Data types for SQL Server

• System.Data.Odbc ODBC provider (since .NET 1.1)

• System.Data.OracleClient Oracle provider (since .NET 1.1)

• System.Data.SqlServerCe Compact Framework

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 16

(17)

Connection-oriented

ADO.NET

(18)

Connection-oriented: Architecture

• DbConnection

• Represents connection to the data source

• DbCommand

• Represents a SQL command

• DbTransaction

• Represents a transaction

• Commands can be executed within a transaction

• DataReader

• Results of a database query

• Allows sequential reading of rows

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 18

(19)

Class hierarchy

• General interface definitions

• IDbConnection

• IDbCommand

• IDbTransaction

• IDataReader

• Special implementations

• OleDb: implementation for OLEDB

• Sql: implementation for SQL Server

• Oracle: implementation for Oracle

(20)

Microsoft SQL Server: Download & install

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 20

(21)

SQL Server Management Studio (SSMS)

• SQL Server Management Studio (SSMS) is an integrated environment for managing any SQL infrastructure, from SQL Server to Azure SQL Database. SSMS provides tools to configure, monitor, and administer instances of SQL Server and databases. Use SSMS to deploy, monitor, and upgrade the data-tier components used by your applications, and build queries and scripts.

• Use SSMS to query, design, and manage your databases and data

warehouses, wherever they are - on your local computer, or in the

cloud.

(22)

SSMS: Download & install

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 22

(23)

Example: Northwind database

• Microsoft SQL Server example: Northwind DB

• Download & restore Northwind DB, e.g., via SSMS

(24)

Northwind database

• Reading the table Employee, then print out all the rows based on EmployeeID,

LastName and FirstName

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 24

(25)

Connection-oriented data access: Pattern

1. Declare the connection try {

1. Request connection to database 2. Execute SQL statements

3. Process result 4. Release Resources } catch (Exception) {

Handle exception } finally {

try {

4. Close connection

} catch (Exception) {

(26)

Example: EmployeeReader

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 26

(27)

Interface IDbConnection

(28)

IDbConnection: Property ConnectionString

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 28

(29)

Command objects

• Command objects define SQL statements or stored procedures

• Executed for a connection

• May have parameters

• May belong to a transaction

(30)

Interface IDbCommand

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 30

(31)

ExecuteReader method

(32)

ExecuteNonQuery method

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 32

(33)

ExecuteScalar method

(34)

Parameter

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 34

(35)

Working with parameters

(36)

Transactions

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 36

(37)

Working with transactions

(38)

Working with transactions (continued)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 38

(39)

Isolation levels for transactions

(40)

DataReader

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 40

(41)

Interface IDataReader

(42)

Working with IDataReader

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 42

(43)

Connectionless-oriented

ADO.NET

(44)

Motivation & idea

• Motivation

• Many parallel, long lasting access operations

• Connection-oriented data access too costly

• Idea

• Caching data in main memory

• “main memory database”

• Only short connections for reading & updates

• DataAdapter

• Main memory database independent from data source

• Conflicting changes are possible

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 44

(45)

Microsoft 3-tier: Architecture

(46)

Connectionless data access: Architecture

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 46

(47)

DataSet

(48)

DataSet: Structure

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 48

(49)

DataSet: Class diagram

(50)

Example: Person contacts

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 50

(51)

Person Contacts: Define schema

(52)

Person Contacts: Define schema (continued)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 52

(53)

Person Contacts: Define relation

(54)

Person Contacts: Define DataRow

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 54

(55)

Person Contacts: Access data

(56)

DataSet: Change management

• DataSets maintain all changes

• Changes are accepted with acceptChanges

• Or discarded with rejectChanges

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 56

(57)

State diagram of a DataRow object

• DataRow objects

have different states

(58)

DataRowVersion

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 58

(59)

Exception handling

• ADO.NET checks validity of operations on DataSets

• And throws DataException s

(60)

DataView

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 60

(61)

DataView: Working with

(62)

Database access with DataAdapter

ADO.NET

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 62

(63)

Architecture

(64)

DataAdapter: Class diagram

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 64

(65)

DataAdapter: Loading data

(66)

DataAdapter: Loading schema & data

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 66

(67)

DataAdapter: Writing back changes

(68)

DataAdapter: Writing back changes (cont’d)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 68

(69)

DataAdapter: Event handling

(70)

DataAdapter: Event handling example

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 70

(71)

Integration with XML

ADO.NET

(72)

Integration DataSets & XML

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 72

(73)

Writing & reading XML data

(74)

Writing & reading XML data: Example

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 74

(75)

DataSet & XML schema

(76)

Typed DataSets

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 76

(77)

Typed DataSets: Example

(78)

Access to DataSets using XML-DOM

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 78

(79)

Preview of ADO.NET 2.0

ADO.NET

(80)

ADO.NET 2.0

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 80

(81)

Bulk copy operation

(82)

Multiple Active Result Sets (MARS)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 82

(83)

Asynchronous operations

(84)

Asynchronous operations: Example

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 84

(85)

Batch processing of database updates

(86)

Paging

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 86

(87)

ObjectSpaces

(88)

ObjectSpaces: Example

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 88

(89)

MyADO (1)

(90)

MyADO (2)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 90

(91)

MyADO (3)

(92)

MyADO (4)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 92

(93)

MyADO (5)

(94)

MyADO (6)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 94

(95)

MyADO: Output

(96)

MyADO2 (1)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 96

(97)

MyADO2 (2)

(98)

MyADO2 (3)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 98

(99)

MyADO2 (4)

(100)

MyADO2 (5)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 100

(101)

MyADO2 (6)

(102)

MyADO2 (7)

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 102

(103)

MyADO2: Output

(104)

Summary

ADO.NET

08/12/2021 2021/2022(1) – Web Programming | MM Irfan Subakti 104

(105)

Summary

• Connection-oriented data access model

• For applications with only a few parallel, short running transactions

• Object-oriented interface abstracts from data source

• Access to database by SQL commands

• Connectionless data access model

• For applications with many parallel, long running transactions

• DataSet as main memory database

• DataAdapter is used as connector to the data source

References

Related documents

Interviews were conducted before and after intervention using a physical restraint questionnaire to examine any change in staff knowledge, attitudes, and behaviours on physical

Unit descriptor This unit describes the performance outcomes, skills and knowledge required to organise domestic and overseas business travel, including developing

As you may recall, last year Evanston voters approved a referendum question for electric aggregation and authorized the city to negotiate electricity supply rates for its residents

Most consumers want to experience a car before they buy it (see panel on “complexity and emotion”) and – at least today – the only way to do that is usually to contact a dealer

According to our findings, the studied journals were in compliance with Scopus' journal selection criteria in the cases regularity in publishing, providing informative

HPC 5380 College Students and their Environments HPC 5820 College Student Development Theories I HPC 5190 Helping Skills in Student Affairs Practice Spring 2012.. HPC 5751

The fifth grade mathematics curriculum that was applied in 2005-2006 academic year and that of 2013 program were investigated comparatively in terms of the following criteria:

Finally, we find that banks prefer to acquire equity interests in countries where either regulatory restrictions on banking activities are lower, possibly because these restrictions