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
'initialise the dsn variable
'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
'Open the recordset object executing the SQL statement and return records
'now lets see if there are any records returned
If Recordset.Eof Then
response.write "There are no records."
'if there are records then loop through the fields
Do While NOT Recordset.Eof
'close the objects and free up resources
Set Recordset = Nothing
Set Connection = Nothing