Connecting to a database with a DSN
If you do not know how to set up a system dsn i suggest you read this article
first of all before attempting this tutorial.
For our example lets assume that our DSN points to an Access database called News.mdb and that we will be selecting records from the table tblNews. <% @ Language="VBScript" %>
<% Option Explicit %>
'declare your variables
Dim connection,dsn,recordset, sql
'declare the SQL statement that will query the database
sql="SELECT * FROM tbNews"
'initialise the dsn variable
dsn="dsn=mydsn" 'mydsn will be the name you have chosen for your dsn
'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, execute the SQL
'now lets see if there are any records returned
If Not recordset.Eof Then
response.write "There are records."
response.write "There are no records
'close the connection and recordset objects and free up resources
Set recordset = Nothing
Set connection = Nothing
Remember that mydsn above is the dsn that you named and created.
Note you can change the table name from tblNews to the name of your table and point the DSN to whatever database you like.
One other thing to remember is that you can set up a dsn on your own machine though if you are using a hosting company you may have to ask the webmaster.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Web Development Articles
More By Developer Shed