Google+ Pieces o' Eight: Using VB6/VBScript to connect to a SQL Server 2012 LocalDB instance

Wednesday, 18 April 2012

Using VB6/VBScript to connect to a SQL Server 2012 LocalDB instance

In a previous post I covered how to use Python to connect to a SQL Server 2012 LocalDB instance. In this post I'll show you how to connect via VB6/VBScript. As per the Python post, you'll need to download and install the SQL Server 2012 LocalDb software, along with the SQL Server Native Client 11 database driver. 

One of the key differences - and one that took me some time to figure out - is that you need to specify a slightly different connection string: 

connectionString = "Provider=SQLNCLI11;Data Source=(localdb)\v11.0;Integrated Security=SSPI;"


One of the gotchas I encountered was with the Integrated Security parameter. If this isn't set to SSPI, you'll get Multiple-Step OLE DB Operation Errors.

Here's a complete listing you should be able to copy into a VBScript file to test:

connectionString = "Provider=SQLNCLI11;Data Source=(localdb)\v11.0;Integrated Security=SSPI;"
Set conn = CreateObject("ADODB.Connection")
Set com = CreateObject("ADODB.Command")
conn.Open connectionString
set com.ActiveConnection = conn
com.CommandText = "SELECT 'Hello, World!'"
set rs = com.Execute
while not rs.eof
    msgbox CStr(rs.Fields(0))
    rs.movenext()
wend
conn.close