1. Trang chủ
  2. » Công Nghệ Thông Tin

Làm việc với dữ liệu pps

28 248 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 28
Dung lượng 574,21 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The SqlDataSource control is used in conjunction with data controls to retrieve data from a relational database and to display or modify the data on a Web page with minimal coding.. Conn

Trang 1

Working with Data C hap

Accessing data from a data source is an important

aspect of a Web application Every Web application

that displays dynamic data needs to access data from

a data source Data from a data source can be

accessed either by implementing the data access

logic directly into the presentation layer or by

separating the data access logic from the

presentation layer by creating a new logical layer

known as Data Access Layer (DAL)

This chapter discusses the basics of accessing data

in Web applications It also discusses how to access

data from a database by using the presentation layer

In addition, it discusses how to implement DAL to

access data from a database

In this chapter, you will learn to:

 Identify basics of data access in Web

applications

 Access data by using the presentation layer

 Access data by using DAL

Objectives

Trang 3

Most Web applications are designed by using the multiple tier model known as the N-tier model Designing an application by using the N-tier model provides increased

performance and scalability This is because the functioning of each layer is completely hidden from other layers, and this makes it possible to change or update one layer without recompiling or modifying the other layers The following figure shows a basic

N-tier model

The Basic N-tier Model

The preceding figure shows three layers, which can further be divided into separate

layers

In a Web application, the presentation layer is the interface that appears when a user opens a Web page in the browser If you see the source code of the page, you would only see the code such as HTML, Javascript, and Cascading Style Sheets You would not be able to see the database queries, loops, calls to classes, or any behind-the-scenes

processing This is due to the fact that the presentation layer does not contain any data access code or program logic However, in case of small Web applications, you may find the database access layer merged with the presentation layer

Identifying the Basics of Data Access in Web

Applications

Presentation Layer

Trang 4

Consider a situation where a developer has implemented the data source controls to access data from the data source In such a situation, the database access is merged with the presentation layer This approach tightly couples the data access logic with the

presentation layer Therefore, this approach is not recommended because the data access logic, being tightly coupled, cannot be implemented by any other application

The business rules layer is also known as the business logic tier or the application tier It contains all the classes and the source code of the Web application The business rules layer allows you to separate the application logic from the user interface of a Web page It enables the programmer to easily search for a specific code because the code is not

cluttered with HTML or Javascript The business rules layer does not contain HTML or JavaScript code

You can implement the data access logic in the presentation layer However, this is not recommended because it clutters the data access code with the HTML and Javascript code In addition, it tightly couples the data access logic with the presentation layer This prevents the data access logic to be reused in other Web applications

All these limitations can be overcome by separating the data access logic from the

presentation layer It can be done by creating a new layer and implementing the data access logic in it This separate layer is referred to as the Data Access Layer (DAL) DAL

is typically implemented as a separate class library

Business Rules Layer

Data Access Layer

Trang 5

Most of the modern day Web applications need to manipulate data in databases This can

be done by implementing data access logic in applications If the data access logic is not

to be reused in any other application, it can be implemented in the presentation layer (ASP.NET pages) This can be done by using:

„ Data source controls

„ ADO.NET

Data source controls allow you to work with different types of data sources such as a database, an XML file, or a middle-tier business object Data source controls act as a bridge between the application and the data sources They can connect to the data sources and retrieve data without requiring you to write any code However, they do not provide the interface to display data To display the retrieved data, it is made available to the data controls or data-aware controls

Data controls are the controls that are designed exclusively for use with data sources Some of the data controls are GridView, DataList, ListView, and FormView controls

Data-aware controls are standard controls that can be used with databases, but are not exclusively for that purpose These controls can also be used without connecting them

to a data source Examples of such controls include DropDownList and ListBox

controls

Data source controls perform the following tasks:

„ Retrieve data from a data source and supply it to the data controls or data aware controls

„ Update the data source when edits take place

Data Source Controls in ASP.NET

ASP.NET provides various data source controls to access different types of data sources These controls are:

Accessing Data by Using the Presentation Layer

Data Source Controls

Trang 6

Note

SqlDataSource Control

The SqlDataSource control is generally used to access data from an SQL relational

database However, it can be used to access any database product for which there is a managed ADO.NET provider

A Provider is a class that communicates with a specific type of database or data store

The SqlDataSource control is used in conjunction with data controls to retrieve data from

a relational database and to display or modify the data on a Web page with minimal coding The following table lists some properties of the SqlDataSource control

Property Description

ProviderName Gets or sets the name of the ADO.NET provider that is used to

connect to an underlying data source

ConnectionString Gets or sets the connection string that is used to connect to an

underlying data source

SelectCommand Gets or sets the SQL string that is used to retrieve data from the

underlying data source

DeleteCommand Gets or sets the SQL string that is used to delete data from the

underlying data source

InsertCommand Gets or sets the SQL string that is used to insert data in the

underlying data source

UpdateCommand Gets or sets the SQL string that is used to update data in the

underlying data source

Properties of the SqlDataSource Control

You can retrieve data from a data source by using the SqlDataSource control, as shown in the following example:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:MusicConnectionString%>" SelectCommand="SELECT [album_name] FROM

[albums]"></asp:SqlDataSource>

Trang 7

Note

In the preceding example, the ConnectionString attribute is used to connect to the underlying data source and the SelectCommand attribute is used to retrieve the

album_name field values from the albums table

You can bind data controls to the SQLDataSource control by using the DataSourceID

property of the data controls

AccessDataSource Control

The AccessDataSource control works with Microsoft Access databases Like the

SqlDataSource control, the AccessDataSource control uses SQL queries to retrieve data However, unlike the SqlDataSource control, you do not set the ConnectionString

property in the AccessDataSource control Instead of the ConnectionString property, you need to set the location of the Access (.mdb) file in the DataFile property

The Access database to be accessed should be placed in the App_Data directory of the website and needs to be referenced by a relative path Using relative path for referencing the database provides security for data files because these files will not be served if requested directly by the client Web browser

You cannot access Access databases that are protected by a user name or password by using the AccessDataSource control This is because you cannot set the

ConnectionString property in the AccessDataSource control If you need to access an Access database that is protected by a user name or password, you need to use the

SqlDataSource control because it allows you to specify a complete connection string

You cannot set the ConnectionString property for the AccessDataSource control

because it is a read only property

You can bind data-bound controls to an AccessDataSource by using the DataSourceIDproperty of the data-bound control The following table lists some properties of the

AccessDataSource control

Property Description

ProviderName Gets the name of the ADO.NET provider that is used to connect to

Microsoft Access

Trang 8

Property Description

DataFile Gets or sets the location of the Microsoft Access file

SelectCommand Gets or sets the SQL string that is used to retrieve data from the

underlying data source

DeleteCommand Gets or sets the SQL string that is used to delete data from the

underlying data source

InsertCommand Gets or sets the SQL string that is used to insert data in the

underlying data source

UpdateCommand Gets or sets the SQL string that is used to update data in the

underlying data source

Properties of the AccessDataSource Control

You can retrieve data from an Access database by using the AccessDataSource control, as shown in the following example:

ObjectDataSource Control

The ObjectDataSource control represents a middle-tier object with data retrieval and update capabilities The ObjectDataSource control is used in conjunction with a

data-bound control to display or modify data on a Web page with minimal coding

Most of the complex Web applications follow the three-tier architecture In this

architecture, the presentation layer is separated from business logic, which is encapsulated

in business objects These business objects form a distinct layer between the presentation layer and the data layer The ObjectDataSource control enables developers to access data from business objects

Trang 9

The following table lists some properties of the ObjectDataSource control

SelectParameters Gets the parameters collection that contains the parameters

used by the SelectMethod property

InsertMethod Gets or sets the method or function that is invoked to insert

data

InsertParameters Gets the parameters collection that contains the parameters

used by the InsertMethod property

UpdateMethod Gets or sets the method or function that is invoked to update

data

UpdateParameters Gets the parameters collection that contains the parameters

used by the UpdateMethod property

DeleteMethod Gets or sets the method or function that is invoked to delete

data

DeleteParameters Gets the parameters collection that contains the parameters

used by the DeleteMethod property

Properties of the ObjectDataSource Control

You can retrieve data from business objects by using the ObjectDataSource control, as shown in the following example:

Trang 10

that the ObjectDataSource is working with The <SelectParameters> element passes the employee ID as parameter because it is required by the GetEmployee method

XmlDataSource

The XmlDataSource control presents XML data to data-bound controls The

XmlDataSource control can be used by data-bound controls to display hierarchical as well

as tabular data The XmlDataSource control loads data from an XML file that is specified

by the DataFile property The XmlDataSource control can also load XML data in string form by using the Data property You can bind data-bound controls to an XmlDataSource control by using the DataSourceID property of the data-bound control

The following table lists some properties of the XmlDataSource control

Property Description

DataFile Specifies the filename of the XML file that is to be bound with the

XmlDataSource control

Data Gets or sets the block of xml data that is to be bound with the

XmlDataSource control in string form

Properties of the XmlDataSource Control

You can retrieve data from an XML file by using the XmlDataSource control, as shown in the following example:

<asp:xmldatasource id="XmlDataSource1" runat="server"

navigation hierarchy The SiteMapDataSource enables Web server controls that are not specifically site navigation controls, such as the TreeView and Menu controls, to bind to site map data These controls can be used to display a site map to navigate a site The SiteMapDataSource control can be bound to the navigational controls by using the

DataSourceID property of the navigational controls The following table lists some properties of the SiteMapDataSource control

Trang 11

Property Description

SiteMapProvider Gets or sets the name of the site map provider

StartingUrlNode Sets the URL in the sitemap that will be considered as the root node

ShowStartingNode Indicates whether to show starting node or not It can only be set to

True or False Properties of the SiteMapDataSource Control

You can retrieve data from a sitemap file by using the SiteMapDataSource control, as shown in the following example:

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

In the preceding example, the SiteMapProvider attribute is not defined Therefore, the default provider, XMLSiteMapProvider, will be used When you use the default provider, the SiteMapDataSource control will always look for the Web.sitemap file in the root folder of the application However, if your application has a sitemap file with a name other than the default name, Web.sitemap, you need to use a custom provider for the SiteMapDataSource control You can use the custom provider by adding it in the

web.config file, as shown in the following example:

In addition to modifying the web.config file, you need to set the SiteMapProvider

property of the SiteMapDataSource control to the custom provider, as shown in the following example:

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"

SiteMapProvider="MusicManiaSiteMap " />

In addition to the preceding data source controls, ASP.NET provides the

LinqDataSource control that allows you to access data from wide variety of data

sources such as a database, data-source classes, and in-memory collections

Trang 12

Displaying Data in Data-Bound Web Server Controls

Data-bound Web server controls are the controls that can be bound to a data source control to display and modify data in a Web application Data-bound Web server controls are composite controls that combine other Web controls, such as Label and TextBox controls, into a single unit In addition to displaying and modifying data, data-bound controls enable you to customize the layout of the control using templates ASP.NET provides various data-bound Web server controls Some of them are:

business objects that expose data However, you cannot change the layout of the

GridView control

The GridView control provides various properties that can be set to display data in the desired format The following table lists some properties of the GridView control

Property Description

DataSource Gets or sets the object from which the control retrieves the items

DataSourceID Gets or sets the ID of the control from which the GridView control

retrieves the data items

AllowPaging Gets or sets the value indicating whether paging is enabled

AllowSorting Gets or sets the value indicating whether sorting is enabled

AlternatingRowStyle Gets the reference of the object that enables you to set the

appearance of the alternate rows

Trang 13

Property Description

SelectedIndex Gets or sets the index of the selected row

SelectedRow Gets the reference of the object that represents the selected row

SelectedValue Gets the data key value of the selected row

Properties of the LinqDataSource Control

Task 6.1: Retrieving Data from a Data Source by Using the SqlDataSource Control

DetailsView Control

The DetailsView control allows you to display, edit, insert, or delete a single record at a time By default, the DetailsView control displays each field of a record in a new line It can display only one record at a time It is generally used in a master/detail scenario In such a scenario, the DetailsView control is used to update or delete records that are

selected in the master control The DetailsView control can also be used to add a new record

The following table lists some properties of the DetailsView control

Property Description

DataSource Gets or sets the object from which the control retrieves the items

DataSourceID Gets or sets the ID of the control from which the DetailsView control

retrieves the data items

AllowPaging Gets or sets the value indicating whether paging is enabled

AlternatingRowStyle Gets the reference of the object that enables you to set the

appearance of the alternate rows

SelectedValue Gets the data key value of the current record

Properties of the DetailsView Control

Trang 14

Note

FormView Control

Similar to the DetailsView control, the FormView control allows you to work with a single record at a time However, there is a difference between the FormView and the DetailsView control The DetailsView control displays the record in a tabular format with each field of the record in a new row whereas the FormView control provides you with the flexibility to modify the layout for displaying the record You can modify the layout

by creating a new template or by modifying the existing template The template can contain formatting, controls, and binding expressions The FormView control is also used

in master/detail scenarios

The FormView control provides various built-in templates such as ItemTemplate,

InsertItemTemplate, and EditItemTemplate that allow you to provide a different

interface for performing different operations such as view, insert, and edit Similarly, the EmptyDataTemplate enables you to specify a template to display when the data

source returns no data

The following table lists some properties of the FormView control

Property Description

DataSource Gets or sets the object from which the control retrieves the items

DataSourceID Gets or sets the ID of the control from which the FormView control

retrieves the data items

AllowPaging Gets or sets the value indicating whether paging is enabled

SelectedValue Gets the data key value of the current record

Properties of the FormView Control

DataList Control

The DataList control allows you to display rows of database information in a

customizable format You can create the format for displaying data by defining templates You can create templates for items, alternating items, selected items, and edit items

Ngày đăng: 12/08/2014, 17:21

TỪ KHÓA LIÊN QUAN

w