DJPB
DJPB

Reputation: 5799

connection timeout property in connection string ignored

I'm building an app in C#. I'm using a connection string like:

DSN=SomeDataSource; Trusted Connection=yes; Uid=SomeId; pwd=somePwd; Connection Timeout=x

But no matter what value I set as x (Connection Timeout = x), by putting a breakpoint, I can see that my DbConnection object's ConnectionTimeout property always has the default value 15.

Am I missing something here?

Thanks.

Upvotes: 14

Views: 25822

Answers (3)

Chalky
Chalky

Reputation: 1642

Yes, 15 is the minimum, depending on what you're doing. Probably a per-provider thing. I've even found when trying to use less than 15 (with some 3rd-party controls) using SQL Native Client, I end up with infinite timeouts instead. Yuck. When I set to 15 or higher, it behaves as expected.

Upvotes: -1

Neil Knight
Neil Knight

Reputation: 48547

The Connection Timeout in the ConnectionString only controls the timeout for the connection. If you need to increase the wait time on your Commands, use the CommandTimeout property of SqlCommand.

Upvotes: 23

Oppositional
Oppositional

Reputation: 11211

I am not seeing the behvior you are indicating, at least when I test the scenario using a SqlConnectionStringBuilder.

The ConnectionTimeout property of the connection matches the ConnectTimeout set on the builder. I used 5, 20 and 120 as values and all were set on the connection.

using System.Data.SqlClient;

var builder = new SqlConnectionStringBuilder();

builder.ApplicationName     = "My Demo App";
builder.ConnectTimeout      = 5;
builder.DataSource          = "(local)";
builder.InitialCatalog      = "My Database";
builder.IntegratedSecurity  = true;

using(var connection = new SqlConnection(builder.ConnectionString))
{
    Console.WriteLine("Connection Timeout: {0}", connection.ConnectionTimeout);
}

If you are not dealing with SQL, you might also look at using DbConnectionStringBuilder or one of its sub-types. In general I find it safest to build the connection string programmatically using one of the available builders so that you can avoid any bad formatting of the connection string you will be using to create connections.

Upvotes: 1

Related Questions