共用方式為


What's New in ASP.NET Data Access

ASP.NET version 2.0 continues to offer managed data access using ADO.NET and managed classes for XML. But ASP.NET 2.0 also includes new features to make data access in Web pages easier to implement and manage.

Highlights of the new data access model in ASP.NET 2.0 are:

  • A simplified data-binding model that uses the new data-source controls and requires no code.

  • Two-way data binding that can both read and update data.

  • New and enhanced data controls to display and update data.

  • Improved support for using middle-tier business object for data access.

Backward Compatibility

An important feature in the new data access model is that existing ASP.NET applications continue to work without changes. The data-binding model used in ASP.NET 1.0 and ASP.NET 1.1 is still supported, and you can still use ADO.NET for data access. Data controls such as the DataList and Repeater controls have been enhanced to work with the new data source controls and to take advantage of automatic data binding, but still work when bound directly to a data source such as a dataset.

Data Binding Using Data Source Controls

In ASP.NET 1.0 and ASP.NET 1.1, you display data on a page using a data control (for example, a Repeater or DataList control). Data control is bound to a data source, such as an ADO.NET dataset filled by an ADO.NET data adapter. You add these data components to the page yourself, and then write code to populate the data source and bind the data controls to the data.

Data Source Controls

In ASP.NET version 2.0, the tasks associated with data access in a Web page are provided by data source controls. A data source control provides a single object in which you can declaratively define the following:

  • Connection information.

  • Query information, such as a SQL statement or the names of methods to invoke on an object.

  • Insert, update, and delete commands to modify data.

  • Parameters for queries, data modification, and filtering.

  • Options such as paging and caching (if the data source supports these options).

When you use data source controls, you do not need to write code for data access, such as calls to the DataBind method. (You can still call the DataBind method explicitly to refresh bound data if required in your application.)

ASP.NET provides data source controls for different types of data stores, including SQL-based databases (SQL Server, OLE DB, and Open Database Connectivity (ODBC) databases), XML data, and business objects. All data source controls expose the same interface to data controls on the page, so that controls such as the DataList and Repeater controls, and the new GridView control can bind the same way to any data source control, regardless of the underlying data store it represents. The result is that you can use the full array of ASP.NET data controls to work with a wide variety of data sources.

Types of Data Source Controls

ASP.NET includes the data source controls listed in the following table.

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. Supports advanced sorting and paging scenarios unavailable with the other data source controls. For more information, see ObjectDataSource Web Server Control.

SqlDataSource

Enables you to work with Microsoft SQL Server, OLE DB, ODBC, or Oracle databases. When used with SQL Server, supports advanced caching capabilities. The control also supports sorting, filtering, and paging when data is returned as a DataSet object. For more information, see SqlDataSource Web Server Control.

AccessDataSource

Enables you to work with a Microsoft Access database. Supports sorting, filtering, and paging when data is returned as a DataSet object. For more information, see AccessDataSource Web Server Control.

XmlDataSource

Enables you to work with XML data, especially for hierarchical ASP.NET server controls such as the TreeView or Menu controls. Supports filtering capabilities using XPath expressions and enables you to apply an XSLT transformation to the data. The XmlDataSource enables you to update data by saving the entire XML document with changes. For more information, see XmlDataSource Web Server Control.

SiteMapDataSource

Used with ASP.NET site navigation. For more information, see ASP.NET Site Navigation Overview.

Enhanced Data Controls

All data controls in ASP.NET 2.0 have been enhanced to work with data source controls. As a result, you can take advantage of automatic data binding in any data-bound control. This includes list controls such as the DropDownList control and composite data-bound controls such as the DataList control.

In addition, ASP.NET 2.0 introduces new data controls that provide additional functionality, as described in the following table.

  • GridView
    The GridView control is the successor to the ASP.NET DataGrid control. The GridView control automates many of the features of the DataGrid control, so that you do not need to write code for editing, sorting, or paging. For situations in which you want to customize the control's behavior, you can continue to use an object model similar to that of the DataGrid control. For more information, see GridView Web Server Control.
  • DetailsView
    The DetailsView control displays one record at a time and enables you to edit, delete, and insert records. You can also page through multiple records. For more information see DetailsView Web Server Control.
  • FormView
    The FormView control is similar to the DetailsView control, but enables you to define a free-form layout for each record. The FormView control is like a DataList control for a single record. For more information, see FormView Web Server Control.
  • TreeView
    The TreeView control renders data in a hierarchical tree of expandable nodes. For more information, see TreeView Web Server Control.
  • Menu
    The Menu control renders data in a hierarchical dynamic menu including "flyout" submenus. For more information, see Menu Web Server Control.

DataGrid Control

You can continue to use the DataGrid control, although it is superseded by the GridView control. Existing pages that use the DataGrid control will work as is. As with other data controls, the DataGrid control has been enhanced to interact with data source controls.

Declarative Parameters

For situations in which you need to pass parameters to a data source control, such as for a filtered query or update command, you can declaratively define parameters as part of the data source control. These parameter objects enable you to supply values from server control properties, Session and Application variables, cookies, QueryString values, and user profile properties to parameterized data operations. Using parameters enables filtering and master-details scenarios with little or no custom code.

For more information, see Using Parameters with Data Source Controls.

Simplified Data Binding

The syntax used for data binding within templates has been simplified. In ASP.NET 1.0 and ASP.NET 1.1, you have to specify the Eval method of the DataBinder class and identify the DataItem from the container and the field to bind, you can now specify just the Eval method and the name of the field to bind. You can optionally include a format for the bound field based on the System.String.Format method.

A Bind method has also been added for two-way data-binding scenarios in edit item templates and update item templates. For more information on the Eval and Bind methods, see Data-Binding Expression Syntax.

When binding to XML data, you can make use of the XPath and XPathSelect methods to bind to the results of an XPath query against the source XML. For more information, see Binding a Tabular Control to the XmlDataSource Control.

The App_Data Folder

To improve the security of the data used by your ASP.NET application, a new subfolder named App_Data has been added for ASP.NET applications. Files stored in the App_Data folder are not returned in response to direct HTTP requests, which makes the App_Data folder the recommended location for data stored with your application, including .mdf (SQL Server Express Edition), .mdb (Microsoft Access), or XML files. Note that when using the App_Data folder to store your application data, the identity of your application has read and write permissions to the App_Data folder.

See Also

Other Resources

Accessing Data with ASP.NET