Home page
 
 Home 
 ASP 
 PHP 
 SQL 
 HTML 
 JavaScript 
 Search 
 Contact 
 
Search
or browse popular tags
ASP Tutorial
Subscription

Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button.

Privacy Policy

RSS Twitter

How to connect to an Access database with a DSN

Print

DSN stands for 'Data Source Name'. It is an easy way to assign useful and easily rememberable names to data sources which may not be limited to databases alone. If you do not know how to set up a system DSN read our tutorial How to set up a system DSN.

In this tutorial we will show you how to connect with a DSN to an Access database called 'examples.mdb'  and retrieve all the records from the table 'cars'. Everything is commented so you won't have trouble.

<% 
'declare the variables
Dim Connection
Dim DSN
Dim Recordset
Dim SQL

'initialise the dsn variable
DSN ="DSN=example_dsn"  

'declare the SQL statement that will query the database

SQL = "SELECT * FROM CARS"

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
connection.Open DSN

'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection

'now lets see if there are any records returned
If Recordset.Eof Then
response.write "There are no records."
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof   
Response.write Recordset("Name")
Response.write Recordset("Year")
Response.write Recordset("Price")
Response.write "<br>"   
Recordset.MoveNext    
Loop
End If

'close the objects and free up resources
Recordset.Close
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
%>



Tags:

Add To: Add to dzone dzone | Digg this digg | Add to del.icio.us del.icio.us | Stumble it stumbleupon

  • Comments





Copyright © 2005-2023             www.WebCheatSheet.com All Rights Reserved.