Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

CT050-3-1-WAPP

WEB APPLICATIONS
ADO.NET

Module Code & Module Title Slide Title SLIDE 1


Topic & Structure of The Lesson
• Introduction
• Relational Database Model
• SQL (Structured Query Language)
• SELECT Statement
• INSERT Statement
• UPDATE Statement
• DELETE Statement
• ADO.NET Object Model
• Data Provider
• Connecting to a Database in ASP.NET
• Data Binding
• Data Source Controls

Module Code & Module Title Slide Title SLIDE 2


Learning Outcomes

• At the end of this topic, You should be able to


• To understand the relational database model.
• To be able to write database queries using SQL (Structured Query Language).
• To learn various database interfaces.
• To understand ADO.NET’s object model.

Module Code & Module Title Slide Title SLIDE 3


Key Terms You Must Be Able To Use

If you have mastered this topic, you should be able to use the following
terms correctly in your assignments and exams:

• SQL
• ADO.NET

Module Code & Module Title Slide Title SLIDE 4


Introduction

• A database is an organized collection of data.

• A database management system (DBMS) provides mechanisms for storing,


organizing, retrieving, and modifying data for many users.

• Today's most popular database management systems are relational database


management systems (RDBMS).

Module Code & Module Title Slide Title SLIDE 5


Introduction

• SQL is the international standard language used almost universally with


relational database systems to perform queries and manipulate data.

• Programs connect to, and interact with, relational databases via an interface
software that facilitates communications between a database management
system and a program.

Module Code & Module Title Slide Title SLIDE 6


INTRODUCTION
C# programs
communicate with
databases and
manipulate their data
through ADO.NET.

Module Code & Module Title Slide Title SLIDE 7


The ADO.NET Object Model

Module Code & Module Title Slide Title SLIDE 8


Relational Databases

• A relational database stores data in tables. Tables are composed of rows and columns in
which values are stored.
• A primary key provides a unique value that cannot be duplicated in other rows of the
same table. The primary key uniquely identifies each row.
• Each row of a table represents a record.

Module Code & Module Title Slide Title SLIDE 9


Relational Databases

• Each column of a table represents a different attribute.

• The primary key can be composed of more than one column.

Module Code & Module Title Slide Title SLIDE 10


Relational Databases

number name department salary location


23603 Jones 413 1100 Kuala Lumpur
24568 Kelvin 413 2000 Jakarta
Row 34589 Larson 642 1800 Jakarta
35761 Marry 611 1400 Singapore
47132 Newton 413 9000 Singapore
78321 Stephens 611 8500 Bangkok

Primary key Column

Module Code & Module Title Slide Title SLIDE 11


Relational Databases

• DBMS
– Microsoft SQL Server
– Microsoft Access
– MySQL
– PostgreSQL
– Oracle
– Sybase
– FrontBase
– Etc.

Module Code & Module Title Slide Title SLIDE 12


SQL (STRUCTURED QUERY LANGUAGE)
SQL provides a rich set of language constructs that enable programmers to define complex
queries to retrieve data from a database.

Module Code & Module Title Slide Title SLIDE 13


SQL (Structured Query Language)

SQL keyword Description


SELECT Selects (retrieves) columns from one or more tables.
FROM Specifies tables from which to get columns or delete
rows. Required in every SELECT and DELETE
statement.
WHERE Specifies criteria that determine the rows to be
retrieved.
INNER JOIN Joins rows from multiple tables to produce a single
set of rows.
GROUP BY Specifies criteria for grouping rows.
ORDER BY Specifies criteria for ordering rows.
INSERT Inserts data into a specified table.
UPDATE Updates data in a specified table.
DELETE Deletes data from a specified table.
CREATE Creates a new table.
DROP Deletes an existing table.
COUNT Returns the number of records that satisfy given
search criteria.
Fig. 22.12 SQL keywords.

Module Code & Module Title Slide Title SLIDE 14


Basic SELECT Query

• SELECT * FROM Members

• SELECT FirstName, LastName FROM Members

Module Code & Module Title Slide Title SLIDE 15


WHERE Clause

• The optional WHERE clause in a query specifies the selection criteria for the query. The
basic form of a query with selection criteria is

SELECT ColumnName1, ColumnName2, ... FROM TableName WHERE Criteria

Module Code & Module Title Slide Title SLIDE 16


WHERE Clause

• The WHERE clause can contain operators <, >, <=, >=, =, <> and LIKE. Operator
LIKE is used for string pattern matching with wildcard characters % and -S.

Module Code & Module Title Slide Title SLIDE 17


WHERE Clause

• LIKE - Pattern matching


– (%)
SELECT FirstName, LastName FROM Members WHERE LastName LIKE ‘D%’

– ( _ ) Pattern matching
SELECT FirstName, LastName FROM Members WHERE LastName LIKE ‘_I%’

Module Code & Module Title Slide Title SLIDE 18


ORDER BY Clause

• The result of a query can be sorted in ascending or descending order using the optional
ORDER BY clause.

• The simplest form of an ORDER BY clause is

SELECT ColumnName1, ColumnName2, ... FROM TableName ORDER BY Column


ASC

Module Code & Module Title Slide Title SLIDE 19


ORDER BY Clause

SELECT ColumnName1, ColumnName2, ... FROM TableName ORDER BY Column


DESC

• where ASC specifies ascending order, DESC specifies descending order and column
specifies the column on which the sort is based. The default sorting order is ascending,
so ASC is optional.

Module Code & Module Title Slide Title SLIDE 20


ORDER BY Clause

• SELECT FirstName, LastName FROM Members ORDER BY LastName

• SELECT FirstName, LastName FROM Memberss ORDER BY LastName DESC

Module Code & Module Title Slide Title SLIDE 21


ORDER BY Clause

• Multiple columns can be used for ordering purposes with an ORDER BY clause of the
form

ORDER BY Column1 SortingOrder, Column2 SortingOrder, ...

Module Code & Module Title Slide Title SLIDE 22


ORDER BY Clause

• The WHERE and ORDER BY clauses can be combined in one query. If used, ORDER
BY must be the last clause in the query.

Module Code & Module Title Slide Title SLIDE 23


Merging Data from Multiple Tables: INNER JOIN

• An INNER JOIN merges rows from two tables by testing for matching values in a
column that is common to the tables. The basic form for the INNER JOIN operator is:

SELECT ColumnName1, ColumnName2, ... FROM Table1 INNER JOIN Table2 ON


Table1.ColumnName = Table2.ColumnName

Module Code & Module Title Slide Title SLIDE 24


Merging Data from Multiple Tables: INNER JOIN

• The ON clause specifies the columns from each table that are compared to determine
which rows are joined.

• If a SQL statement uses columns with the same name from multiple tables, the column
names must be fully qualified by prefixing them with their table names and a dot (.).

Module Code & Module Title Slide Title SLIDE 25


INSERT Statement

• An INSERT statement inserts a new row into a table. The basic form of this statement is

INSERT INTO TableName ( ColumnName1, ColumnName2, ..., ColumnNameN )


VALUES ( Value1, Value2, ..., ValueN )

• SQL uses single quotes (') as the delimiter for strings. To specify a string containing a
single quote in SQL, the single quote must be escaped with another single quote.

• where TableName is the table in which to insert the row. The TableName is
followed by a comma separated list of column names in parentheses. The list of column
names is followed by the SQL keyword VALUES and a comma-separated list of values
in parentheses.
Module Code & Module Title Slide Title SLIDE 26
UPDATE Statement

• An UPDATE statement modifies data in a table. The basic form of an UPDATE statement is

UPDATE TableName SET ColumnName1 = Value1, ColumnName2 = Value2, ...,


ColumnNameN = ValueN WHERE Criteria

where TableName is the table in which to update data. The TableName is followed by
keyword SET and a comma-separated list of column name/value pairs in the format
ColumnName = Value.

• The optional WHERE clause criteria determines which rows to update. If the WHERE
clause is omitted, the UPDATE applies to all rows of the table.

Module Code & Module Title Slide Title SLIDE 27


DELETE Statement

• A DELETE statement removes rows from a table. The simplest form for a DELETE
statement is

DELETE FROM TableName WHERE Criteria

where TableName is the table from which to delete a row (or rows). The optional
WHERE criteria determines which rows to delete. If the WHERE clause is omitted, the
DELETE applies to all rows of the table.

Module Code & Module Title Slide Title SLIDE 28


ADO.NET Object Model

• The ADO.NET object model provides an API Created for the .NET framework for
accessing database systems programmatically.

• Namespace System.Data is the root namespace for the ADO.NET API.

Module Code & Module Title Slide Title SLIDE 29


ADO.NET Object Model

• The other important namespaces for ADO.NET, System.Data.OleDb and


System.Data.SqlClient, contain classes that enable programs to connect with
and modify data sources (i.e., locations that contain data, such as a database or
an XML file).

Module Code & Module Title Slide Title SLIDE 30


ADO.NET Object Model

• An object of class SqlConnection of namespace System.Data.SqlClient represents a


connection to a data source specifically a SQL Server database.

• An object of class OleDBConnection of namespace System.Data.OleDB represents a


connection to a data source specifically an Access database.

Module Code & Module Title Slide Title SLIDE 31


ADO.NET Object Model

• An object of class SqlCommand of namespace System.Data.SqlClient represents an


arbitrary SQL command to be executed against a SQL Server database.

• An object of class OleDBCommand of namespace System.Data.OleDB represents an


arbitrary SQL command to be executed against an Access database.

Module Code & Module Title Slide Title SLIDE 32


ADO.NET Object Model

• A GridView ASP.NET data control displays data on a Web Form in a tabular format.

• A GridView's colors can be set using the Auto Format... link in the GridView Tasks
smart tag menu.

Module Code & Module Title Slide Title SLIDE 33


APP_DATA FOLDER
A database used by an ASP.NET Web site should be located in the project's App_Data
folder.

Module Code & Module Title Slide Title SLIDE 34


DATA PROVIDER

A .NET Framework data


provider is used for connecting
to a database, executing
commands, and retrieving
results.

Module Code & Module Title Slide Title SLIDE 35


Core Objects of Data Providers

Object Description

Connection Establishes a connection to a specific data source. The base class for all
Connection objects is the DbConnection class.

Command Executes a command against a data source. Exposes Parameters and can
execute within the scope of a Transaction from a Connection. The base
class for all Command objects is the DbCommand class.

DataReader Reads a forward-only, read-only stream of data from a data source. The
base class for all DataReader objects is the DbDataReader class.

DataAdapter Populates a DataSet and resolves updates with the data source. The base
class for all DataAdapter objects is the DbDataAdapter class.

Module Code & Module Title Slide Title SLIDE 36


Connecting to a Database in ASP.NET

• OleDbDataReader or SqlDataReader object


– Reads data from a database

• OleDbConnection or SqlConnection object


– Represents connection to database

Module Code & Module Title Slide Title SLIDE 37


Connecting to a Database in ASP.NET

• OleDbCommand or SqlCommand object


– Two parameters
• queryString
– Contains SQL to execute
• Database connection

• ExecuteReader

Module Code & Module Title Slide Title SLIDE 38


Connecting to a Database in ASP.NET

• Set of data and includes the tables that


DataSet contain and order it

OleDbDataAdapter • Retrieve information from database and


or place resulting information in DataSet
SqlDataAdapter

Module Code & Module Title Slide Title SLIDE 39


Data Binding

• The technique through which GUI controls are tied to data sources is known as data
binding.

• An AccessDataSource control allows a Web application to interact with an Access


database.

• A SqlDataSource control allows a Web application to interact with a SQL Server


database.

Module Code & Module Title Slide Title SLIDE 40


DATA BINDING
SqlDataSource property ConnectionString indicates the
connection through which the SqlDataSource control interacts
with the database.

An ASP.NET expression, delimited by <%$ and %>, can be


used to access a connection string stored in an application's
web.config configuration file.
Module Code & Module Title Slide Title SLIDE 41
DATA SOURCE CONTROLS

Data source controls enable rich


capabilities for retrieving and
modifying data, including querying,
sorting, paging, filtering, updating,
deleting, and inserting. ASP.NET
includes the following data source
controls:

Module Code & Module Title Slide Title SLIDE 42


Data-bound Controls

Data source control Description


ObjectDataSource Enables you to work with a business
object or other class and create Web
applications that rely on middle-tier
objects to manage data.
SqlDataSource Enables you to work with ADO.NET managed
data providers, which provide access to
Microsoft SQL Server, OLE DB, ODBC, or
Oracle databases.
AccessDataSource Enables you to work with a Microsoft Access
database.
XmlDataSource Enables you to work with an XML file, which is
especially useful for hierarchical ASP.NET server
controls such as the TreeView or Menu control.
SiteMapDataSource Used with ASP.NET site navigation.

Module Code & Module Title Slide Title SLIDE 43


Data-bound Controls

Data bound Description


control

GridView Displays data in a table and includes support for editing,


updating, sorting, and paging data without requiring code.

Menu Renders data in a hierarchical dynamic menu that can include


submenus.

Repeater Renders data in a list. Each item is rendered using an item


template that you define.

TreeView Renders data in a hierarchical tree of expandable nodes.

Module Code & Module Title Slide Title SLIDE 44


Quick Review Question

• Write SQL statements to perform join between tables.

Module Code & Module Title Slide Title SLIDE 45


Summary of Main Teaching Points

• SQL
• Insert
• Update
• Delete
• Select
• ADO.NET

Module Code & Module Title Slide Title SLIDE 46


Question and answer session

Q&A
Module Code & Module Title Slide Title SLIDE 47
What we will cover next

• Web Site Design Method

Module Code & Module Title Slide Title SLIDE 48

You might also like