The connection timeout property in the connection string is ignored

I am creating an application in C #. I use the connection string, for example:

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

But no matter what value I set as x ( Connection Timeout = x ), setting a breakpoint, I see that the DbConnection object ConnectionTimeout property always has a default value of 15 .

Did I miss something?

Thanks.

+7
c # timeout connection-string
source share
3 answers

Connection Timeout in the ConnectionString controls only the connection timeout. If you need to increase the latency in your commands, use the CommandTimeout property for SqlCommand.

+14
source share

I do not see the pointer that you are pointing to, at least when I test the script using SqlConnectionStringBuilder .

The ConnectionTimeout property of the connection corresponds to the ConnectTimeout on the builder. I used 5, 20, and 120 as values, and they were all installed in 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 can also use DbConnectionStringBuilder or one of its subtypes. In general, I believe that it is safe to build the connection string programmatically using one of the available collectors, so that you can avoid the poor formatting of the connection string that you will use to create the connections.

+1
source share

Yes, 15 is the minimum, depending on what you do. Probably for the supplier. I even found that when I try to use less than 15 (with some third-party controls) using my own SQL client, I end up with endless timeouts. Ugh. When I set the value to 15 or higher, it behaves as expected.

-one
source share

All Articles