Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

New Ways to Handle Data

ASP.NET 1.0 introduced some revolutionary ways to retrieve and manipulate data. ADO.NET (introduced in .NET 1.0) enabled you to grab data from data stores in an intuitive manner and then store the retrieved data in objects like the new DataSet object. Although the process was revolutionary, it was complicated and contained numerous possible pitfalls.

ASP.NET 2.0 introduces data source controls to bridge the gap between your data stores and the data-bound controls at your disposal. These new data controls not only enable you to retrieve data from various data stores, but they also let you easily manipulate the data (using paging, sorting, editing, and filtering) before the data is bound to an ASP.NET server control.

This chapter presents the new data source controls as well as some of the new data-bound controls that you can use to display retrieved data.

The New Data Source Controls

Before the introduction of the new data source controls, just retrieving data from a data store and displaying it in a DataGrid control was a multistep process. This is illustrated in Listing 4-1.

Listing 4-1: Binding a DataGrid control in ASP.NET 1.0/1.1 (VB only)

Dim conn As SqlConnection = New SqlConnection(“server=’localhost’; trusted_connection=true; Database=’Northwind’”)

Dim cmd As SqlCommand = New SqlCommand(“Select * From Customers”, conn) conn.Open()

Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)

Dim ds As New DataSet

da.Fill(ds, “Customers”)

DataGrid1.DataSource = ds

DataGrid1.DataBind()

Chapter 4

In this example, you can see the many steps required just to retrieve the Customers table from SQL Server. First, a SqlConnection object is created to connect to SQL Server. Next, a SqlCommand object is created to pass in the command to the database from which data is to be retrieved. After the connection is opened, SqlDataAdapter is used to get the data and then fill the DataSet object that is created. After the DataSet object is in place and filled with the data from the Customers table, the DataSet object is bound to the DataGrid control with a DataBind() command.

Because ASP.NET 2.0 is backward compatible, the example shown in Listing 4-1 works just the same as it did in earlier versions of ASP.NET. Now, however, you can use one of the six new data source controls that have been introduced in ASP.NET 2.0. These controls provide a declarative way to connect to data stores and retrieve specific data. Working with the new data source controls is considerably easier than any previous methods. In most cases, you simply need a single line of code to get at the data you want and, in some cases, zero lines of code are required.

The new data source controls include some specifically designed to work with Microsoft SQL Server, Microsoft Access, and many other types of data stores. The following table details the new data source controls available in ASP.NET 2.0.

Data Source Control

Description

 

 

SqlDataSource

Enables you to work with any SQL-based database, such as Microsoft

 

SQL Server or Oracle.

AccessDataSource

Enables you to work with a Microsoft Access file (.mbd).

ObjectDataSource

Enables you to work with a business object or a Visual Studio 2005 data

 

component.

XmlDataSource

Enables you to work with the information from an XML file or an XML

 

source (for example an RSS feed).

SiteMapDataSource

Enables you to work with the hierarchical data represented in the site

 

map file (.sitemap). These files and how to bind to them are discussed

 

in Chapter 5.

DataSetDataSource

Enables you to work with data that is represented in a DataSet object.

 

 

These data source controls connect to the assigned data store, retrieve the data, and perform any manipulations on the data that you specify using control attributes. The data source control does all the sorting, paging, and editing of the data. It also works with any data-bound controls such as the new GridView control to perform automatic databinding without any work on your part.

The Data-Bound Ser ver Controls

ASP.NET 2.0 provides a large collection of new data-bound server controls that can be used in conjunction with the new data source controls to display retrieved data in the browser. Although you probably recognize many controls from ASP.NET 1.0/1.1, you also meet some new server controls — such as the

76