it is the primary data access model for.net applications next version of ado can be divided into...

34

Upload: drusilla-burns

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data
Page 2: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It is the primary data access model for .Net applications

Next version of ADO Can be divided into two parts

◦ Providers◦ DataSets

Resides in System.Data namespace

Page 3: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It enables connection to the data source

Each data source has it’s own connection provider

Common data access objects◦ Connection◦ Command◦ Parameter◦ DataAdapter◦ DataReader

Page 4: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It provides the connection that is used for accessing data source

Common types◦ OleDbConnection◦ OdbcConnection◦ SqlConnection

Page 5: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Object Linking and Embedding Database

It aims to access to specific set of data sources from .Net applications

Continuous access to data source even path of the source is changes.

Odbc vs OleDb

System.Data.OleDb

Page 6: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Commonly used properties and functions

◦ ConnectionString◦ ConnectionTimeout◦ BeginTransaction()◦ Close()◦ CreateCommand()◦ Open()

Page 7: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

To add a connection object to the form, just drag and drop an OleDbConnection from the toolbox

Page 8: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Use ConnectionString property in to Properties Window to set connection information.

DataLink properties window will be opened when you ConnectionString property click

Page 9: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

From the first tab select the type of data provider

To define a connection to an access database, you should select Microsoft Jet 4.0 OLE Db Provider

Page 10: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data
Page 11: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data
Page 12: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It performs CRUD (Create-Read-Update-Delete) operations on the database.

Common Types◦ OdbcCommand◦ OleDbCommand◦ SqlCommand

Page 13: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It uses OleDb framework

Common properties and functions

◦ CommandText◦ Connection◦ Parameters◦ Transaction◦ ExecuteNonQuery()◦ ExecuteReader()

Page 14: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Constructors

◦ OleDbCommand()

◦ OleDbCommand(string cmdText)

◦ OleDbCommand(string cmdText, OleDbConnection myoledbConn)

◦ OleDbCommand(string cmdText, OleDbConnection myoledbConn, OleDbTransaction myoledbtrans)

Page 15: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Type of the object

Name of the object

Connection object

The SQL command that

will be run

Page 16: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It provides a data parameter to command object

Usage;◦ First crate a command object with an SQL

statement which contains special characters for placing parameters.

◦ Add parameters to the command object with assigning values

◦ Execute the command object

Page 17: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Adding parameters

Type of the parameter

Length of the parameter

Assigning values to the parameter

Page 18: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Used for retrieving data from datasource without modifying the actual data (works readonly)

Common types◦ OdbcDataReader◦ OleDbDataReader◦ SqlDataReader◦ OracleDataReader◦ Db2DataReader

Page 19: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It has no public constructor. So, to crate a DataReader object you should call ExecuteReader() function of the releates command object.◦ OleDbDataReader ordr = ocmd.ExecuteReader();

When Read() fucntion is called, DateReader object starts to read data or moves to next record.◦ if(ordr.Read())◦ while(ordr.Read())

To access actual data one should use indexes or Get functions of the reader◦ ordr[0].ToString();◦ ordr[“NameOfTheColumn”].ToString()◦ ordr.GetString(0);

Page 20: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

If you plan to continue to use Connection object (e.g. to execute another SQL statement), then you should call Close() function of the reader object.◦ ordr.Close();

Common properties and functions◦ IsClosed◦ FieldCount◦ GetInt32(), GetDecimal(), GetString() ......◦ IsDBNull()◦ Read()◦ Close

Page 21: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Reader object

If there is data to be

read

Create reader object by

executing the command

Fetch the data by providing the column

number on the current row

Page 22: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Reader object

If there is data to be

read

Create reader object by

executing the command

Fetch the data by providing the column

name on the current row

Page 23: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Filter a set of data Eleminates row mismathcing rows, passes

matching ones. If a select sql is executed without a where

keyword, then all rows in tables that are used in select sql will be returned.

It needs columns in order to be used Pay importance to data types while using

where keyword.

Page 24: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

SELECT * FROM PERSONEL WHERE ADI = ‘ALİ’◦ This query will return all rows in table PERSONEL whose name are

equal to ALİ◦ where keyword should be used after table name◦ After then we should write filter statements

SELECT * FROM PERSONEL WHERE ADI LIKE ‘AL%’◦ This query will return all rows in table PERSONEL whose name

starts with AL◦ LIKE keyword is used to search for specified patterns in a column.

SELECT * FROM PERSONEL WHERE ADI = ‘ALİ’ AND SOYADI = ‘KAYA’◦ Where keyword may include multiple filtering statements◦ The sql query seen above will return the rows with name equals

ALİ and surname equals KAYA

Page 25: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It is used for erasing records from table

Generally used with where keyword

It is an irreversible operation (unless used within a transaction), so use it very carefully.

To execute a delete sql, first put it in to a command object, then call ExecuteNonQuery function.

Page 26: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

DELETE FROM PERSONEL◦ Deletes all rows (records) in table PERSONEL

DELETE FROM PERSONEL WHERE AGE < 18◦ Deletes rows in table PERSONEL who are younger

then 18

DELETE FROM PERSONEL WHERE ADI LIKE ‘AL%’◦ Deletes rows in table PERSONEL whose names

starts with AL

Page 27: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

It is used for changing values in a tables More effective then delete->insert Usually used with where keyword User should supply column names that will

be updated To execute a update sql, first put it in to a

command object, then call ExecuteNonQuery function.

Page 28: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

UPDATE PERSONEL SET ADI = ‘ALİ’◦ Changes all names to ALİ in table PERSONEL

UPDATE PERSONEL SET YAS = 18 WHERE YAS < 18 AND SOYAD = ‘KAYA’◦ Sets the age value of the records to 18 who are

younger than 18 and surname is KAYA

Page 29: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

This operation is called JOIN operation It combines desired columns from multiple

tables in to one data set. Usually one column of a table is matched to

anoter related column in other table If two tables have column with same name,

then we should write table names before column names to get over confusion

Page 30: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

ID ST_NAME

ST_SURNAME

1 ALİ KAYA

2 VELİ TAN

ID LC_NAME

5 PHYSICS

6 CHEMISTRY

ID ST_ID LC_ID GRADE

1 1 5 50

2 1 6 30

3 2 5 80

STUDENTS LECTURES

GRADES

Page 31: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Id, name and surnames are stored in STUDENT table

Id and lecture names are stored in LECTURES table

In grades table we store the scores of students in courses

How to find the score of a student in a specified lecture.

Page 32: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Answer: We have to combine (join) three tables.

How?◦ Pick the id of the student from STUDENTS table◦ Pick the lecture information from LECTURES table◦ Pick the lectures that student attends from

GRADES table◦ Fetch grades of students from desired lessons

from GRADES table

Page 33: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

Joining

◦ Join STUDENTS (ID) with GRADES(ST_ID)

◦ Join LECTURED(ID) with GRADES(LC_ID)

Page 34: It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data

To select student name, surname, attended lectures and grades we should write the following SQL query

SELECT ST_NAME, ST_SURNAME, LC_NAME, GRADE FROM STUDENTS, GRADES, LECTURES

WHERE STUDENTS.ID = GRADES.ST_ID AND LECTURES.ID = GRADES. LC_ID