This tutorial documents code for connecting to a MS Access Database using the DSN-Less approach. With DNS-Less approach all you have to do in your code is define the location where your database resides.
Content:
This tutorial will show you how to connect to a MS Access database using the DSN-less approach.
Remember, your database can reside anywhere in your computer. If you are not sharing your database it is advisable not to keep your database under the “wwwroot” directory.
Assumptions:
Database is password protected
Running IIS
Just copy and paste the following code in your ASP file and read the comments to modify settings according to your needs: (Text in grey are comments)
<%
' Output to browser
Do While Not rs.EOF
response.write("Col1 and Col2 Values: " & rs("col1") & ", " & rs("col2") & "<BR>")
' Move to the Next Record in the database
rs.MoveNext
Loop
' Close the recordset
rs.close
' Close connection
conn.close
%>
The provided code is written in ASP (Active Server Pages), a Microsoft technology used for creating dynamic web pages that interact with databases. Here’s a breakdown of what each part of the code does:
<% %>
: These tags indicate the beginning and end of ASP script within an HTML document. Code within these tags is processed on the server before the page is sent to the client’s browser.- Comments (
'
): Text following a single quote is a comment, intended to explain the code but not executed as part of the script. Do While Not rs.EOF
: This line starts a loop. The loop continues as long asrs.EOF
(End Of File) isFalse
. In this context,rs
refers to a recordset object which represents a set of records (rows) returned by a database query.rs.EOF
becomesTrue
when the recordset has been completely processed, meaning there are no more records to read.response.write(...)
: This line sends output to the web browser.response.write
is a method used to write a specific string to the HTTP response. Here, it concatenates the string"Col1 and Col2 Values: "
with the values from thecol1
andcol2
fields of the current record in the recordset (rs("col1")
andrs("col2")
), followed by a line break ("<BR>"
) for HTML formatting.rs.MoveNext
: This command moves the recordset’s current position to the next record. It’s necessary to progress through the recordset while iterating over it in the loop.Loop
: The end of theDo While
loop. Ifrs.EOF
is stillFalse
, the loop will continue, processing the next record.rs.close
: This command closes the recordset (rs
). It’s important to close the recordset to free up system resources once you’re done with it.conn.close
: Closes the database connection (conn
). Closing the connection is crucial after all database operations are complete to ensure efficient resource management.
In summary, this script loops through a set of records from a database, writing specific fields from each record to the web browser, and then closes the recordset and database connection. This is a common pattern in web applications where data is retrieved from a database and displayed to the user.