How to connect to a SQL Server instance with C# using Visual Studio


I am unable to connect to SQL Servers that have the '\' char which is used when connecting to a server instance. Any other type of server works without any issues. Any support or resource on this issue would be extremely helpful as I have exhausted my possible solutions.

Thanks! Chris

I have tried: - adding/removing the '@' - using a double '\' - hard coding the server name and connecting

var masterConnString = $@"Server={Server}; Database={ProjectCode}; Trusted_Connection=True;";

using (var connection = new SqlConnection(masterConnString))
     //do something

The following error occurs:

System.Data.SqlClient.SqlException HResult=0x80131904 Message=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) Source=.Net SqlClient Data Provider


When attempting to update the SQL Server Configuration Manager the connection fails due to lack of permissions.

asked on Stack Overflow Oct 16, 2019 by Chris Canney • edited Oct 16, 2019 by Chris Canney

1 Answer


Remove the trusted param and add this param

"Integrated Security=SSPI"

This always works with me when the server don't have any authentication

answered on Stack Overflow Oct 16, 2019 by spitfiremunguia

User contributions licensed under CC BY-SA 3.0