SqlConnection myConnection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\\Users\\Joe\\Documents\\Visual Studio 2012\\Projects\\FileIO\\FileIO\\Database.mdf';Integrated Security=True");
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
返回此錯誤:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll System.Data.SqlClient.SqlException: 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)
什麼想法?
檢查http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection- to-sql-server-microsoft-sql-server-error /和http://www.mssqltips.com/sqlservertip/2340/resolving-could-not-open-a-connection-to-sql-server-errors/ –
在這裏看看在創建連接字符串到SQL Server數據庫時需要的所有幫助。http://www.connectionstrings.com/sql-server-2008 – MethodMan
@JoeBabcock你可以在字符串前面放@ @ a- la @「yourpathhere」使它成爲一個字符串文字,並且您不再需要雙反斜線,這使得它更容易......不要忘記添加它們,如果你明白我的意思 –