ADODB Connection string for LocalDB and SQL Server Native Client

The limit on Access MDB file size is 2GB. To get around that I wanted to try out the lightweight LocalDB server, rather than SQLExpress.
SqlLocalDB.msi is 38MB
sqlncli.msi is 3MB for win32, 5MB for win64

It took a while to get the right connection string, so to save others the wasted time, here’s what I found:

http://msdn.microsoft.com/en-us/library/hh510202.aspx
SQL Server 2014 Express LocalDB
says under “Connecting to the Automatic Instance”:

The easiest way to use LocalDB is to connect to the automatic instance owned by the current user by using the connection string “Server=(localdb)\MSSQLLocalDB;Integrated Security=true”. To connect to a specific database by using the file name, connect using a connection string similar to “Server=(LocalDB)\MSSQLLocalDB; Integrated Security=true ;AttachDbFileName=D:\Data\MyDB1.mdf”.
Note
The first time a user on a computer tries to connect to LocalDB, the automatic instance must be both created and started. The extra time for the instance to be created can cause the connection attempt to fail with a timeout message. When this happens, wait a few seconds to let the creation process complete, and then connect again.

The string uses “Integrated Security=true;”, but as you can see below that failed for me.

You will see other results in google using
Server=(localdb)\v11.0;
or
Server=(localdb)\v12.0;
and you can check the version at a command prompt;
C:\> sqllocaldb v
Microsoft SQL Server 2014 (12.0.2000.8)

but MS have now gone to a version independent name
Server=(localdb)\MSSQLLocalDB;
This is not yet mentioned on
https://www.connectionstrings.com/sql-server/

As I am connecting from Excel using ADODB, my connection string will also need a Provider, which is not mentioned in the MSDN article above.

I use the SQL Server Native Client :
http://msdn.microsoft.com/en-us/library/ms131321.aspx
you can download SQLNCLI11 directly from
http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409

I initially used “Provider=SQLNCLI;…” but got an error 3706
Provider cannot be found. It may not be properly installed.
The windows script I posted earlier gives a list of what providers are already installed on your PC.
so I find I need to use the version number in this case:
“Provider=SQLNCLI11;…”
If you want to check if a specific ADO provider is installed, check it in registry at path HKEY_CLASSES_ROOT\[Provider_Name] which gives you its CLSID.

Another potential source of confusion is the use of equivalent names in the connection string
eg Trusted_Connection=true is ODBC and Integrated Security=SSPI is OLEDB.

http://stackoverflow.com/questions/3077412/what-is-the-difference-between-trusted-connection-and-integrated-security-in-a-c
gives a handy table showing that for example, these are the same:

    +----------------------+-------------------------+
    | Value                | Synonym                 |
    +----------------------+-------------------------+
    | extended properties  | attachdbfilename        |
    | timeout              | connect timeout         |
    | server               | data source             |
    | database             | initial catalog         |
    | trusted_connection   | integrated security     |
    +----------------------+-------------------------+

Error messages

Here I used the wrong version, 11 instead of 12:
“Provider=SQLNCLI11;Server=(localdb)\v11.0;Integrated Security=SSPI;”
-2147467259 SQL Server Network Interfaces: Cannot create an automatic instance.

This is correct; if you get a timeout error, simply retry:
“Provider=SQLNCLI11;Server=(localdb)\v12.0;Integrated Security=SSPI;”
-2147467259 Unable to complete login process due to delay in opening server connection
I got a timeout error on my PC after 20 seconds so now add “timeout=30;” to the string
I imagine that time might improve with Windows prefetch.

This is the version-independent string:
“Provider=SQLNCLI11;Server=(localdb)\MSSQLLocalDB;Integrated Security=SSPI;”

The MS article uses “Integrated Security=true;”, but that failed for me:
“Provider=SQLNCLI11;Server=(localdb)\MSSQLLocalDB;Integrated Security=true;”
-2147217887 Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

http://www.connectionstrings.com/sql-server-native-client-11-0-oledb-provider/
confirms that it should be “Trusted_Connection=yes;”

This is what I finally used:
“Provider=SQLNCLI11;Server=(localdb)\MSSQLLocalDB;Trusted_Connection=yes;timeout=30;”
I am posting this here to provide a google hit for that string, in the hope this helps someone.

Patrick

Advertisements

About Patrick O'Beirne, spreadsheet auditor

Patrick provides consultancy and training in spreadsheet development, auditing / testing and model review; and the Excel addin XLtest
This entry was posted in Excel/VBA and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s