Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AccessDataSource Class

Note: This class is new in the .NET Framework version 2.0.

Represents a Microsoft Access database to data-bound controls.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class AccessDataSource : SqlDataSource
public class AccessDataSource extends SqlDataSource
public class AccessDataSource extends SqlDataSource

The AccessDataSource class is a data source control that works with Microsoft Access databases. Like its base class, SqlDataSource, the AccessDataSource control uses SQL queries to perform data retrieval.

One of the unique characteristics of the AccessDataSource control is that you do not set the ConnectionString property. All you need to do is set the location of the Access .mdb file, using the DataFile property, and the AccessDataSource takes care of the underlying connection to the database. You should place Access databases in the App_Data directory of the Web site and reference them by a relative path (for example, ~/App_Data/Northwind.mdb). This location offers additional security for data files, because they are not served if they are requested directly by the client Web browser.

The AccessDataSource class does not support connecting to Access databases that are protected by a user name or password, because you cannot set the ConnectionString property. If your Access database is protected by a user name or password, use the SqlDataSource control to connect to it so that you can specify a complete connection string.

You bind data-bound controls to an AccessDataSource using the DataSourceID property of the data-bound control. For more information on binding a data-bound control to data source controls, see Binding to Data Using a Data Source Control.

Because the AccessDataSource control is specific to only one database product, it always uses the same provider—the System.Data.OleDb .NET data provider. For this reason, the ProviderName property is read-only. When using the System.Data.OleDb provider, the order of the parameters in a parameterized SQL statement is significant; any parameterized SQL queries that you specify in the SelectCommand, UpdateCommand, InsertCommand, and DeleteCommand properties must match the order of any Parameter objects that are in the corresponding parameter collection. If no parameter collection is specified on the AccessDataSource, the order of parameters that are supplied to these operations must match the order in which they appear in the underlying SQL statement. For more information, see Using Parameters with the SqlDataSource Control.

The AccessDataSource control supports the same caching behavior that its base class supports. Use caching to increase the performance of pages that use the AccessDataSource control. For more information on data source caching, see Caching Data Using Data Source Controls.

The following table describes the features of the AccessDataSource control.

Capability

Requirements

Sorting

Set the DataSourceMode property to the DataSet value.

Filtering

Set the FilterExpression property to a filtering expression used to filter the data when the Select method is called.

Paging

The AccessDataSource does not support direct paging operations on an Access database. A data-bound control, such as the GridView, can page over the items returned by the AccessDataSource, if the DataSourceMode property is set to the DataSet value. 

Updating

Set the UpdateCommand property to a SQL statement used to update data. This statement is typically parameterized.

Deleting

Set the DeleteCommand property to a SQL statement used to delete data. This statement is typically parameterized.

Inserting

Set the InsertCommand property to a SQL statement used to insert data. This statement is typically parameterized.

Caching

Set the DataSourceMode property to the DataSet value, the EnableCaching property to true, and the CacheDuration and CacheExpirationPolicy properties according to the caching behavior you want for your cached data.

As with all data source controls, the AccessDataSource control is associated with a strongly typed data source view class. Just as the AccessDataSource control extends the SqlDataSource control, the AccessDataSourceView class extends the SqlDataSourceView class. The AccessDataSourceView class overrides a few of the SqlDataSourceView methods to use the System.Data.OleDb .NET data provider.

There is no visual rendering of the AccessDataSource control; it is implemented as a control to enable declarative persistence and to permit, optionally, participation in state management. As a result, you cannot set properties that support visual features, such as the EnableTheming and SkinID properties.

TopicLocation
How to: Display Ads From a Database Using the AdRotator Web Server ControlBuilding ASP .NET Web Applications
How to: Bind to Data in a Templated ControlBuilding ASP .NET Web Applications
How to: Display Ads From a Database Using the AdRotator Web Server ControlBuilding ASP .NET Web Applications
How to: Bind to Data in a Templated ControlBuilding ASP .NET Web Applications
How to: Connect to an Access Database Using the AccessDataSource Control (Visual Studio)Building ASP .NET Web Applications in Visual Studio
How to: Add Repeater Web Server Controls to a Web Forms Page (Visual Studio)Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Web Page to Display Access Database DataBuilding ASP .NET Web Applications in Visual Studio
How to: Bind to Data in a Templated Control in Visual StudioBuilding ASP .NET Web Applications in Visual Studio

The following code example demonstrates a common display and update scenario with the GridView control. The SelectCommand property is set to an appropriate SQL query, and data from the Northwind database is displayed in the GridView control. Because a UpdateCommand property is also specified and the AutoGenerateEditButton property is set to true, you can edit and update the records with no additional code. The GridView control handles adding parameters to the UpdateParameters collection; the GridView control passes the parameter values for the BoundField objects first, followed by the values of fields that are specified by the DataKeyNames property. The GridView calls the Update method automatically. If you want to order the parameters, explicitly, specify an UpdateParameters collection on the AccessDataSource.

<%@Page  Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
  <BODY>
    <form runat="server">

      <asp:accessdatasource
        id="AccessDataSource1"
        runat="server"
        datasourcemode="DataSet"
        datafile="~/App_Data/Northwind.mdb"
        selectcommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
        updatecommand="Update Employees SET FirstName=?,LastName=?,Title=? WHERE EmployeeID=@EmployeeID">
      </asp:accessdatasource>

      <asp:gridview
        id="GridView1"
        runat="server"
        autogeneratecolumns="False"
        datakeynames="EmployeeID"
        autogenerateeditbutton="True"
        datasourceid="AccessDataSource1">
        <columns>
          <asp:boundfield headertext="First Name" datafield="FirstName" />
          <asp:boundfield headertext="Last Name" datafield="LastName" />
          <asp:boundfield headertext="Title" datafield="Title" />
        </columns>
      </asp:gridview>

    </form>
  </BODY>
</HTML>

<%@Page  Language="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
  <BODY>
    <FORM runat="server">

      <asp:AccessDataSource
          id="AccessDataSource1"
          runat="server"
          DataSourceMode="DataSet"
          DataFile="~/App_Data/Northwind.mdb"
          SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
          UpdateCommand="Update Employees SET FirstName=@FirstName,LastName=@LastName,Title=@Title WHERE EmployeeID=@EmployeeID">
      </asp:AccessDataSource>

      <asp:GridView
        id="GridView1"
        runat="server"
        AutoGenerateColumns="False"
        DataKeyNames="EmployeeID"
        AutoGenerateEditButton="True"
        DataSourceID="AccessDataSource1">
        <Columns>
          <asp:BoundField HeaderText="First Name" DataField="FirstName" />
          <asp:BoundField HeaderText="Last Name" DataField="LastName" />
          <asp:BoundField HeaderText="Title" DataField="Title" />
        </Columns>
      </asp:GridView>

    </FORM>
  </BODY>
</HTML>

System.Object
   System.Web.UI.Control
     System.Web.UI.DataSourceControl
       System.Web.UI.WebControls.SqlDataSource
        System.Web.UI.WebControls.AccessDataSource

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft