Connecting Classic ASP to SQL Server

Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.

Open a SQL Server connection

VBScript example

Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Provider=MSOLEDBSQL19;Data Source=sql01.example.com;" & _
        "Initial Catalog=appdb;Integrated Security=SSPI;"
' Execute commands, then close when finished.
cn.Close
Set cn = Nothing

Replace the example server and database names; the installed OLE DB driver, Windows identity, encryption, and trusted certificate must be configured. Keep credentials out of public files.

Important: Driver 19 encryption settings and certificate validation must match your infrastructure; never disable TLS verification as a generic fix.
On this page — Show page sectionsHide page sections

Keep the SQL Server connection string in one server-side include so database pages share a single configuration. The example uses Windows integrated authentication, which avoids embedding a database password in the ASP source when IIS/SQL Server identities are configured appropriately. If SQL authentication is required, keep the secret in protected server configuration rather than client-visible code or downloads.

Include the connection settings

<!-- #include virtual="/conn.asp" -->

Current OLE DB provider

<%
Dim aConnectionString
aConnectionString = "Provider=MSOLEDBSQL19;" & _
"Data Source=sql01.example.com;" & _
"Initial Catalog=appdb;" & _
"Integrated Security=SSPI;"
%>

Microsoft recommends the current MSOLEDBSQL driver generation for SQL Server. Driver 19 uses stronger encryption defaults, so production environments should be configured with a trusted server certificate and connection settings appropriate to the installed driver.

Open and close the ADO connection

<%@ Language="VBScript" %>
<% Option Explicit %>
<!-- #include virtual="/adovbs.inc" -->
<!-- #include virtual="/conn.asp" -->
<%
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Mode = adModeRead
conn.ConnectionString = aConnectionString
conn.Open

' Run database work here.

conn.Close
Set conn = Nothing
%>

Connection errors

Microsoft OLE DB Driver for SQL Server
Connection attempt failed because the server, credentials, network path, encryption or certificate configuration could not be validated.

A connection failure is not proof that SQL Server is absent. Check the server/instance name, TCP/network access, credentials, database permissions, installed OLE DB driver, encryption settings and certificate trust.

Keep connection settings private

Use an IIS-protected server-side include for the connection string. Do not put database secrets in browser HTML or a downloadable ZIP. Production TLS certificate validation should not be disabled just to suppress connection errors.

ADO constants and parameters

Examples using names such as adCmdText and adVarWChar require the ADO constants to be declared, commonly with adovbs.inc. Some concise examples use the documented numeric values instead.

Frequently asked questions

Do these examples require Classic ASP on IIS?
Yes. The server-side code uses VBScript and ADO components from Classic ASP; the .php file here is the Plus2Net tutorial wrapper.
Can I pass form input directly into SQL strings?
No. Validate it and bind it through ADO parameters rather than concatenating it into SQL text.
What should I do if no records match?
Check EOF before accessing fields and show a clear empty-result message.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer