简体   繁体   中英

Losing connection to sql server 2008 in C#

I use the SqlConnection class to connect to the server and after some time and some queries, I get the very common

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).

The problem is that I can connect to the server and I only get the error after sometime and after running some queries, while this is a classic cannot connect at all error...

Why is this happening? What can possible cause such behavior?

Any help/direction will be much appreciated.

This sounds like a problem with connection pooling. Check the lifecycle of sql connection object.

If this occurs while you are debugging your application, it's perfectly normal.

After you are checking the code and move on to the next query execution, while the connection is still not closed in your code, it actually times out.

Check keep-alive timeout and advance it accordingly at your SQL server connection parameters. SQL server drops connections with no activity after short down-time inactivity timer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM