2011-05-17 135 views
2

我正在使用以下代碼,並想知道是否需要設置命令超時如果使用企業庫的CreateSprocAccessor,如果不是,那麼如何管理超時?企業庫5.0和命令超時

var accessor = _sqlDatabase.CreateSprocAccessor<xyz>("uspGetxyz", 
        new xyzParameters(_sqlDatabase), 
        MapBuilder<xyz>.MapAllProperties().Build()); 

//Execute the accessor to obtain the results 
var Data = accessor.Execute(); 
xyzList = Data.ToList<xyz>(); 

回答

0

Volla !!!我已經對企業庫的源代碼進行了更改,添加了一個新的「執行」方法,這將需要timeOut參數,在Sproc訪問器類中,並在我的項目中使用這些二進制文件

2

我不能相信它的企業庫隊取得了什麼大錯,他們沒有得到任何的方式來設置命令超時的訪問器的情況下,這是一個已知問題與他們 http://entlib.codeplex.com/workitem/28586 不敢相信它,我已經開發了整個項目,並剛剛知道這是一個知道的問題:-(wtf

2

我已經開始使用Microsoft Enterprise Library,在正常情況下,使用「數據庫」類提供的方法調用數據庫操作時會滿足需要。在某些情況下,對於長時間運行的查詢,開發人員想要設置SqlCommand(或DbCommand)類的CommandTimeout屬性。這將允許查詢作爲命令超時設置的值被長時間執行。

默認情況下數據訪問應用程序塊不支持/在方法調用中使用簡單的CommandTimeout參數(網上有許多可用的解決方案樣本)。爲了以最小的變化實現同樣的效果,我在「Microsoft.Practices.EnterpriseLibrary.Data.Database」類中添加了一個名爲「WithCommandTimeOut」的簡單函數,該函數返回「Database」類的相同實例的timeOutSecond參數。請參閱下面更新的代碼片段以進行代碼更改。希望這將解決超時問題。

//Class Level Static Variables 
//Used to reset to default after assigning in "PrepareCommand" static method 
static int DEFAULT_COMMAND_TIMEOUT_RESET = 30; 

//Default value when "WithCommandTimeOut" not called 
static int COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET; 

public Database WithCommandTimeOut(int timeOutSeconds) 
{ 
    COMMAND_TIMEOUT_FOR_THIS_CALL = timeOutSeconds; 
    return this; 
} 

protected static void PrepareCommand(DbCommand command, DbConnection connection) 
{ 
    if (command == null) throw new ArgumentNullException("command"); 
    if (connection == null) throw new ArgumentNullException("connection"); 

    //Here is the magical code ---------------------------- 
    command.CommandTimeout = COMMAND_TIMEOUT_FOR_THIS_CALL; 
    //Here is the magical code ---------------------------- 

    command.Connection = connection; 

    //Resetting value to default as this is static and subsequent 
    //db calls should work with default timeout i.e. 30 
    COMMAND_TIMEOUT_FOR_THIS_CALL = DEFAULT_COMMAND_TIMEOUT_RESET; 
} 

Ex。 Database db = EnterpriseLibraryContainer.Current.GetInstance(Of Database)(「SmartSoftware」); db.WithCommandTimeOut(0).ExecuteDataSet(CommandType.Text,query);

1

您可以修改DbCommand超時在xyzParametersAssignParameters方法:

public void AssignParameters(
    System.Data.Common.DbCommand command, object[] parameterValues) 
    { 
    command.CommandTimeout = 0; 
    ... 
    } 
1

我們可以在連接字符串中更新此,增加連接超時= 1000;

+0

這可能是最好的答案 - 你可以在連接字符串中設置它,我已經做了很多次,它的工作原理與廣告一樣。然而,答案有點不對 - 它實際上是連接超時= xxx,而不是連接超時= xxx。 Data Source = myDbServer; Database = MyDB; Persist Security Info = True; User ID = MyUser; Password = MyPassword; MultipleActiveResultSets = true; Connect Timeout = 200; Max Pool Size = 200; – Jim 2017-10-19 21:09:38