Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server connection timeout on VS2017

I have a console application which is doing a standard:

new SqlConnection("<connection string>").Open()

but is timing out with the below exception (eventually):

System.Data.SqlClient.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: TCP Provider, error: 0 - The wait operation timed out.) ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

while attempting to open a connection to an (Azure hosted) SQL Server 12 instance. I'm having trouble pinpointing what's cause the problem, as:

  1. It only happens in VS2017. VS2015 runs the exact same code fine.
  2. I have no trouble connecting to the DB via SSMS, LINQPad and an ASP.NET project in the same solution (even when using 2017).
  3. The connection timeout is set to 30 seconds.
  4. It only happens when running in Debug mode.
  5. It also happens when connecting to local db.

.NET 4.5.2, VS15.0.0, Win10

Any ideas appreciated, I'm at a loss what could be causing this.

Edit: Seems to have started working. I'm not exactly sure why, but possibly the upgrade to VS15.1 did the trick. Thanks all for suggestions.

like image 487
Taran Avatar asked Apr 04 '17 16:04

Taran


3 Answers

Just a shot in the dark, but are you a consultant who works from home? I had the exact same thing start happening to me today, however, I have not even verified that it still works in VS2015 or Linqpad like you have. I first check to see if I am still connected to the VPN and that my connection has not dropped.

Even if you are connected, I had this situation happen to me only once since I started here but the VPN said I was connected and I could reach Avocado but not COSMOS or any file shares. It was like a half-connected state, which I had to disconnect and reconnect a few times to get rid of. I also cleared my saved credentials from the VPN connection.

Maybe try these and see if this helps? If not, please let me know once you figure it out. Thanks!

like image 188
MajorKusinagi Avatar answered Oct 10 '22 10:10

MajorKusinagi


You are using Azure Host, try the following methods:

1) First check you are connected to database from within vs 2017

From within vs2017, display Server explorer (menu->view->server explorer)

You find a tree with azure node.

Configure the connection to azure (sql server) and connect to database.

if success connection, review the generated connection string (Is it different? use it in your application)

Success connection means : No blocking for firewall, it's independent of debug/release mode or type of application: console/web/.... and the connection string is valid

2) Generate two executable program one in debug mode , and the second in release mode.

Use the connection string directly (without reading it from the configuration file(to isolate the root of the problem).

Run the two console application in your development environment (that run vs 2017) and other machine (don't have vs 2017 installed).

Record your finding.

Be sure, you don't use compilation flag in debug mode in source code that may affect connection.

3) review the following posts, it may help:

Azure SQL Database connection exception (System.Data.SqlClient.SqlException is thrown )

Azure Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'null'

Let me know the results of 1 and 2

3)The last step (using Data Tracing in ADO.NET)

If something strange during applying method 1 or 2, we may have to use data tracing in ado.net to discover the magic :) as described in: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/data-tracing

like image 40
M.Hassan Avatar answered Oct 10 '22 11:10

M.Hassan


Could you try or check any of these:

  • Specify port 1433 in the connection string (This is the port for Azure).
  • Check that TCP IP is enabled, see here https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/configure-client-protocols
  • Have you tried increasing the timeout to see if it eventually connects?
like image 1
Ryan Wolton Avatar answered Oct 10 '22 12:10

Ryan Wolton