Monday, July 20, 2009

How do I connect to a non-default instance of SQL Server?

The convention of "Data Source" (for creating link server) or "Server Name" (called in management studio" is : ,/ or /,

Here is a connection string that attaches to an instance of SQL Server that is NOT the default instance.


cst = "Provider=SQLOLEDB;" & _
"Data Source=127.0.0.1\instanceName;" & _
"Initial Catalog=pubs;" & _
"Network=DBMSSOCN;" & _
"User ID=;" & _
"Password="

set conn = CreateObject("ADODB.Connection")
conn.open cst


If your instance is running on a non-default port (by default, SQL Server runs on 1433):


cst = "Provider=SQLOLEDB;" & _
"Data Source=127.0.0.1,1510\instanceName;" & _
"Initial Catalog=pubs;" & _
"Network=DBMSSOCN;" & _
"User ID=;" & _
"Password="

set conn = CreateObject("ADODB.Connection")
conn.open cst