To connect to a database you would use the Connection object. Create an instance of the Connection object,and open it using either a DSN or a connection string.
With a DSN named company:
<html>
<head>
<title>
</title>
</head>
<body>
<%
Set cn = Server.CreateObject("ADODB".Connection")
cn.Open "Company"
cn.Execute "SELECT * FROM Employees WHERE Lastname = 'Smith'"
%>
</body>
</html>
|
or alternatively a detailed connection string:
<html>
<head>
<title>
</title>
</head>
<body>
<%
Set cn = Server.CreateObject("ADODB".Connection")
cn.Open "DATABASE=CompanyData;DSN=Company;UID=sa;Password=;"
cn.Execute "SELECT * FROM Employees WHERE Lastname = 'Smith'"
%>
</body>
</html>
|
It is also possible to create the Connection object in the Application and Session onStart events. The example below shows how to create a connection for a user's session:
<html>
<head>
<title>
</title>
</head>
<body>
<%
Sub Session_onStart()
Set cn = Server.CreateObject("ADODB".Connection")
Set Session("cn") = cn
End Sub
%>
</body>
</html>
|
or just create the connection string and open and close the connections per page (this can be an efficient technique for sites with a lot of hits.)
<html>
<head>
<title>
</title>
</head>
<body>
<%
Sub Session_onStart()
Set Session("connection_string")= “DATABASE=CompanyData;DSN=Company;UID=sa;Password=;"
End Sub
%>
</body>
</html>
|