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

0

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))
{                
     connection.Open();
     //do something
     connection.Close();
}

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

Update:

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

c#
sql
sql-server
visual-studio
asked on Stack Overflow Oct 16, 2019 by Chris Canney • edited Oct 16, 2019 by Chris Canney

1 Answer

1

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