I am currently unable to connect to a remote SQL Server using the TCP/IP protocol in C#.
Using named pipes does work, but the TCP/IP connection doesn't. The frustrating thing is that I can connect to the database using the TCP/IP protocol in the server explorer window within Visual Studio 2015, but my code cannot, even though I am copying and pasting the exact connection strings.
As soon I a remove "Network Library = dbmssocn" from the connection strings, the code works, but this makes it use named pipes, which I cannot rely on being an an option outside of the development environment.
Just in case, I have verified that the TCP/IP protocol is enabled on the SQL Server. This makes sense, since I can specify the TCP/IP protocol in visual studio's server explorer window and those connections work.
The server is also configured for both SQL authentication and Windows authentication. The windows firewall is probably also not the issue as it is currently turned off.
Here is the code that I'm using:
DbConnection testConnectionIntegrated = new SqlConnection("Data Source = MyServer; Initial Catalog = MyDatabase; Integrated Security = True; Encrypt = True; TrustServerCertificate = True; Network Library = dbmssocn");
try
{
Console.Write("Attempting to connect using Windows Authentication...");
testConnectionIntegrated.Open();
Console.WriteLine(testConnectionIntegrated.State);
}
catch (Exception caughtException)
{
Console.WriteLine("Integrated Security Connection Failed");
Console.WriteLine(caughtException.Message);
}
finally
{
Console.Write("Closing connection...");
testConnectionIntegrated.Close();
Console.WriteLine(testConnectionIntegrated.State);
}
DbConnection testConnectionSQLAuth = new SqlConnection("Data Source = MyServer; Initial Catalog = MyDatabase; Persist Security Info = True; User ID = my_user_name; Password = my_password; Encrypt = True; TrustServerCertificate = True; Network Library = dbmssocn");
try
{
Console.Write("Attempting to connect using SQL Authentication...");
testConnectionSQLAuth.Open();
Console.WriteLine(testConnectionSQLAuth.State);
}
catch (Exception caughtException)
{
Console.WriteLine("SQL Authentication Connection Failed");
Console.WriteLine(caughtException.Message);
}
finally
{
Console.Write("Closing connection...");
testConnectionSQLAuth.Close();
Console.WriteLine(testConnectionIntegrated.State);
}
And this is the error message I'm seeing for both connection attempts:
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: TCP Provider, error: 0 - A non-recoverable error occurred during a database lookup.)
Is there anything that I'm missing?
Edit: I have verified the following on the server:
- Remote connections are allowed.
- The TCP/IP protocol is enabled and configured.
- The named pipes protocol is enabled and configured.
- Firewall is not the problem.
- The SQL Server and SQL Server Browser services are running.
- I have restarted the services to make sure.
Also, I have restarted my development machine, and the problem still exists.