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.
<%
dim connection,recordset
dim SQL,sConnString
SQL="SELECT * FROM yourTable "
sConnString="DRIVER={SQL Server};SERVER=yourServername;UID=yourUsername;" & _
"PWD=yourPassword;DATABASE=yourDatabasename"
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
connection.Open sConnString
recordset.Open SQL,connection
If recordset.eof then
response.write "There were no records returned."
Else
response.write "There are records returned."
End if
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|