© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
89 replies
aca

❔ Connection Timeout argument passed doesn't work for a SqlConnection

I have the following connection string:

<add name="DatabaseServerInstance" 
     connectionString="Server=xx.xx.x.xx; Database=db; User Id=user; Password=pass; TrustServerCertificate=true; Connection Timeout=2;" />
<add name="DatabaseServerInstance" 
     connectionString="Server=xx.xx.x.xx; Database=db; User Id=user; Password=pass; TrustServerCertificate=true; Connection Timeout=2;" />


I have a WPF with a control calling a MessageBox to show whether the connection to the database is established or not and I want Connection.Open() to terminate early instead of the user having to wait 15 seconds for the failed connection message to be displayed, but Connection Timeout=2 seems to do nothing.

An open connection gives an instantaneous response, while the failed connection despite the argument, loads with the average time no matter what the timeout is set to.

This is the code for the sqlconnection call:
public class SqlPusher
{
    private static SqlCommand command = new SqlCommand();
    private static string connectionString = ConfigurationManager.ConnectionStrings["DatabaseServerInstance"].ConnectionString;

    public Boolean TestConnection()
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
        }
    }

}
public class SqlPusher
{
    private static SqlCommand command = new SqlCommand();
    private static string connectionString = ConfigurationManager.ConnectionStrings["DatabaseServerInstance"].ConnectionString;

    public Boolean TestConnection()
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
        }
    }

}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

SqlConnection Connection Disposed
C#CC# / help
15mo ago
Creating a SqlConnection connection via connection string
C#CC# / help
3y ago
Specify timeout for websocket connection
C#CC# / help
2y ago
❔ Filter doesn't work
C#CC# / help
3y ago