Jon Pawley
Jon Pawley

Reputation: 487

ODBC connection to SQL Server 2012 LocalDB instance

We have a "legacy" application, which uses ODBC connections to an underlying database, which can be Access, Oracle or SQL Server. For unit (or, perhaps more properly, "integration") test purposes, I'd like to hook up a SQL Server 2012 LocalDB instance. However, I cannot figure out a correct ODBC connection string to use.

I have tried:

    [TestMethod]
    public void OdbcConnectionToLocalDb()
    {
        string connectionString = "DRIVER=SQL Server Native Client 11.0;Trusted_Connection=Yes;SERVER=(localdb)\v11.0;Description=LocalDB;";
        using (OdbcConnection connection = new OdbcConnection(connectionString))
        {
            using (var command = new OdbcCommand("select * from Person", connection))
            {
                connection.Open();
                // ... 
            }
        }
    }

However, when the connection is opened, the following exception is thrown:

System.Data.Odbc.OdbcException: ERROR [08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider:  Could not open a connection to SQL Server [67]. 
ERROR [HYT00] [Microsoft][SQL Server Native Client 11.0]Login timeout expired
ERROR [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

Is it possible to connect to a SQL Server 2012 LocalDB via an ODBC connection/driver? Is it possible to connect to a specific file?

[EDIT] Garrett points out it is possible, great. I must have the connection string wrong, so my question really should be: what should the connection string be?

Upvotes: 2

Views: 9904

Answers (2)

steoleary
steoleary

Reputation: 9278

You need to specify your connection string like this:

Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=(localdb)\v11.0

The main thing I think is that you reference it as a data source rather than server.

Upvotes: 3

Garrett Vlieger
Garrett Vlieger

Reputation: 9494

Yes, it's possible. Make sure you install the latest driver: SQL Server Native Client "Denali" (for ODBC and OLE DB).

Look here for more info: http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx

Upvotes: 0

Related Questions