Академический Документы
Профессиональный Документы
Культура Документы
Introduction-1
The key issue with ASP.NET data binding is a lack of a higher-level and possibly declarative model for data fetching and manipulation (edit, insert, and delete). As a result, an ASP.NET 1.x data access layer is boring to write and requires hundreds of lines of code even for relatively simple scenarios.
Introduction-2
The ASP.NET 2.0 data source model addresses this problem. To simplify the data binding mechanism, the architecture of data-bound controls now supports a new family of data componentsthe data source componentswhich in turn support a declarative model of binding.
Introduction-3
The data source control represents a source of data that returns and accepts data over a well-known stream such as SQL, XML, DataSet, and custom formats such as the Microsoft Excel worksheets.
ASP.NET 2.0 adds a new property to all data-bound controls so that any control can be successfully bound to a new data source control. The new property,DataSourceId, matches the name of a data source control defined in the same page. The following code snippet shows how to list the employees in the Northwind Employees database. The data-bound control used for the output is the Repeater.
<asp:SqlDataSource runat="server ID="MySource ConnectionString="SERVER=;DATA BASE=northwind;UID=; DataSourceMode="DataSet SelectCommand="SELECT firstname, lastname FROM employees />
<asp:Repeater runat="server ID="data DataSourceId="MySource > <ItemTemplate> <%# Eval(ProductName) %> <%# Eval(Price) %> <br> </ItemTempate> </asp:Repeater>
The DataSourceControl abstract class serves as the base class for all data source controls and defines the interface between data-bound controls and the underlying data. A data source control exposes the contents of its underlying data source through a set of properties and methods. Some of these members are specific to the control; others are common to all source controls and are defined as part of the IDataSource interface.
Tabular data source controls are AccessDataSource, DataSetDataSource, Object-DataSource, and SqlDataSource.
SqlDataSource control
The SqlDataSource control is a data source control that represents a connection to a relational data store such as SQL Server or Oracle or any data source accessible through OLE DB and ODBC bridges.
You set up the connection to the data store using two main properties,ConnectionString and ProviderName. The former represents the connection string and contains enough information to open a session with the underlying engine. The latter specifies the namespace of the ADO.NET managed provider to use for the operation. The ProviderName property defaults System.Data.SqlClient, which means that the default data store is SQL Server.
Continue..
The DataSourceMode property controls how the Select command retrieves data. The DataSourceMode property accepts values defined by the SqlDataSourceMode enumeration DataSet and DataReader.
Code Usage
<asp:SqlDataSource runat="server ID="MySqlSource ConnectionString="SERVER=;DATAB ASE=northwind;UID=; SelectCommand="SELECT * FROM employees />
Code Usage
The following code shows how to use the AccessDataSource control to open an .mdb file and bind its content to a drop-down list control. <asp:AccessDataSource runat="server ID="MyAccessSource DataFile="nwind.mdb" SelectCommand="SELECT * FROM Customers /> Select a Customer: <asp:DropDownList runat="server DataSourceId="MyAccessSource />
Several features of the AccessDataSource control are inherited from the base class, SqlDataSource. In fact, the Access data source control is basically a SQL data source control optimized to work with Access databases. The ShareMode property controls how the Access data source opens thedatabase and determines what data operations can be performed. The defaultvalue is Read, which means that only the Select operation can be accomplished
In read mode, the CanDelete, CanInsert, and CanUpdate properties all return false, thus blocking the corresponding data operations. To enable read/write operations, set ShareMode to ReadWrite.
Code Usage
<asp:GridView runat="server ID="grid DataSourceId="MyDataSetSource AutoGenerateColumns="true"> </asp:GridView> <asp:DataSetDataSource runat="server ID="MyDataSetSource DataFile="data.xml />
ASP.NET 2.0 provides richer support for site maps. You start by creating a configuration file named app.sitemap in the root of the Web application. The file describes the relationship between pages on the site.
An XML node is an instance of the XmlNode class; the complete hierarchy is an instance of the XmlDocument class. The XML data source supports both read-only and read-write scenarios. The XmlDataSource control can accept XML input data as a relative or absolute filename assigned to the DataFile property or as a string containing the XML content assigned to the Data property.
The control exposes that data through the IDataSource or the IHierarchicalDataSource interface. In general, the XmlDataSource control is commonly bound to a hierarchical control, such as the TreeView.
Code Usdage(1)
To understand how the XML data source works, consider the following small XML file, named data.xml:
<warehouse> <department name="dairy deptid="111"> <category name="yogurt categoryid="2222"> <product name="horizon sku="3333-3333"/> </category> </department> </warehouse>
Code Usage(2)
<asp:XmlDataSource runat="server ID="MyXmlSource DataFile="data.xml /> <asp:TreeView runat="server DataSourceId="MyXmlSource"> <DataBindings> <asp:TreeNodeBinding Depth="0 DataMember="Department TextField="Name ValueField="DeptId /> <asp:TreeNodeBinding Depth="1 DataMember="Category TextField="Name ValueField="CategoryId /> <asp:TreeNodeBinding Depth="2 DataMember="Product TextField="Name ValueField="SKU /> </DataBindings> </asp:TreeView>
It stores objects whose base class is Parameter. The Parameter class represents a parameter in a parameterized query, filter expression, or command executed by a data source control.
Code Usage(1)
<asp:SqlDataSource runat="server ID="MySource ConnectionString="SERVER=;DATAB ASE=northwind;UID=;" DataSourceMode="DataSet SelectCommand="SELECT firstname, lastname FROM employees WHERE employeeid > @MinID">
Code Usage(2)
<SelectParameters> <asp:ControlParameter Name="MinID ControlId="EmpID PropertyName="Text /> </SelectParameters> </asp:SqlDataSource>
The query contains a placeholder named @MinID. The data source control automatically populates the placeholder with the information returned by the ControlParameter objectone of the supported parameter types in ASP.NET 2.0. The
Parameter Types
How a parameter is bound to a value depends on the type of the parameter.
ProfileParameter: Gets the parameter value from the specified property name in the profile object created from the applications personalization scheme.
QueryStringParameter: Gets the parameter value from the specified variable in the request query string. SessionParameter: Sets the parameter value based on the content of the specified Session slot.
Each parameter class has a Name property and a set of properties specific to its role and implementation.
Caching Behavior
Data caching is crucial to any realistic Web application and an important feature to optimize to make the application run faster. Caching is also an important aspect of data source controls. A data source control retrieves data that will be made available to other components within the application. When multiple pages need to access this information, an up-to-date cache provides for a significantly faster response.
You can instruct the SqlDataSource control to cache the results of a query for a certain amount of timebut only if the data source mode is DataSet. The Sql-DataSource control provides automatic caching using a time-based cache expiration policy. It can also support an expiration policy based on the new SqlCacheDependency component.
The CacheDuration property specifies the number of seconds before the contents of the data source are discarded, to be reloaded on the next request.
Code
<asp:SqlDataSource runat="server ConnectionString="DSN=MyData" ProviderName="System.Data.Odbc" SelectCommand="SELECT * FROM products" EnableCaching="true" CacheDuration="3600 />
Code
<asp:SqlDataSource runat="server ConnectionString="SERVER=;DATABASE=p ubs;UID=;" SelectCommand="sp_getdata" EnableCaching="true" CacheDuration="3600 SqlCacheDependency="pubs:Authors;pubs:TitleA uthor />