outline - wmich.edualfuqaha/summer09/cs5950/... · and od.productid = p.productid ... initial...

33
Data Access This material is based on the original slides of Dr. Mark Sapossnek, Computer Science Department, Boston University, Mosh Teitelbaum, evoch, LLC, and Joe Hummel, Lake Forest College Outline Relational Databases ADO.NET Overview ADO.NET Classes

Upload: truongkien

Post on 31-Aug-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Data Access

This material is based on the original slides of Dr. Mark Sapossnek, Computer Science Department, Boston University, Mosh Teitelbaum, evoch, LLC, and Joe Hummel, Lake Forest College

Outline

Relational Databases

ADO.NET Overview

ADO.NET Classes

Databases Databases

Virtually all interesting applications require a structured, persistent data store

E-Commerce: placing an order, fulfilling an order

HR: Personnel data

Sales

Database needs vary with the type of applicationTransaction Processing/OLTP

Business Intelligence/Data Warehouse/OLAP

Relational DatabasesTables

Table (relation, entity)A collection of data about a specific type of thing Organized in rows and columns

Column (attribute, field)Describes part of an entity (e.g. FirstName)Has a data type (e.g. integer, character, binary)Can be null

Row (tuple, record)A single instance of data in a tableEach row is unique

JonesDiane2

SmithJoe1

LastNameFirstNameAuthID

Relational DatabasesRelating Data

Tables can be related through primary/foreign key relationships (e.g., a book has an author)

Primary keyGuarantees the uniqueness of a row

Can be composed of one or more columns

Ensures entity integrity

Foreign keyEstablishes logical relationship between tables

One or more columns of a table that match the primary or alternate key of another table

Referential integrity

Relational DatabasesRelating Data

Primary Key

Foreign Key

Books Table

Authors Table

PK/FK Relationship

JonesDiane2

SmithJoe1

LastNameFirstNameAuthID

ReferenceDatabase Handbook12

AutobiographyMy Life as a DBA21

TypeTitleAuthIDBookID

Relational DatabasesNormalization/Denormalization

NormalizationThe process of breaking large tables into multiple smaller tables

Goal: minimize redundant data, maximize correctness

Improves performance for updates

Desirable in transaction-based applications

DenormalizationThe process of combining smaller tables into fewer larger tables

Goal: improve performance

Introduces redundant data

Improves performance for reads

Desirable in data warehouse applications

Relational DatabasesJoins

A join is a way of combining data in multiple tables, usually by resolving primary key/foreign key relationships

$25

$8

$5

$10

Cost

BleccoFoobar

Blecco

AcmeThingy

Widget

Acme

Vendor

Widget

Product

Product table

Blecco

Acme

Vendor

Adam P.

Linda A.

Contact

WA

MA

State

Vendor table

Relational DatabasesJoins

Result of a natural join

Linda A.MAAcme$10Widget

Linda A.MAAcme$5Thingy

Blecco

Blecco

Vendor

WA

WA

State

Adam P.

Adam P.

Contact

$25

$8

Cost

Foobar

Widget

Product

Relational DatabasesStructured Query Language (SQL)

Standard language for accessing a relational database, standardized by American National Standards Institute (ANSI); SQL-92Open, but not really

Common functions are mostly the same across productsMost vendors have proprietary extensions

Subsets of SQLData Definition Language (DDL)Data Manipulation Language (DML)Data Control Language (DCL)

Relational DatabasesDDL Examples

Used to create and modify database objects

CREATE DATABASE Bookstore

CREATE TABLE tBooks

(

BookID INT IDENTITY(1,1) PRIMARY KEY,

Title VARCHAR(30) NOT NULL,

PubDate DATE NOT NULL,

[Description] VARCHAR(50),

Category INT NOT NULL

)

Relational DatabasesDML Examples

Select data to viewSELECT * FROM tAuthors

SELECT AuthID, FirstName, LastName

FROM tAuthors

SELECT AuthID, FirstName, LastName, Phone

FROM tAuthors

WHERE City = ‘Boston’

SELECT FirstName, LastName, Phone

FROM tAuthors

WHERE AuthID = 249

Relational DatabasesDML Examples

Using SELECT to join tables

SELECT AuthID, FirstName, LastName, Phone,

BookID, Title, PubDate, Description

FROM tAuthors, tBooks

WHERE tAuthors.AuthID = tBooks.AuthID

SELECT AuthID, FirstName, LastName, Phone,

BookID, Title, PubDate, Description

FROM tAuthors INNER JOIN tBooks

ON tAuthors.AuthID = tBooks.AuthID

Relational DatabasesDML Examples

INSERT INTO tBooks

(Title, PubDate, [Description], Category)

VALUES

(‘Database Design’, GETDATE(),

‘How to design a database’, 3)

UPDATE tAuthors

SET Phone = ‘617-555-1234’

WHERE AuthID = 5

DELETE FROM tAuthors

WHERE AuthID = 5

Insert, update and delete data

Relational DatabasesDCL Examples

Set security options on database objects

GRANT INSERT, UPDATE, DELETE

ON tAuthors

TO Mary, John

REVOKE CREATE TABLE FROM Joe

DENY ALL

ON tAuthors, tBooks

TO Sally

Relational DatabasesViews

A view is a virtual table

Abstracts the underlying table structures

Abstracts a (possibly complex) query

Provides security abstraction from table

In SQL Server 2000, a view can beIndexed

Updated and inserted into

Relational Databases View Definition Example

CREATE VIEW vwCustomerOrders AS

SELECT o.OrderId, c.CompanyName

FROM Customers c

INNER JOIN Orders o

ON c.CustomerID = O.CustomerID

ORDER BY o.OrderId

Relational DatabasesView Usage Example

SELECT *

FROM vwCustomerOrders

WHERE CompanyName = 'My Favorite Customer'

My Favorite Customer137

My Favorite Customer101

CompanyNameOrderId

Relational DatabasesStored Procedures

A group of SQL statements that runs within the databaseNot part of SQL standardProvides greater performanceCan control access to dataCan accept parametersCan return data

Output parametersReturn valuesResult set

Relational Databases Stored Procedure Example

CREATE PROCEDURE CustOrderHist @CustomerID nchar(5)

AS

SELECT ProductName, Total=SUM(Quantity)

FROM Products P, [Order Details] OD, Orders O, Customers C

WHERE C.CustomerID = @CustomerID

AND C.CustomerID = O.CustomerID

AND O.OrderID = OD.OrderID

AND OD.ProductID = P.ProductID

GROUP BY ProductName

Relational DatabasesStored Procedure Examples

exec CustOrderHist 'alfki'

......

21Chartreuse verte

6Aniseed Syrup

TotalProductName

Relational Databases Stored Procedure Examples

Use RETURN statement to return status0 is default in SQL Server

Can only be numeric

Use OUTPUT parameters to return results

RETURN 1

CREATE PROCEDURE MyProcedure @ReturnValue INT OUTPUT

...

SELECT @ReturnValue = ColumnName FROM Table

Relational DatabasesTriggers

Like stored procedures, triggers are code that runs within a databaseNot directly called by a userExecuted when a specified data modification takes place (INSERT, UPDATE or DELETE)Enforces business rulesFOR AFTER: trigger executes after triggering action completesFOR INSTEAD OF: trigger executes in place of triggering action

Relational DatabasesTransactions

Transaction: a sequence of SQL statements that constitute a logical unit of work

Outline

Databases

Relational Databases

ADO.NET Overview

ADO.NET Classes

ADO.NET OverviewLooking Back

ODBC (Open Database Connectivity)Interoperability to a wide range of database management systems (DBMS)Widely accepted APIUses SQL as data access language

OLE DBBroad access to data, relational and otherBuilt on COMNot restricted to SQL for retrieving dataCan use ODBC driversLow-level (C++) interface

ADO (ActiveX Data Objects)Simple component-based, object-oriented interfaceProvides a programming model to OLE DB accessible outside of C++

ADO.NET OverviewLooking Back

ADO

ODBC Provider Simple Provider Native Provider

OLE DB Provider

ODBC

ODBC Driver

TextFile

Database Database

OLE DB Provider

Mainframe

OLE DB

Your Application

ADO.NET OverviewLooking Back

ADO was designed as a connected, tightly coupled model

Appropriate for client/server architectures

Primarily relational (not hierarchical like XML)

Object design is not well factoredToo many ways to do the same thing

Objects try to do too much

Not originally designed for a distributed, n-tier environment

ADO.NET OverviewWhat Is ADO.NET?

ADO .NET is a collection of classes, interfaces, structures, and enumerated types that manage data access from relational data stores within the .NET Framework

These collections are organized into namespaces:System.Data, System.Data.OleDb, System.Data.SqlClient, etc.

ADO .NET is an evolution from ADO.Does not share the same object model, but shares many of the same paradigms and functionality!

ADO.NET OverviewADO.NET Goals

Well-factored design

Highly scaleable through a robust disconnected model

Rich XML support (hierarchical as well as relational)

Data access over HTTP

Maintain familiar ADO programming model

Keep ADO available via .NET COM interoperability

ADO.NET OverviewManaged Providers

Merges ADO and OLEDB into one layer

Each provider contains a set of classes that implement common interfaces

Initial managed provider implementations:ADO Managed Provider: provides access to any OLE DB data source

SQL Server Managed Provider: provides optimal performance when using SQL Server

Exchange Managed Provider: retrieve and update data in Microsoft Exchange

SQL Managed Provider

SQL ServerDatabase

ADO.NET OverviewManaged Providers

ADO.NET Managed Provider

ADO Managed Provider

OLE DB Provider

Database

Your Application

ADO.NET OverviewData Access Styles

Connected: Forward-only, read-onlyApplication issues query then reads back results and processes them

“Firehose” cursor

DataReader object

DisconnectedApplication issues query then retrieves and stores results for processing

Minimizes time connected to database

DataSet object

ADO.NET OverviewData Binding

Key component of Web Forms framework

Flexible and easy to useBind a control’s property to information in any type of data store

Provides control over how data moves back and forth

Simple controls for displaying a single value

Complex controls for displaying a data structure

<asp:Label runat=server Text='<%# CustList(0).FirstName %>'/>

Outline

Database Theory and History

Relational Database Concepts and Terminology

ADO.NET Overview

ADO.NET Classes

ADO.NET ClassesIDbConnection Interface

Creates a unique session with a data source

Implemented by SqlConnection and OleDbConnection

FunctionalityOpen, close connections

Begin transactionsIDbTransaction provide Commit and Rollback methods

Used in conjunction with IDbCommand and IDataAdapter objects

Additional properties, methods and collections depend on the provider

ADO.NET Classes IDbCommand Interface

Represents a statement to be sent to a data sourceUsually, but not necessarily SQL

Implemented by OleDbCommand and SqlCommand

FunctionalityDefine statement to executeExecute statementPass and retrieve parametersCreate a prepared (compiled) version of command

ExecuteReader returns rows, ExecuteNonQuerydoesn’t, ExecuteScalar returns single valueAdditional properties, methods and collections depend on the provider

ADO.NET Classes IDataReader Interface

Forward-only, read-only (“fire hose”) access to a stream of data

Implemented by SqlDataReader and OleDbDataReader

Created via ExecuteReader method of IDbCommand

Operations on associated IDbConnectionobject disallowed until reader is closed

ADO.NET Classes System.Data.OleDb Namespace

Managed provider for use with OLEDB providersSQLOLEDB (SQL Server) – use System.Data.SQL

MSDAORA (Oracle)

JOLT (Jet)

OLEDB for ODBC providers

OleDbConnection, OleDbCommand and OleDbDataReader classes

Classes for error handling

Classes for connection pooling

ADO.NET Classes DataReader Example

string sConnString = “Provider=SQLOLEDB.1;” +

“User ID=sa;Initial Catalog=Northwind;” +

“Data Source=MYSERVER”;

OleDbConnection conn = new OleDbConnection(sConnString);

conn.Open();

string sQueryString = “SELECT CompanyName FROM Customers”;

OleDbCommand myCommand = new OleDbCommand(sQueryString, conn);

OleDbDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read()) {

Console.WriteLine(myReader.GetString(0));

}

myReader.Close();

conn.Close();

ADO.NET ClassesSystem.Data Namespace

Contains the core classes of the ADO.NET architecture

Disconnected DataSet is central

Supports all types of applicationsInternet based

ASP.NET

XML

Windows forms based

ADO.NET ClassesSystem.Data Namespace

Contains classes used by or derived from managed providers

IDbConnection, IDbCommand, IDbDataReader

DataAdapter & DataSet

ADO.NET ClassesDataSet

A collection of tablesHas no knowledge of the source of the dataKeeps track of all relationships among tablesRich programming model (has objects for tables, columns, relationships, and so on)Remembers original and current state of dataCan dynamically modify data and metadataNative serialization format is XMLLocated in System.Data

ADO.NET Classes DataSet

DataSet

DataTable

DataRelation

DataRow

DataColumn

ADO.NET ClassesSystem.Data.SqlClient Namespace

Managed provider native to SQL ServerBuilt on TDS (Tabular Data Stream) for high performance in SQL ServerSqlConnection, SqlCommand and SqlDataReader classesClasses for

Error handlingConnection pooling (implicitly enabled by default )

System.Data.SqlTypes provides classes for native SQL Server data types

ADO.NET Classes IDataAdapter Interface

Populates or sends updates to a DataSet

Implemented by OleDbDataAdapter and SqlDataAdapter

Not connection based

Represents an asynchronous approach

A superset of a command object

Contains four default command objects for Select, Insert, Update, and Delete

ADO.NET Classes DataSet Example

string sConnString = “Persist Security Info=False;” +

“User ID=sa;Initial Catalog=Northwind;” +

“Data Source=MYSERVER”;

SqlConnection conn = new SqlConnection(sConnString);

conn.Open();

string sQueryString = “SELECT CompanyName FROM Customers”;

SqlDataAdapter myDSAdapter = new SqlDataAdapter();

DataSet myDataSet = new DataSet();

myDSAdapter.SelectCommand = new SqlCommand(sQueryString, conn);

myDSAdapter.Fill(myDataSet);

conn.Close();

ADO.NET Classes DataTable

In-memory object representing one tableColumns

Rows

Schema defined by Columns collection

Data integrity provided through Constraintobjects

Public eventsModifying/deleting rows

Modifying columns

ADO.NET Classes DataColumn

Fundamental building block of a DataTableschema (contained in Columns collection)

Defines what type of data may be entered (via DataType property)

Other important properties include AllowNull, Unique, and ReadOnly

Can contain Constraints (a collection on DataTable)

Can contain Relations (collection on DataSet)

ADO.NET Classes DataRow

Represents data in a DataTable (contained in Rows collection)

Conforms to schema defined by DataColumns

Properties for determining row state (e.g., new, changed, deleted, etc.)

All additions/modifications “committed” with AcceptChanges method of DataTable

ADO.NET Classes DataRelation

Relates two DataTables via DataColumns

DataType value of both DataColumns must be identical

Updates can be cascaded to child DataTables

Modifications that invalidate the relation are disallowed

ADO.NET Classes Creating a DataSet in Code

DataSet dataset = new DataSet();dataset.DataSetName = “BookAuthors”;

DataTable authors = new DataTable(“Author”);DataTable books = new DataTable(“Book”);

Create DataSet

Define tables

ADO.NET Classes Creating a DataSet in Code

Define columns

Define keysDataColumn id = authors.Columns.Add("ID", typeof(Int32));id.AutoIncrement = true;authors.PrimaryKey = new DataColumn[] {id};

DataColumn name = new authors.Columns.Add("Name",typeof(String));

DataColumn isbn = books.Columns.Add("ISBN", typeof(String));books.PrimaryKey = new DataColumn[] {isbn};

DataColumn title = books.Columns.Add("Title", typeof(String));DataColumn authid = books.Columns.Add(“AuthID”,typeof(Int32));DataColumn[] foreignkey = new DataColumn[] {authid};

ADO.NET Classes Creating a DataSet in Code

Add the tables to the DataSet

dataset.Tables.Add (authors);dataset.Tables.Add (books);

ADO.NET Classes Creating a DataSet in Code

Add data and save the DataSetDataRow shkspr = authors.NewRow();shkspr["Name"] = "William Shakespeare";authors.Rows.Add(shkspr);

DataRow row = books.NewRow();row["AuthID"] = shkspr["ID"];row["ISBN"] = "1000-XYZ";row["Title"] = "MacBeth";books.Rows.Add(row);

dataset.AcceptChanges();

ADO.NET ClassesTyped DataSets

Typed DataSetDerived from base DataSet class

Uses XML schema to generate new class

Tables, columns, etc. compiled into new class

Untyped DataSet

No built-in schema

Tables, columns, etc. exposed only as collections

ds.Customers.FirstName

ds.Tables[“Customers”].Rows[0][“FirstName”]

ADO.NET Classes Errors and Exceptions

Error classContains information on an error or warning returned by data sourceCreated and managed by Errors class

Errors classContains all errors generated by an adapterCreated by Exception class

Exception classCreated whenever an unhandled error occursAlways contains at least one Error instance

ADO.NET Classes Errors and Exceptions Example

try {

DataTable myTable = new DataTable();

myTable.Columns.Add(“myCol”);

myTable.Columns.Add(“myCol”);

//whoops!

}

catch (DataException myException) {

Console.WriteLine ("Message: " + myException.Message + "\n" +

"Source: " + myException.Source + "\n" +

“Stack Trace: " + myException.StackTrace + "\n");

}

Installing SQL Server 2005

Download and install:SQL Server 2005

OR

Download and install:SQL Server 2005 Express Edition

SQL Server Management Studio Express

Code Generators – What Are They?

Template based tools capable of generating code in any language

Generally read metadata/schema information from databases

CodeSmith has a syntax similar to ASP.NET making it familiar to a wide range of developers

Code Generators: How They Work

Template

Metadata

Generated Code

+

=

This is the code common to all generated output.

This is the data that makes each output different.

This is the result of running a template with metadata.

CodeSmith

Download CodeSmith from:

http://www.codesmithtools.com/

This tool helps to automate the auto-generation of the Data Access Layer (DAL) tier.

.NET TIERS

Download .NET Tiers from:http://www.nettiers.com/

.NET Tiers provides templates that can be used by CodeSmith to auto-generate code for the Data Access Layer (DAL).

Connection Strings

Use http://www.ConnectionStrings.com to prepare connection strings.