|
ASP.NET 2.0 includes an AccessDataSource control for exposing data from an Access database to an ASP.NET 2.0 (.aspx) page. The control has a simple set of properties. The most important property of AccessDataSource is the DataFile property, which points to the path of the MDB file on disk. The following are the steps to include the control on the page:-
- Open the Toolbox > Go to Data tab > Drag and Drop “AccessDataSource” control on the webform.
- A popup windows appear, which will ask to “Choose a Database” by using the browser window or enter the name of the Mdb format file.
- Now the wizard will ask for “would you like to retrieve data from the database?” . Here you can have the option either you can specify a custom SQL statement or stored procedure or Specify columns from a table or view.
- Once you move further, the wizard will ask you to view the data that is returned by the SQL query that you mentioned earlier. Here you have to click on the “Test Query” button to view the query result.
- Click on the “Finish” button to end.
GridView Control :
Use GridView Control to view all the data that is there in the “tblUser”. Here by clicking on the arrow, you can assign the data source property of the grid view to the accessdatasource id, then all the columns that is there will be displayed with the help of GridView Control.
Pros and Cons of Access Database
The following are the pros and cons associated with the Access database.
- Access works very well as a desktop database, and the interface is familiar to many readers. Many small offices have resources already stored in Access MDB files.
- Access does not scale to support more than a few users simultaneously.
- The problem with Access is that it was never designed to handle execution in a fast-paced, multi-threaded environment such as a Web server. Access was designed and intended as a single- or few-user desktop database.
- Access simply does not provide enough performance to run a public Web site effectively and may actually produce unpredictable results under these conditions.
- Access also creates development challenges when using parameterized SQL statements, particularly when values must be provided in order to be written to the database. When using parameters, Access correlates the values to fields based on the order in which the values are presented. This is sometimes difficult to manage if the order of fields in your form does not match the order of fields in your SQL.
Example: Demonstrate Accessing MS Access through VS.Net and displaying results.
<%@ Page Language ="C#" AutoEventWireup ="true" CodeFile ="Access1.aspx.cs" Inherits ="Access1" %>
<! 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">
</head>
<body>
<form id ="form1" runat ="server">
<div>
<asp:Label ID ="Label1" runat ="server" Text ="Accessing Access Database">
</asp:Label>
<asp:AccessDataSource ID ="AccessDataSource1" runat ="server" DataFile ="~/App_Data/Accessdb1.mdb" SelectCommand ="SELECT [userId], [userName], [password], [designation], [address] FROM [tblUser]">
</asp:AccessDataSource>
<asp:GridView ID ="grdvUser" runat ="server" AutoGenerateColumns ="False" DataKeyNames ="userId" DataSourceID ="AccessDataSource1">
<Columns>
<asp:BoundField DataField ="userId" HeaderText ="userId" ReadOnly ="True" SortExpression ="userId" />
<asp:BoundField DataField ="userName" HeaderText ="userName" SortExpression ="userName" />
<asp:BoundField DataField ="password" HeaderText ="password" SortExpression ="password" />
<asp:BoundField DataField ="designation" HeaderText ="designation" SortExpression ="designation" />
<asp:BoundField DataField ="address" HeaderText ="address" SortExpression ="address" />
</Columns>
</asp:GridView >
</div>
</form>
</body>
</html>
Note:- User can also add the SQL query for selecting some specific data into the Grid by adding the SQL directly into the grid property. |