Vous êtes sur la page 1sur 29

Developing Web Applications Using ASP.

NET
Objectives

In this session, you will learn to:


Access data by using the presentation layer Access data by using Data Access Layer (DAL) Identify the basics of Language-Integrated Query (LINQ) Access data from disparate data sources by using LINQ

Ver. 1.0

Slide 1 of 29

Developing Web Applications Using ASP.NET


Using ADO.NET

You can access data from various data sources such as MS- Access, SQL Server, and Oracle by using ADO.NET. To access data from a database by using ADO.NET, you need to use certain objects, such as:
Connection objects Command objects Data Reader objects Dataset objects

Ver. 1.0

Slide 2 of 29

Developing Web Applications Using ASP.NET


Using ADO.NET (Contd.)

Connection objects are used to make a connection to the data source. When creating a connection object, you need to set the ConnectionString property that defines all the information required to find the data source, log in, and choose an initial database. Some of the important connection string properties are:
Data Source Initial Catalog Integrated Security

After specifying the connection string, you need to open the connection.

Ver. 1.0

Slide 3 of 29

Developing Web Applications Using ASP.NET


Using ADO.NET (Contd.)

After a connection has been established with the data source, you can execute commands and return results from the data source by using a command object. To execute SQL statements, you need to create an instance of the SqlCommand class present in the System.Data.SqlClient namespace.

The command object specifies:


The SQL statement that needs to be executed. The connection that needs to be used to execute the statement.

You can execute SQL statements or stored procedures through command objects.

Ver. 1.0

Slide 4 of 29

Developing Web Applications Using ASP.NET


Using ADO.NET (Contd.)

After defining the command object, you need to execute it to get the data from the data source. The data returned by executing the command object needs to be stored in another object. The DataReader object:
Can be used to store the data returned by executing the command object. Supports forward-only read-only access to the results returned by executing the command object. Works in a connected environment and requires an open connection for its working.

To create a DataReader object, you use the ExecuteReader() method of the command object.

Ver. 1.0

Slide 5 of 29

Developing Web Applications Using ASP.NET


Using ADO.NET (Contd.)

A dataset:
Is a memory-based relational representation of data. Is a disconnected, cached set of records that are retrieved from a database. Acts like a virtual database containing tables, rows, and columns. Is extensively used to retrieve data from data sources in Web applications. Optimizes the use of server resources because the connection is not used for a long time.

The dataset object requires the data adapter to retrieve data from the data source. You can retrieve data from a data source by using a dataset.

Ver. 1.0

Slide 6 of 29

Developing Web Applications Using ASP.NET


Activity 6.1: Accessing Data by Using the Presentation Layer

Problem Statement:
The management at MusicMania wants that the website should have the options that would enable users to view details of music albums, view album reviews, and add album reviews. The information regarding the reviews and the music albums should be accessed from the database. For this, you need to modify the Albums page so that it retrieves the album details from the database. The Albums page should also have a button to add reviews. When a user clicks this button, he/she is directed to the AddReviews Web page, which should be created to enable the users to add reviews to the database. In addition, the management wants that the performance of the website should be improved. To improve the performance of the website, you need to implement output caching on the Home page.

Ver. 1.0

Slide 7 of 29

Developing Web Applications Using ASP.NET


Activity 6.1: Accessing Data by Using the Presentation Layer (Contd.)

Solution:
To display the album details and add reviews to the database, you need to perform the following tasks:
1. 2. 3. 4. 5. Modify the Albums page. Add a new Web form. Design the new Web form. Modify the Home Page. Execute the application.

Ver. 1.0

Slide 8 of 29

Developing Web Applications Using ASP.NET


Accessing Data by Using DAL

DAL:
Is a layer of code that provides simplified access to the data stored in persistent storage such as a database. Is used by other program modules to access and manipulate data within the data source without dealing with the complexities involved in accessing the data. Separates the data access code such as creating a connection to the database, issuing SELECT, INSERT, UPDATE, and DELETE commands from the application logic.

Ver. 1.0

Slide 9 of 29

Developing Web Applications Using ASP.NET


Advantages of Using DAL

DAL separates the data access code from the rest of the code. This provides the following advantages:
It minimizes the impact of a change in database provider. It also minimizes the impact of a change in data representation such as a change in database schema. It simplifies testing and maintenance. It increases code reusability because the data access code can be generalized to work across applications.

Ver. 1.0

Slide 10 of 29

Developing Web Applications Using ASP.NET


Creating a DAL

You can create a DAL by creating a class library and writing all the data access functions in that library. All the data access functions written in the class library should be generic because a DAL is created to separate the data access logic from the application layer. The functions written in a DAL should be reusable.

Ver. 1.0

Slide 11 of 29

Developing Web Applications Using ASP.NET


Activity 6.2: Creating a DAL

Problem Statement:
To minimize the impact of the change in database provider and to simplify testing and maintenance, your team leader wants you to separate the data access code from the application logic by creating a DAL. However, you have never created a DAL. Therefore, in the first phase the team leader has asked you to create a DAL only for the Reviews table. You also need to implement this DAL in the AddReviews Web page.

Ver. 1.0

Slide 12 of 29

Developing Web Applications Using ASP.NET


Activity 6.2: Creating a DAL (Contd.)

Solution:
To create a DAL, and then use it in the AddReviews page, you need to perform the following tasks:
1. Create a DAL. 2. Modify the AddReviews.aspx file. 3. Execute the application.

Ver. 1.0

Slide 13 of 29

Developing Web Applications Using ASP.NET


Identifying the Basics of LINQ

LINQ:
Provides a standard way to access data from disparate data sources. Enables you to access data from disparate data sources by writing a standard query, called LINQ query. Provides various facilities such as stronger typing, compile-time checking, and improved debugger support.

Ver. 1.0

Slide 14 of 29

Developing Web Applications Using ASP.NET


Identifying the Basics of LINQ (Contd.)

The following figure shows a model of .NET LINQ.

Ver. 1.0

Slide 15 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query

A LINQ query:
Is an expression that retrieves data from a data source. Enables you to write a standard query expression to retrieve data form disparate data sources.

To access data by using LINQ, you need to perform the following steps:
1. Specify the data source. 2. Create the query.
3. Execute the query.

Ver. 1.0

Slide 16 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Specifying the data source:


The data source in a LINQ query can be an XML file, an array, a collection, or a database. If the data source that you want to use is an array, you can specify the data source by using the following statement:
string[] customers = new string[2] {John, Mary};4

Ver. 1.0

Slide 17 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Creating the query:


A query describes what information is to be retrieved from a specified data source. It must include the from and select clauses to retrieve data, as shown in the following query:
var query = from <type of the value returned> cus in customers select cus;

Ver. 1.0

Slide 18 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Executing the query:


In LINQ, the query variable only stores the query. The actual execution of the query is deferred until the query variable is iterated by using the foreach loop, as shown in the following code snippet:
//Query execution foreach (var cus in query) { Response.Write(cus); }

Ver. 1.0

Slide 19 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

In addition to selecting data, a query can perform the following operations on data:
Filtering Ordering Grouping Joining

Ver. 1.0

Slide 20 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Filtering:
To retrieve data depending on certain criteria or condition, you need to filter the data. Filtering causes the query to return only the data that matches with the specified condition. The condition to retrieve specific data can be specified by using the where clause. For example, the following code snippet returns the data of those customers who live in London:
var query = from <type of the value returned> cus in customers where cus.City == "London" select cus;

Ver. 1.0

Slide 21 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Ordering:
LINQ queries can also be used to retrieve data and display it in a sorted manner. This can be done by using the orderby clause. The orderby clause enables a user to sort the retrieved data in ascending or descending order. For example, the following code snippet returns the data for the customers who live in London. The data is sorted by customer names in ascending order:
var query = from <type of the value returned> cus in customers where cus.City == "London" orderby cus.Name descending select cus;

Ver. 1.0

Slide 22 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Grouping:
You can also retrieve data and group it by using the group clause. For example, the following code snippet returns the data for all the customers, grouped on the basis of the cities they live in:
var query = from <type of the value returned> cus in customers group cus by cus.City;

Ver. 1.0

Slide 23 of 29

Developing Web Applications Using ASP.NET


Understanding the LINQ Query (Contd.)

Joining:
It is possible that the data you need to retrieve exists in more than one table. To retrieve the required information, you need to join both the tables. This can be done by using the join clause, as shown in the following code snippet:
var query = from <type of the value returned> cus in customers join <type of the value returned> ord in Order on cus.order_id equals ord.order_id Select new {CustomerName=cus.Name, OrderAmount=Order.Amount};

Ver. 1.0

Slide 24 of 29

Developing Web Applications Using ASP.NET


Accessing Data from Disparate Data Sources

In Web applications, the data that is to be displayed on the Web pages may exist in disparate data sources. These data sources can be objects, databases, or XML files. You can access data from these data sources either by using the LINQ queries or by using the LINQ data source control.

Ver. 1.0

Slide 25 of 29

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Queries

LINQ provides a standard query for retrieving data from disparate data sources. These queries can be used to access data from:
Objects Databases XML files

Ver. 1.0

Slide 26 of 29

Developing Web Applications Using ASP.NET


Accessing Data by Using the LINQ Queries (Contd.)

Data from objects can be accessed by using LINQ to Objects. LINQ to Objects provides an easy way to create a query for accessing data from an object collection such as an ArrayList by writing foreach loops. To use LINQ to query an object collection, you need to declare a range variable. The type of the range variable should match the type of the objects in the collection, as shown in the following code snippet:
var query = from customers cust in arrList select cust;

Ver. 1.0

Slide 27 of 29

Developing Web Applications Using ASP.NET


Summary

In this session, you learned that:


The basic objects used to access data from a database by using ADO.NET are:
Connection objects Command objects Data Reader objects Dataset objects

DAL is a layer of code that provides simplified access to the data stored in persistent storage such as a database. LINQ provides a standard way to access data from disparate data sources.

Ver. 1.0

Slide 28 of 29

Developing Web Applications Using ASP.NET


Summary (Contd.)

The following steps are performed when accessing data by using LINQ:
Specify the data source. Create the query. Execute the query.

LINQ provides a standard query for retrieving data from disparate data sources. LINQ queries can be used to access data from:
Objects Databases XML files

You can also use the LINQ data source control to access data from disparate data sources.

Ver. 1.0

Slide 29 of 29

Vous aimerez peut-être aussi