connection timeout property in connection string ignored

大憨熊 提交于 2019-12-10 12:45:47

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2265733/connection-timeout-property-in-connection-string-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!