提示: 后面附有文件,不喜歡看吐槽的,直接到文章結尾下載
摘要: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字段,依然沒有
那么只能借助網絡去找答案了,某度 呵呵,大量的轉載,特意在搜索中加入了“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 }
編譯,引用,成功執行,一切又是那么的美好。
好吧,故事就到這了,特地把文件分享出來,畢竟助人為快樂之本,
里面有加入了Timeout字段並編譯好的文件和 源碼安裝文件(未修改)
http://files.cnblogs.com/files/twzy/Microsoft.ApplicationBlocks.Data.zip
最后宣傳一下自己的抓包軟件
NetAnalzyer交流群:39753670 (PS 只提供交流平台,群主基本不說話^_^)
[轉載請保留作者信息 作者:馮天文 網址:http://www.cnblogs.com/twzy/p/4976867.html]