ADO - Open Method

What is the Open Method?

The Open method is used with the Connection object in ADO to establish a connection to a data source (like SQL Server, Oracle, Access, etc.) using a connection string.

Without calling Open, the Connection object is not active and you cannot execute commands or queries.


Syntax

Connection.Open(ConnectionString, UserID, Password, Options)

Arguments

  1. ConnectionString (optional if you use DSN)

    • Specifies the provider, server, database, and login information.

    • Example: "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDB;User Id=sa;Password=pass;"

  2. UserID (optional)

    • Login name (if not included in the connection string).

  3. Password (optional)

    • Login password (if not included in the connection string).

  4. Options (optional)

    • Controls how the provider opens the connection (e.g., adConnectUnspecified, adAsyncConnect).


Examples

1. Simple SQL Server Connection

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDB;User Id=sa;Password=pass;"

If conn.State = 1 Then
    Response.Write "Connection successful!"
End If

conn.Close

2. Using UserID and Password Arguments

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDB;", "sa", "mypassword"

3. Opening an Access Database

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\MyDatabase.mdb;"

4. Asynchronous Connection (non-blocking)

Const adAsyncConnect = 16

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDB;User Id=sa;Password=pass;", , , adAsyncConnect

This lets your script continue running while the connection is being established (provider-dependent).


Key Points

  • Always call Open before executing any SQL commands.

  • If you don’t close the connection, ADO will try to reuse it through connection pooling.

  • Use conn.State to check if the connection is open (1) or closed (0).

  • Wrap Open in error handling (On Error Resume Next) to catch login or provider errors.


The Open method is the entry point into the database world in ADO — it activates your Connection object using a connection string and optional credentials.