2

我们有一个“遗留”应用程序,它使用 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?是否可以连接到特定文件?

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

4

2 回答 2

3

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

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

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

于 2012-12-17T17:56:09.973 回答
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

于 2012-12-13T00:47:17.087 回答