Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:23:54+00:00 2026-05-16T17:23:54+00:00

I am using an SQL connection string with SqlClient.SqlConnection and specifying Connection Timeout=5 in

  • 0

I am using an SQL connection string with SqlClient.SqlConnection and specifying Connection Timeout=5 in the string, but it still waits 30 seconds before returning failure. How do I make it give up and return faster? I’m on a fast local network and don’t want to wait 30 seconds. The servers that are not turned on take 30 seconds to fail. This is just a quick utility program that’s going to always run just on this local network.

Edit: Sorry if I was unclear. I want the SqlConnection.Open to fail more quickly. Hopefully that could be deduced from the fact that the servers I want to fail more quickly are turned off.

Edit: It seems that the setting only fails sometimes. Like it knows the IP address of the server, and is using TCP/IP to talk to it (not local) but can’t contact SQL Server at that address? I’m not sure what the pattern is, but I don’t see the problem when connecting locally with SQL Server stopped, and I don’t see it when attempting to connect to a non-existent server. I have seen it when attempting to contact a server where the Windows 2008 firewall is blocking SQL Server, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:23:55+00:00Added an answer on May 16, 2026 at 5:23 pm

    It looks like all the cases that were causing long delays could be resolved much more quickly by attempting a direct socket connection like this:

    foreach (string svrName in args)
    {
       try
       {
          System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient(svrName, 1433);
          if (tcp.Connected)
             Console.WriteLine("Opened connection to {0}", svrName);
          else
             Console.WriteLine("{0} not connected", svrName);
          tcp.Close();
       }
       catch (Exception ex)
       {
          Console.WriteLine("Error connecting to {0}: {1}", svrName, ex.Message);
       }
    }
    

    I’m going to use this code to check if the server responds on the SQL Server port, and only attempt to open a connection if it does. I thought (based on others’ experience) that there would be a 30 second delay even at this level, but I get a message that the machine “actively refused the connection” on these right away.

    Edit: And if the machine doesn’t exist, it tells me that right away too. No 30-second delays that I can find.

    Edit: Machines that were on the network but are not turned off still take 30 seconds to fail I guess. The firewalled machines fail faster, though.

    Edit: Here’s the updated code. I feel like it’s cleaner to close a socket than abort a thread:

    static void TestConn(string server)
    {
       try
       {
          using (System.Net.Sockets.TcpClient tcpSocket = new System.Net.Sockets.TcpClient())
          {
             IAsyncResult async = tcpSocket.BeginConnect(server, 1433, ConnectCallback, null);
             DateTime startTime = DateTime.Now;
             do
             {
                System.Threading.Thread.Sleep(500);
                if (async.IsCompleted) break;
             } while (DateTime.Now.Subtract(startTime).TotalSeconds < 5);
             if (async.IsCompleted)
             {
                tcpSocket.EndConnect(async);
                Console.WriteLine("Connection succeeded");
             }
             tcpSocket.Close();
             if (!async.IsCompleted)
             {
                Console.WriteLine("Server did not respond");
                return;
             }
          }
       }
       catch(System.Net.Sockets.SocketException ex)
       {
          Console.WriteLine(ex.Message);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using SQL Server Express 2005's User Instance feature with a connection string like
The excel spreadsheet is connecting to SQL server 2005 using the connection string below:
Using VB6 and SQL Server 2005 I want to write a sql connection for
I'm using Excel 2010 to read some SQL data using an ODBC data connection.
Need to query my SQL server from Access using an ADO connection (for example),
is it possible to retrieve by using connection.getschema() the description item from a sql
In my installer, I am using System.Data.SqlClient.SqlConnection(connectionstring) to check if I can connect to
I have made connection with sql server but i have never done a connection
I connect my windows mobile app directly to ms-sql server using the sqlclient dll
I am creating Windows Application and using System.Data.SqlClient.SqlConnection object SqlConnection targetDatabaseConnection = new SqlConnection(ConnectionString);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.