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 %>