天天看點

使用 Microsoft.ApplicationBlocks.Data SqlHelper 查詢逾時以及解決方案

 提示: 後面附有檔案,不喜歡看吐槽的,直接到文章結尾下載下傳

摘要:Data Access Application Block 是一個 .NET 元件,包含優化的資料通路代碼,可以幫助使用者調用存儲過程以及向 SQL Server 資料庫發出 SQL 文本指令。它傳回 SqlDataReader、DataSet 和 XmlReader 對象。您可以在自己的 .NET 應用程式中将其作為構造塊來使用,以減少需要建立、測試和維護的自定義代碼的數量。您可以下載下傳完整的 C# 和 Visual Basic .NET 源代碼以及綜合文檔。【這段是抄的】

故事:

最近在項目中使用到了這個古老的元件,一切都是那麼的美好,隻到昨天下午,當我用這個元件執行一個時間比較長的存儲過程時,厄運就來了:

1             try
2             {
3                 SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, sql);
4                 return 1;
5             }
6             catch (Exception ex)
7             {
8                 return -1;
9             }      

每當執行一段時間,自動就抛出Timeout的異常,好吧,作為一個有理智的程式員,馬上就去找怎麼給SqlHelper中的查詢自定義Timeout,然而……悲催的我竟然沒有找到,好吧,拿出反編譯工具看看有沒有Timeout字段,依然沒有

使用 Microsoft.ApplicationBlocks.Data SqlHelper 查詢逾時以及解決方案

那麼隻能借助網絡去找答案了,某度 呵呵,大量的轉載,特意在搜尋中加入了“Timeout”  “逾時” ,然并卵,萬能的過濾大發,把那些詞都忽略了。

再想想 google 哎……  後來隻能通過一些國内的“谷粉搜搜”之類的找一找了,雖然不多但還是找到了,都是E文的,然後借助自己很渣的E溫水瓶,成功沒找到答案。

好吧那隻能看看有沒有源代碼了,某度照樣渣,最後在一個外國網站上還真找到了源代碼,然後簡單修改

1         /// <summary>
 2         /// This method opens (if necessary) and assigns a connection, transaction, command type and parameters 
 3         /// to the provided command.
 4         /// </summary>
 5         /// <param name="command">the SqlCommand to be prepared</param>
 6         /// <param name="connection">a valid SqlConnection, on which to execute this command</param>
 7         /// <param name="transaction">a valid SqlTransaction, or 'null'</param>
 8         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
 9         /// <param name="commandText">the stored procedure name or T-SQL command</param>
10         /// <param name="commandParameters">an array of SqlParameters to be associated with the command or 'null' if no parameters are required</param>
11         private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
12         {
13             //if the provided connection is not open, we will open it
14             if (connection.State != ConnectionState.Open)
15             {
16                 connection.Open();
17             }
18 
19             command.CommandTimeout = Timeout;//注意這裡是我加的
20             //associate the connection with the command
21             command.Connection = connection;
22 
23             //set the command text (stored procedure name or SQL statement)
24             command.CommandText = commandText;
25 
26             //if we were provided a transaction, assign it.
27             if (transaction != null)
28             {
29                 command.Transaction = transaction;
30             }
31 
32             //set the command type
33             command.CommandType = commandType;
34 
35             //attach the command parameters if they are provided
36             if (commandParameters != null)
37             {
38                 AttachParameters(command, commandParameters);
39             }
40 
41             return;
42         }      

 編譯,引用,成功執行,一切又是那麼的美好。

使用 Microsoft.ApplicationBlocks.Data SqlHelper 查詢逾時以及解決方案

好吧,故事就到這了,特地把檔案分享出來,畢竟助人為快樂之本,

裡面有加入了Timeout字段并編譯好的檔案和 源碼安裝檔案(未修改)

 https://files.cnblogs.com/files/twzy/Microsoft.ApplicationBlocks.Data.zip

最後宣傳一下自己的抓包軟體

NetAnalyzer下載下傳位址

NetAnalzyer交流群:39753670 (PS 隻提供交流平台,群主基本不說話^_^)

[轉載請保留作者資訊  作者:馮天文  網址:http://www.cnblogs.com/twzy/p/4976867.html]

[轉載請保留作者資訊  作者:馮天文 ]