我们有一个“遗留”应用程序,它使用 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 指出这是可能的,太好了。我一定是连接字符串错了,所以我的问题应该是:连接字符串应该是什么?