ADO Connect

Connecting to a database is the first step in using ADO. The Connection object is used to establish a connection to a data source, enabling data retrieval and manipulation.

Key Topics

Basic Connection

Example

<%
    Dim conn
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDatabase;User ID=myUser;Password=myPassword;"
    Response.Write("Database connected successfully.")
    conn.Close
    Set conn = Nothing
%>

Explanation: This example connects to a SQL Server database using the ADODB.Connection object and then closes the connection.

Connection with Parameters

Example

<%
    Dim connString, conn
    connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyDatabase.mdb;"
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open connString
    Response.Write("Access database connected successfully.")
    conn.Close
    Set conn = Nothing
%>

Explanation: This example demonstrates connecting to an Access database using a connection string stored in a variable.

Key Takeaways

  • ADO uses the Connection object to establish a link to a database.
  • Connection strings vary depending on the type of data source.
  • Always close and clean up the connection to release resources.