the data source_controls

Upload: amessi-young

Post on 03-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 The Data Source_controls

    1/51

    The Data Source

    Object Model

  • 7/29/2019 The Data Source_controls

    2/51

    Introduction-1

    The key issue with ASP.NET data

    binding is a lack of a higher-level and

    possibly declarative model for datafetching and manipulation (edit, insert,

    and delete). As a result, an ASP.NET

    1.x data access layer is boring to writeand requires hundreds of lines of code

    even for relatively simple scenarios.

  • 7/29/2019 The Data Source_controls

    3/51

    Introduction-2

    The ASP.NET 2.0 data source model

    addresses this problem. To simplify the

    data binding mechanism, thearchitecture of data-bound controls now

    supports a new family of data

    componentsthe data sourcecomponentswhich in turn support a

    declarative model of binding.

  • 7/29/2019 The Data Source_controls

    4/51

    Introduction-3

    The data source control represents a

    source of data that returns and accepts

    data over a well-known stream suchas SQL, XML, DataSet, and custom

    formats such as the Microsoft Excel

    worksheets.

  • 7/29/2019 The Data Source_controls

    5/51

    The Rationale of Data Source

    Components The data binding model of ASP.NET 2.0

    provides two ways of connecting

    programmatically (as in ASP.NET 1.x)and declaratively (using data source

    components)

  • 7/29/2019 The Data Source_controls

    6/51

    Codeless Data Binding

    In ASP.NET 2.0 developers can set up

    sophisticated Web sites without knowing a lot

    of SQL or the page life cycle. Codeless data

    binding, and the data source model in

    particular, also lets developers work together,

    each contributing the best of his skills.

    ASP.NET 2.0 data binding enables pagedevelopers to implement data binding

    scenarios, including displaying, editing,

    paging, and sorting data, with no code.

  • 7/29/2019 The Data Source_controls

    7/51

    ASP.NET 2.0 adds a new property to all

    data-bound controls so that any control

    can be successfully bound to a newdata source control. The new

    property,DataSourceId, matches the

    name of a data source control defined inthe same page.

    The following code snippet shows how

    to list the employees in the NorthwindEmployees database. The data-bound

    control used for the output is the

    Repeater.

  • 7/29/2019 The Data Source_controls

    8/51

  • 7/29/2019 The Data Source_controls

    9/51

  • 7/29/2019 The Data Source_controls

    10/51

    Defining a Standard Set of Data

    Operations A data source component is a data control

    that can implement some or all of a standard

    set of data operations. Whether a controlimplements the entire standard set of

    operations or only a subset depends on the

    class.

    The standard set of data operations includesthe four basic I/O operations: select, delete,

    insert, and update.

  • 7/29/2019 The Data Source_controls

    11/51

    Binding Data Source Components

    to Controls One difference between data source

    components and ASP.NET 1.x bindable

    classes is that the new data sourceobject model allows you to select,insert, delete, and update data stored ina bound sourcebe it a relationaldatabase,an XML file, or an Exceldocument.

  • 7/29/2019 The Data Source_controls

    12/51

    Data Source Control Internals

    A data source control is a .NET

    Framework class that facilitates the

    binding ofdata between data stores anddata-bound controlsboth existing

    controls such as the DataGridand new

    data-bound controls such as GridView,TreeView, and DetailsView.

  • 7/29/2019 The Data Source_controls

    13/51

    The DataSourceControlabstract classserves as the base class for all datasource controls and defines theinterface between data-bound controlsand the underlying data.

    A data source control exposes thecontents of its underlying data sourcethrough a set of properties andmethods. Some of these members arespecific to the control; others arecommon to all source controls and aredefined as part of the IDataSource

    interface.

  • 7/29/2019 The Data Source_controls

    14/51

    Tabular Data Source Controls

    shows a diagram of the classes that form the datasource object model.

  • 7/29/2019 The Data Source_controls

    15/51

    Tabular data source controls areAccessDataSource,

    DataSetDataSource,

    Object-DataSource, and SqlDataSource.

  • 7/29/2019 The Data Source_controls

    16/51

    SqlDataSource control

    The SqlDataSource control is a data

    source control that represents a

    connection to a relational data storesuch as SQL Server or Oracle or any

    data source accessible through OLE DB

    and ODBC bridges.

  • 7/29/2019 The Data Source_controls

    17/51

    You set up the connection to the data store

    using two main properties,ConnectionStringand ProviderName. The former representsthe connection string and contains enoughinformation to open a session with theunderlying engine. The latter specifies thenamespace of the ADO.NET managedprovider to use for the operation. TheProviderName property defaultsSystem.Data.SqlClient, which means that the

    default data store is SQL Server.

  • 7/29/2019 The Data Source_controls

    18/51

    Continue..

    The DataSourceMode property controls

    how the Select command retrieves data.

    The DataSourceMode property acceptsvalues defined by the

    SqlDataSourceMode enumeration

    DataSetand DataReader.

  • 7/29/2019 The Data Source_controls

    19/51

    Code Usage

  • 7/29/2019 The Data Source_controls

    20/51

    The AccessDataSourceClass

    TheAccessDataSource control is a data source control thatrepresents a connection

    to an Access database.

    In particular, theAccessDataSource control replacesproperties such as ConnectionStringand

    ProviderName with a more directDataFileproperty. Youset this property to the .mdb database file of choice. The

    data source control resolves the file path at run time anduses the Microsoft Jet 4 OLE DB provider to connect tothe database.

  • 7/29/2019 The Data Source_controls

    21/51

    Code Usage

    The following code shows how to use theAccessDataSource control to open an .mdb fileand bind its content to a drop-down list control.

  • 7/29/2019 The Data Source_controls

    22/51

    Several features of theAccessDataSource 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 ShareModeproperty controls how the Access

    data source opens thedatabase and determines

    what data operations can be performed. The

    defaultvalue isRead, which means that only theSelectoperation can be accomplished

  • 7/29/2019 The Data Source_controls

    23/51

    In read mode, the CanDelete, CanInsert, and

    CanUpdateproperties all returnfalse, thusblocking the corresponding data operations. To

    enable read/write operations, set ShareMode to

    ReadWrite.

  • 7/29/2019 The Data Source_controls

    24/51

    The DataSetDataSourceClass

    TheDataSetDataSource class takes an XML fileor static data (such as an XML string) as input andexposes theDataSetrepresentation of this data to

    databound controls.

    TheDataSetDataSource class instantiates aDataSetobject and uses theDataSetobjectsReadXmlmethod to parse the XML input. Ideally,the supplied XML data is a DiffGram script or anyXML file created by theDataSets WriteXmlmethod.

  • 7/29/2019 The Data Source_controls

    25/51

    Code Usage

  • 7/29/2019 The Data Source_controls

    26/51

    The ObjectDataSourceClass

    The ObjectDataSource class enables businesscomponents to associate their contents to data-

    bound controls. The class supports declarative

    parameters that allow developers to pass page-

    level variables to the objects methods.

    The ObjectDataSource control is designed to

    simplify and encourage a common practice among

    page developersencapsulating data retrieval and business logic into an additional layer between the

    presentation page and data provider.

  • 7/29/2019 The Data Source_controls

    27/51

    The SiteMapDataSourceClass

    A site map is a graph that represents all the pagesand directories found in a Web site.

    Site map information is used to show users the

    logical coordinates of the page they arevisiting,allow them to access site locationsdynamically, and render all the navigationinformation in a graphical fashion. The site map isa hierarchical piece of information that can beused as input for a hierarchical data sourcecontrol. This control is the SiteMapDataSourceclass.

  • 7/29/2019 The Data Source_controls

    28/51

  • 7/29/2019 The Data Source_controls

    29/51

    ASP.NET 2.0 provides richer support for

    site maps. You start by creating a

    configuration file named app.sitemap in theroot of the Web application. The file

    describes the relationship between pages on

    the site.

  • 7/29/2019 The Data Source_controls

    30/51

    The XmlDataSourceClass

    TheXmlDataSource control is a special

    type of data source control that supports

    both tabular and hierarchical views of data.The tabular view of XML data is just a list

    of nodes at a given level of hierarchy,

    whereas the hierarchical view shows thecomplete hierarchy.

  • 7/29/2019 The Data Source_controls

    31/51

    An XML node is an instance of the

    XmlNode class; the complete hierarchy is an

    instance of theXmlDocumentclass. The

    XML data source supports both read-only

    and read-write scenarios.

    TheXmlDataSource control can accept

    XML input data as a relative or absolute

    filename assigned to theDataFileproperty

    or as a string containing the XML content

    assigned to theDataproperty.

  • 7/29/2019 The Data Source_controls

    32/51

    The control exposes that data through the

    IDataSource or theIHierarchicalDataSource

    interface. In

    general, theXmlDataSource control is commonly

    bound to a hierarchical control, such as theTreeView.

  • 7/29/2019 The Data Source_controls

    33/51

    Code Usdage(1)

    To understand how the XML data source works, consider

    the following small XML file, named data.xml:

  • 7/29/2019 The Data Source_controls

    34/51

    Code Usage(2)

  • 7/29/2019 The Data Source_controls

    35/51

    The Final Result

  • 7/29/2019 The Data Source_controls

    36/51

    Using the SqlDataSourceControl

    The SqlDataSource control represents a

    connection to an ADO.NET managed dataprovider, such as SQL Server, OLEDB,

    ODBC, Oracle, or a third-party provider.

  • 7/29/2019 The Data Source_controls

    37/51

    Data Source Parameters

    In the programming interface of theSqlDataSource control each command propertyhas its own collection of parameters. A parametercollection is a collection class named

    ParameterCollection. It stores objects whose base class isParameter. TheParameterclass representsa parameter in a parameterized query, filterexpression, or command executed by a datasource control.

  • 7/29/2019 The Data Source_controls

    38/51

    Code Usage(1)

    @MinID">

  • 7/29/2019 The Data Source_controls

    39/51

  • 7/29/2019 The Data Source_controls

    40/51

    The query contains a placeholder named

    @MinID. The data source control

    automatically populates the placeholderwith the information returned by the

    ControlParameterobjectone of the

    supported parameter types in ASP.NET2.0. The

  • 7/29/2019 The Data Source_controls

    41/51

    Parameter Types

    How a parameter is bound to a value

    depends on the type of the parameter.

  • 7/29/2019 The Data Source_controls

    42/51

    Parameter Types in ASP.NET 2.0

    Parameter Description

    ControlParameter: Gets the parameter value

    from any public property of a server control.

    CookieParameter: Sets the parameter valuebased on the content of the specified HTTP

    cookie.

    FormParameter: Gets the parameter valuefrom the specified input field in the HTTP

    request form.

  • 7/29/2019 The Data Source_controls

    43/51

    ProfileParameter: Gets the parameter value from

    the specified property name in the profile object created from the applications

    personalization scheme.

    QueryStringParameter: Gets the parametervalue from the specified variable in the

    request query string.

    SessionParameter: Sets the parameter value

    based on the content of the specified

    Session slot.

  • 7/29/2019 The Data Source_controls

    44/51

    Each parameter class has aNameproperty

    and a set of properties specific to its role

    and implementation.

  • 7/29/2019 The Data Source_controls

    45/51

    Caching Behavior

    Data caching is crucial to any realistic Webapplication and an important feature tooptimize to make the application run faster.

    Caching is also an important aspect of datasource controls. A data source controlretrieves data that will be made available toother components within the application.

    When multiple pages need to access thisinformation, an up-to-date cache providesfor a significantly faster response.

  • 7/29/2019 The Data Source_controls

    46/51

    You can instruct the SqlDataSource controlto cache the results of a query for a certain

    amount of timebut only if the data source

    mode isDataSet.

    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.

  • 7/29/2019 The Data Source_controls

    47/51

    Enabling Automatic Caching

    To enable caching on the data sourcecaching isdisabled by defaultyou set theEnableCaching

    property to true. You should also give the

    CacheDurationproperty a nonzero value.

    The CacheDurationproperty specifies the

    number of seconds before the contents of

    the data source are discarded, to be reloaded

    on the next request.

  • 7/29/2019 The Data Source_controls

    48/51

    Code

  • 7/29/2019 The Data Source_controls

    49/51

    SQL Server Cache Dependency

    The SqlDataSource control also supports a form of cache

    expiration based on a dependency between its contents and

    a table in a SQL Server database.

    The dependency is specified as a string property. The

    syntax has the form database:table. The database part ofthe string must refer to a database

    listed under the section of the

    web.config file,and the table part must be the name of a

    table in that database. You canspecify multiple table dependencies by separating them

    with semicolons

  • 7/29/2019 The Data Source_controls

    50/51

    Code

  • 7/29/2019 The Data Source_controls

    51/51