AccessDataSource Class
Assembly: System.Web (in system.web.dll)
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 in 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 about 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 Parameters with the SqlDataSource and AccessDataSource Controls.
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 about 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.
| Topic | Location |
|---|---|
| How to: Display Ads From a Database Using the AdRotator Web Server Control | Building ASP .NET Web Applications |
| How to: Bind to Data in a Templated Control | Building ASP .NET Web Applications |
| How to: Display Ads From a Database Using the AdRotator Web Server Control | Building ASP .NET Web Applications |
| How to: Bind to Data in a Templated Control | Building 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 Data | Building ASP .NET Web Applications in Visual Studio |
| How to: Bind to Data in a Templated Control in Visual Studio | Building 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="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" 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>
- AspNetHostingPermission For operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission For operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
System.Web.UI.Control
System.Web.UI.DataSourceControl
System.Web.UI.WebControls.SqlDataSource
System.Web.UI.WebControls.AccessDataSource