在Visual Studio 2012中链接并创建了一个LocalDb的数据库, 然后直接拷贝了其数据Connection String ("Data Source=(LocalDB)\v11.0;Initial Catalog=StatsDatabase;Integrated Security=True;Pooling=False") 到代码中使用,可是每次都抛出下面的异常 :
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
应该不是LocalDB的问题,因为在Visual Studio中是可以正常访问的。最终发现是Connection String的问题, 直接拷贝出来的Connection String带有 "\v"需要在代码中转意一下再使用,否则会被当作vertical tab 来理解:
错误:"Data Source=(LocalDB)\v11.0;Initial Catalog=StatsDatabase;Integrated Security=True;Pooling=False"
正确:"Data Source=(LocalDB)\\v11.0;Initial Catalog=StatsDatabase;Integrated Security=True;Pooling=False"