Mark Forums Read
  #1  
Old 06-04-2009, 08:06 AM
rose19 rose19 is offline
Junior Member
 
Join Date: Jun 2009
Posts: 12
rose19 is on a distinguished road
Default Connecting to an Access database and iterating through it

Hello.

Introduction

This tutorial will show you how to connect to an access database with asp.net and then iterate through the records inside the database and display them on a page. For this tutorial I will be using "Microsoft Visual Web Developer 2008 Express", which is a free program and can be downloaded by anyone right here.

Creating the Database

First thing that you will need to do is to create the database we are going to use. By default we will create the Database in the "App_Data" folder of our website, so if we have created a website called "Tutorial1", the Database would usually be created in "Tutorial1\App_Data\Database-name.mdb". So let's browse to this folder and create the database, so in the "App_Data" folder, right click and select "New > Microsoft Office Access Application" and give it your desired name. Let's add a table to this database and call it "Customers" and inside that table we'll add the following columns: "CustomerID(AutoNumber)", "Name(Text)", "LastName(text)", "TelephoneNumber(number)". Then just populate this table with some fake information.

Setting Up The Page


Let's create the following page for this example: "customers.aspx", page language: "visual basic", and the code is going to be in the same page, not in the code-behind page.
Once you have got the page open where you'd like to display all the data from the database, we first have to add two namespaces that are going to be used in this example so right at the top you will have the following
Line number On/Off
Quote:
Code: Select all

1. <%@ Page Language="VB" %>
2. <%@ Import Namespace="System.Data" %>
3. <%@ Import Namespace="System.Data.OleDb" %>
So this then imports the "System.Data" namespace and the "System.Data.OleDb" namsepace, these naspaces are not a must to import at the top of our page, but makes life a little easier in the long run.

Connecting to the database

You will now add the code somewhere in your page with the code tags (<% %>) around it:
Line number On/Off
Code: Select all

1. Dim ConString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("App_Data\Customers.mdb")
2. Dim oCon As OleDbConnection = New OleDbConnection(ConString)
3. oCon.Open()
4. Dim sQuery As String = "SELECT * FROM Customers"
5. Dim oCommand As OleDbCommand = New OleDbCommand(sQuery, oCon)
6. Dim oDataReader As OleDbDataReader = oCommand.ExecuteReader()
7. oCon.Close


So this code works as follows:
ConString is the connection string to the selected Database, and the "Server.MapPath" maps the path to the "App_Data\Customers.mdb" for when this is uploaded to the server/host we don't have to add a fixed path, but it get's worked out for us.
oCon is the connection to the database which creates a connection to the database using the connection string provided ("ConString").
oCon.Open() opens the connection to the database.
sQuery is the SQL query we specify to be executed on this connection, in this case we select all the fields from the Customers table.
oCommand is a Database command, and in this case it sets "sQuery" to "oCon" ready for execution
oDataReader is a Data Reader that in this case executes "oCommand" and reads out all the data from that database with the selected query.
oCon.Close() closes the database connection.

Reading Data from the Database

now that we have defined the "oDataReader" as an OleDbDataReader we can use it to iterate throught the records. We do that by adding the following code which is placed between "Dim oDataReader As OleDbDataReader = oCommand.ExecuteReader()" and "oCon.Close()"
Line number On/Off
Quote:
Code: Select all

1. While oDataReader.Read
2. Response.Write("<p>Name: " & oDataReader("Name") & "</p>")
3. Response.Write("<p>Name: " & oDataReader("LastName") & "</p>")
4. Response.Write("<p>Name: " & oDataReader("TelephoneNumber") & "</p>")
5. End While
This piece of code goes through the database row by row and reads the "Name","LastName" and "TelephoneNumber" fields from the database for each row.

The full page's code will look somethig like this


Line number On/Off
Quote:
Code: Select all

1. <%@ Page Language="VB" %>
2. <%@ Import Namespace="System.Data" %>
3. <%@ Import Namespace="System.Data.OleDb" %>
4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5. <script runat="server"></script>
6. <html xmlns="http://www.w3.org/1999/xhtml" >
7. <head runat="server">
8. <title>First Db Connection</title>
9. <link rel='stylesheet' type='text/css' href='stylesheet.css' />
10. </head>
11. <body>
12. <h1>First Db Connection</h1>
13. <p>This is a tutorial I found on Ozzu</p>
14. <%
15. Dim ConString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("App_Data\Customers.mdb")
16. Dim oCon As OleDbConnection = New OleDbConnection(ConString)
17. oCon.Open()
18. Dim sQuery As String = "SELECT * FROM Customers"
19. Dim oCommand As OleDbCommand = New OleDbCommand(sQuery, oCon)
20. Dim oDataReader As OleDbDataReader = oCommand.ExecuteReader()
21. While oDataReader.Read
22. Response.Write("<p>Name: " & oDataReader("Name") & "</p>")
23. Response.Write("<p>Name: " & oDataReader("LastName") & "</p>")
24. Response.Write("<p>Name: " & oDataReader("TelephoneNumber") & "</p>")
25. End While
26. oCon.Close
27. %>
28. </body>
29. </html>

Conclusion


We saw here how to connect to an Access database and read Data from it and display it on our page. So the steps we followed were: we connect to the Database, we open the connection, we use a OleDbDataReader to read through the records, as the reader reads we write the results on our page, and we close the connection.
__________________
web design in Birmingham
Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
  #2  
Old 03-18-2010, 06:43 AM
boby boby is offline
Junior Member
 
Join Date: Mar 2010
Location: UK
Posts: 17
boby is on a distinguished road
Default

Hello friends

I wish to use VBA to open an Access Query, loop through each record, and then based on the result of a certain field create calculations in which i wish to place as a new record in a new table.

Thanks for all friends
__________________
Web Design London
Reply With Quote
  #3  
Old 08-05-2011, 07:21 AM
peterjones10 peterjones10 is offline
Member
 
Join Date: Aug 2011
Posts: 33
peterjones10 is on a distinguished road
Default

This tutorial will show you how to connect to an access database with asp.net and then iterate through the records inside the database and display them on a page. For this tutorial I will be using "Microsoft Visual Web Developer 2008 Express", which is a free program and can be downloaded by anyone right here.
__________________
Residential services
Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 05:51 AM.


Powered by vBulletin Version 3.6.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 2.4.0