Codefixer - ASP tutorials, resources and software
ASP.NET Hosting with US & UK Data Centers!
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 6 October
MembersPro PayPal - ASP Membership software

New MS SQL 2008 Available – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Connecting to a database with a DSN

If you do not know how to set up a system DSN (Data Source Name) don't worry as our next tutorial will go through this.

For our example below lets assume that our DSN points to an Access database called 'News.mdb' and that we will be selecting records from the table 'tblNews'.

Our file is called 'dsn_connection.asp'.

<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Connecting to a database with a DSN</title>
</head>
<body>
<%
'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
connection.Open dsn

'Open the recordset object, execute the SQL
recordset.Open sql,connection

'now lets see if there are any records returned
If Not recordset.Eof Then
response.write "There are records."
Else
response.write "There are no records
End If

'close the connection and recordset objects and free up resources
recordset.Close
Set recordset = Nothing
connection.Close
Set connection = Nothing
%>
</body>
</html>

Remember that 'mydsn' above is name of the DSN.
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.


Previous: OLEDB & ODBC connection strings
LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software