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
Sunday 7 September
MembersPro PayPal - ASP Membership software

New MS SQL 2008 Available – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Connecting to a MS SQLServer database in ASP

Below is the code for connecting to a MS SQLServer database using a DSN and without a DSN.

1. With a DSN

<%
Set connection = Server.CreateObject("ADODB.Connection")
connection.Open "DSN=DSNname;UID=username;PWD=password;Database=databasename"
%>

2. Without a DSN

<%
sConnString="DRIVER={SQL Server};SERVER=servername;UID=username;PWD=password;DATABASE=dbname"
Set connection = Server.CreateObject("ADODB.Connection")
connection.Open sConnString
%>

Here's an example of what the code would look like connecting to an SQLServer database without a DSN.

<%
'declare your variables
dim connection,recordset
dim SQL,sConnString
'declare SQL statement that will query the database
SQL="SELECT * FROM yourTable "
'define the connection string, specify database
'driver and the location of database
sConnString="DRIVER={SQL Server};SERVER=yourServername;UID=yourUsername;" & _ "PWD=yourPassword;DATABASE=yourDatabasename"
'create an ADO connection and recordset
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
'Open the connection to the database
connection.Open sConnString

'Open the recordset object, execute the SQL statement
recordset.Open SQL,connection
If recordset.eof then
 response.write "There were no records returned."
Else
 response.write "There are records returned."
End if

'close the connection and recordset objects freeing up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>


Previous: Try and avoid SELECT *
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