与SQL Server 2012 LocalDB实例的ODBC连接

时间:2012-12-13 00:33:40

标签: c# 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?是否可以连接到特定文件?

[编辑] 加勒特指出这很有可能。我必须让连接字符串错误,所以我的问题应该是:连接字符串应该是什么?

2 个答案:

答案 0 :(得分:3)

您需要像这样指定连接字符串:

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

我认为主要的是你将它作为数据源而不是服务器引用。

答案 1 :(得分:0)

是的,这是可能的。确保安装最新的驱动程序:SQL Server Native Client“Denali”(用于ODBC和OLE DB)。

点击此处了解更多信息: http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx