2013-12-19 44 views
8

web.configの接続文字列にCommandTimeoutを追加するにはどうすればよいですか?web.configの接続文字列にCommandTimeoutを追加する方法

私が試した:

<add name="ConnectionString" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;User ID=sa;[email protected];Connect Timeout=200" providerName="System.Data.SqlClient"/> 

</connectionStrings> 

と、この:

<add name="MyProject.ConnectionString" 
     connectionString="Data Source=127.0.0.1;Initial Catalog=MyDB;Persist Security Info=True;CommandTimeout=60;User ID=sa;Password=saPassw0rd" 
     providerName="System.Data.SqlClient" /> 

を、それは私のための仕事をdidntの。

おかげ

答えて

8

私はこのようにそれを作った:

private readonly MyDbContext _context; 

    public LinqToSql() : this(new MyDbContext()) 
    { 
    } 

    private LinqToSql(MyDbContext context) 
    { 
     _context = context; 
     _context.CommandTimeout = 500; 

    } 
6

は、私の知る限りでは、コマンドタイムアウトプロパティを設定するグローバルな方法はありません、あなたが作成し、各コマンドオブジェクトごとに個別にCommandTimeoutプロパティを設定する必要があります。

1

あなたが設定でタイムアウトを設定し、コマンドのタイムアウトが設定されている場合、その値を参照することができます。 appSettingsの下の設定で
はCommandTimeoutのためのキーを追加します。

<add key="ContextCommandTimeout" value="500" /> 

次に、あなたのコード内で:

int cmdTimeout = -1; 
string timeoutSettings = ConfigurationManager.AppSettings["ContextCommandTimeout"]; 

if(!string.IsNullOrEmpty(timeoutSettings)) 
{ 
    int.TryParse(timeoutSettings, out cmdTimeout); 
} 

if(cmdTimeout >=0) 
    _context.CommandTimeout = cmdTimeout;  
-1

あなたのweb.configファイルに入れたい場合は、スペースを追加する必要があります。試してみてください:

Command Timeout=60; 

の代わり:

CommandTimeout=60; 
関連する問題