EnterpriseLibrary SQL超時(Timeout expired)解決方法

 

今天在項目中遇到一個Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding SQL執行超時異常,在網上google了一下,大家都遇到過我種情況,我還是第一次遇到,影響服務器產生超時的設置大致有:
1. Server.scrīptTimeout,
2. Connection
對象的CommandTimeOut屬性
,
3. Command
對象的CommandTimeOut屬性
,
4. IE
瀏覽器的設置
.
Server.scrīptTimeout,
默認值是90
.
要增大它,在你的asp文件中加一句,如下
:
Server.scrīptTimeout=999,
將頁面超時設爲999.

最初我只設置Server.scrīptTimeout,
但仍會出現timeout錯誤,無論它的值設成都多大
.
後在社區裏看到一帖子,提到commandTimeout屬性
,
於是查看Option Pack文檔,果然還有其他的
timeout.
Connection
對象和Command對象都有個CommandTimeOut屬性
,
連接字符串中設置了 Connect Timeout只對SqlConnection起作用。

SqlCommand.CommandTimeout
獲取或設置在終止執行命令的嘗試並生成錯誤之前的等待時間。
等待命令執行的時間(以秒爲單位)。默認爲 30 秒。
SqlConnection.ConnectionTimeout
獲取在嘗試建立連接時終止嘗試並生成錯誤之前所等待的時間。
等待連接打開的時間(以秒爲單位)。默認值爲 15 秒。

SqlHelper這一點不是很讓人滿意啊,

最後我加了一個 SqlCommand.CommandTimeout屬性,結果運行正常,問題解決.
/// <summary>
執行命令超時時間
</summary>
private const int TIMEOUT = 999;

public static DataSet GetDataSetByStoredProc(string sqlStoredProcName, List<SqlParameter> parameters)
        {
            DataSet ds = new DataSet();
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = null;
                dbCommand = db.GetStoredProcCommand(sqlStoredProcName);
                dbCommand.CommandTimeout = TIMEOUT;
                if (parameters != null)
                {
                    Addparameter(db, dbCommand, parameters);
                }
                ds = db.ExecuteDataSet(dbCommand);
                if (parameters != null)
                {
                    FillOutParameter(db, dbCommand, parameters);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return ds;
        }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章