iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎃

Difference between SqlConnection.ConnectionTimeout and SqlCommand.CommandTimeout

に公開

I didn't quite understand the difference between SqlConnection.ConnectionTimeout and SqlCommand.CommandTimeout, so I decided to look it up.

Conclusion

SqlConnection.ConnectionTimeout is the time spent waiting for a connection to the database.
The default value is 15 seconds.

SqlCommand.CommandTimeout is the time spent waiting for query execution.
The default value is 30 seconds.

Verification Environment

name version
C# 10.0
.NET 6.0.11
SQL Server 15.0.4261.1

Verifying Connection Timeout

Let's try specifying an incorrect DataSource on purpose.

var connectionString = new SqlConnectionStringBuilder
{
    DataSource = "local", // Incorrect DataSource
    InitialCatalog = "test",
    UserID = "sa",
    Password = "rootRootr00t",
    ConnectTimeout = 10, // Wait for 10 seconds
}.ToString();

try
{
    using (var connection = new SqlConnection(connectionString))
    {
        var query = "WAITFOR DELAY '00:00:10'";

        using (var command = new SqlCommand(query, connection))
        {
            command.Connection.Open();

            // Wait for 10 seconds
            command.CommandTimeout = 10;

            command.ExecuteNonQuery();
        }
    }

    Console.WriteLine("Done");
}
catch (Exception exception)
{
    Console.WriteLine("Exception occurred");
    Console.WriteLine(exception.Message);
}

Execution Result

Since it could not connect to the database even after 10 seconds, an exception occurred, and the following message was output.

Exception occurred
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)

Verifying Query Timeout

Set CommandTimeout to 5 seconds and execute a query that waits for 10 seconds.

var connectionString = new SqlConnectionStringBuilder
{
    DataSource = "localhost",
    InitialCatalog = "test",
    UserID = "sa",
    Password = "rootRootr00t",
    ConnectTimeout = 10,
}.ToString();

try
{
    using (var connection = new SqlConnection(connectionString))
    {
        // Query that waits for 10 seconds
        var query = "WAITFOR DELAY '00:00:10'";

        using (var command = new SqlCommand(query, connection))
        {
            command.Connection.Open();

            // Wait for 5 seconds
            command.CommandTimeout = 5;

            command.ExecuteNonQuery();
        }
    }

    Console.WriteLine("Done");
}
catch (Exception exception)
{
    Console.WriteLine("Exception occurred");
    Console.WriteLine(exception.Message);
}

Execution Result

Since there was no response from the database even after 5 seconds from executing the query, a timeout occurred, and the following message was output.

Exception occurred
Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
Operation cancelled by user.

References

SqlConnectionStringBuilder.ConnectTimeout Property (System.Data.SqlClient) | Microsoft Learn
SqlConnection.ConnectionTimeout Property (System.Data.SqlClient) | Microsoft Learn
SqlCommand.CommandTimeout Property (System.Data.SqlClient) | Microsoft Learn

GitHubで編集を提案

Discussion