天天看點

在代碼中連接配接LocalDB出現異常

       在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"

繼續閱讀