我已經開始使用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);
這可能是最好的答案 - 你可以在連接字符串中設置它,我已經做了很多次,它的工作原理與廣告一樣。然而,答案有點不對 - 它實際上是連接超時= 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