The
The AccessDataSource control inherits the
Connecting to a Database using the AccessDataSource Control
The AccessDataSource control connects to the Microsoft Access database file (.mdb file) identified in the DataFile property. You can set the DataFile property to a universal naming convention (UNC) path that points to an Access database file. The following example demonstrates how you can use a root-relative path to identify an Access database that is located in the App_Data folder of the current Web application.
В | Copy Code |
---|---|
<asp:AccessDataSource id="AccessDataSource1" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees"> </asp:AccessDataSource> |
If you are storing an Access database file with your Web application, it is recommended that you store it in the App_Data folder to keep the database private. ASP.NET does not allow files in the App_Data folder to be returned if requested directly. The ASP.NET process identity must be granted read and write file permissions to the Access databases stored in the App_Data folder. For information on the ASP.NET process identity, see Configuring ASP.NET Process Identity.
The AccessDataSource control sets the
Note |
---|
The AccessDataSource will not connect to an Access database that is password-protected; to retrieve data from a password-protected Access database, use the SqlDataSource control. |
Selecting Data Using the AccessDataSource Control
You can specify an SQL query for the AccessDataSource control to execute by setting its
Visual BasicВ | Copy Code |
---|---|
<form runat="server"> <asp:AccessDataSource id="EmployeesAccessDataSource" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="SELECT EmployeeID, FirstName, LastName FROM Employees" /> <asp:GridView id="EmployeesGridView" runat="server" AutoGenerateColumns="True" DataSourceid="EmployeesAccessDataSource" /> |
C#В | Copy Code |
---|---|
<form runat="server"> <asp:AccessDataSource id="EmployeesAccessDataSource" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="SELECT EmployeeID, FirstName, LastName FROM Employees" /> <asp:GridView id="EmployeesGridView" runat="server" AutoGenerateColumns="True" DataSourceid="EmployeesAccessDataSource" /> |
You can return results from a Microsoft Access query by setting the
Visual BasicВ | Copy Code |
---|---|
<asp:AccessDataSource id="InvoiceAccessDataSource" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="[Employee Sales By Country]" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:Parameter Name="Beginning Date" Type="DateTime" defaultValue="1/1/1997" /> <asp:Parameter Name="Ending Date" Type="DateTime" defaultValue="1/31/1997" /> </SelectParameters> </asp:AccessDataSource> <asp:GridView id="InvoiceGridView" runat="server" AutoGenerateColumns="True" DataSourceid="InvoiceAccessDataSource" /> |
C#В | Copy Code |
---|---|
<asp:AccessDataSource id="InvoiceAccessDataSource" DataFile="~/App_Data/Northwind.mdb" runat="server" SelectCommand="[Employee Sales By Country]" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:Parameter Name="Beginning Date" Type="DateTime" defaultValue="1/1/1997" /> <asp:Parameter Name="Ending Date" Type="DateTime" defaultValue="1/31/1997" /> </SelectParameters> </asp:AccessDataSource> <asp:GridView id="InvoiceGridView" runat="server" AutoGenerateColumns="True" DataSourceid="InvoiceAccessDataSource" /> |
For information on modifying data using the AccessDataSource control, see Modifying Data using the SqlDataSource Control; that topic applies to working with the AccessDataSource control because the control inherits the capability of the SqlDataSource control and implicitly makes use of the System.Data.OleDb provider.
Using Parameters with the AccessDataSource Control
You can use parameterized queries for commands and supply parameters at run time. Additionally, you can specify parameters at run time when calling a Microsoft Access query. (To call a Microsoft Access query, you set the command type property for the command to StoredProcedure). For more information, see Using Parameters with Data Source Controls.
Because the AccessDataSource control extends the SqlDataSource class and uses the System.Data.OleDb provider, you specify parameter placeholders using the "?" placeholder character. The System.Data.OleDb provider does not support named parameters; instead, the parameter values are applied in the order they are specified in the parameters collection. You must ensure that the order of the parameters in the parameters collection matches the order of the parameter placeholders in your SQL statement or Microsoft Access query. For more information and examples, see Using Parameters with the SqlDataSource Control.