[ Team LiB ] |
Recipe 17.3 Retrieve Access Data from an ASP.NET Application17.3.1 ProblemYour ASP.NET web site needs to access data from one of your Access databases. How do you retrieve Access data using ADO.NET? 17.3.2 SolutionFollow these steps to create an ASP.NET page, AltRock.aspx, which displays a list of alternative rock albums from the 17-03.MDB database using a DataGrid control:
Figure 17-5. The data behind this DataGrid was retrieved from the 17-03.MDB database using the .NET OleDb provider17.3.3 DiscussionProbably the trickiest part of retrieving data from an Access database using ADO.NET is in creating the connection string. The basic syntax of the connection string is as follows: "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=path_to_database" If you are using a workgroup-secured database, you will need to add User Id and Password items to the connection string: "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=path_to_database;" & _ "User Id=user_name;Password=password;" If the database is password-protected, you will need to use the following connection string: "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=path_to_database;" & _ "Jet OLEDB:Database Password=database_password;"
The steps for constructing a Windows Forms-based application that accesses an Access database are fairly similar. This example binds the DataGrid to an OleDbDataReader object. You can also bind a DataGrid to a DataSet object. It's more efficient to use an OleDbDataReader; however, its usage is more limited. For example, if you wished to enable paging for the DataGrid, you would have to use a DataSet instead. 17.3.4 See AlsoThe following link provides a walkthrough for working with Access data from ADO.NET: Walkthrough: Editing an Access Database with ADO.NET (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/adon_wtaccessdb.asp). Another helpful article on ADO.NET is Unlocking Microsoft Access Data with ADO.NET (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office12062001.asp). The following article discusses how to create a pageable DataGrid using a DataSet: (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-pageablesortable.asp). If you're having trouble creating ADO.NET connection strings, check out Able Consulting's Connection Strings page (http://www.able-consulting.com/ADO_Conn.htm). |
[ Team LiB ] |