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

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

Đ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

Working with Data Chapter 6 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 NIIT Working with Data 6.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 6.4 Working with Data NIIT 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 NIIT Working with Data 6.5 N ot e 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:  SqlDataSource  AccessDataSource  ObjectDataSource  XmlDataSource  SiteMapDataSource Accessing Data by Using the Presentation Layer Data Source Controls 6.6 Working with Data NIIT N ot e 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> NIIT Working with Data 6.7 N ot e N ot e 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 DataSourceID property 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. 6.8 Working with Data NIIT 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: <asp:AccessDataSource id="AccessDataSource1" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="SELECT LastName, FirstName FROM Employees"> </asp:AccessDataSource> In the preceding example, the DataFile attribute is used to specify the Access database file. The SelectCommand attribute is used to specify the query that retrieves the LastName and FirstName field values from the Employees table. 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. NIIT Working with Data 6.9 The following table lists some properties of the ObjectDataSource control. Property Description TypeName Identifies the name of the class that the ObjectDataSource works with. SelectMethod Gets or sets the method or function that is invoked to retrieve data. 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: <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetEmployee" typename="EmployeeLogic" > <selectparameters> <asp:querystringparameter name="EmployeeID" querystringfield="empid"/> </selectparameters> </asp:objectdatasource> In the preceding example, the SelectMethod attribute sets the method, GetEmployee, which is invoked to retrieve data. The TypeName attribute defines the name of the class 6.10 Working with Data NIIT 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" datafile="books.xml" /> In the preceding example, the datafile attribute specifies the filename of the XML file that is to be bound with the XmlDataSource control. SiteMapDataSource Control The SiteMapDataSource control provides navigation data to the navigational controls. It obtains navigation data from a site map. The navigation data includes information about the pages on your website, such as the URL, title, description, and location in the 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.

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

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan