ODBC 连接到 SQL Server 2012 LocalDB 实例



我们有一个"遗留"应用程序,它使用ODBC连接到底层数据库,可以是Access,Oracle或SQL Server。出于单元(或者更恰当地说,"集成")测试目的,我想挂接一个SQL Server 2012 LocalDB实例。但是,我无法找出要使用的正确 ODBC 连接字符串。

我试过:

    [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();
                // ... 
            }
        }
    }

但是,当打开连接时,将引发以下异常:

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.

是否可以通过 ODBC 连接/驱动程序连接到 SQL Server 2012 LocalDB?是否可以连接到特定文件?

[编辑]加勒特指出这是可能的,太好了。我一定是连接字符串错误,所以我的问题应该是:连接字符串应该是什么?

需要指定连接字符串,如下所示:

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

我认为最主要的是您将其引用为数据源而不是服务器。

是的,这是可能的。请确保安装最新的驱动程序:SQL Server 本机客户端"Denali"(适用于 ODBC 和 OLE DB)。

查看此处以获取更多信息:http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx