我需要爲特定的SqlCommand
設置CommandTimeout
屬性。前提是,主要連接字符串和SqlConnection
正在使用不能更改。我喜歡定義超時值的可配置。想知道它的良好和公平的做法,以定義它在app.config(這是一個桌面應用程序)?從配置文件中定義SqlCommand.CommandTimeout
0
A
回答
3
app.config
對於這種設置是一個很棒的地方。也許是這樣的:
<appSettings>
<add key="commandTimeout" value="2" />
</appSettings>
然後,這將在一定的角度持續時間,基於你如何利用它。也許是這樣的:
var timeout = cnn.ConnectionTimeout;
var configTimeout = ConfigurationManager.AppSettings["commandTimeout"];
if (!string.IsNullOrEmpty(configTimeout))
{
timeout = Convert.ToInt32(configTimeout);
}
這當然可以存在於一個靜態類,這樣也許:
public static class AppSettings
{
private static int _commandTimeout = -1;
public static int CommandTimeout
{
get
{
if (_commandTimeout != -1) { return _commandTimeout; }
var configTimeout = ConfigurationManager.AppSettings["commandTimeout"];
if (!string.IsNullOrEmpty(configTimeout))
{
_commandTimeout = Convert.ToInt32(configTimeout);
}
else
{
_commandTimeout = 1; // this is the default if the setting doesn't exist
}
return _commandTimeout;
}
}
}
然後,所有你需要做的是:
var timeout = AppSettings.CommandTimeout;
或者更簡潔:
cmd.CommandTimeout = AppSettings.CommandTimeout;
+0
SqlCommand.CommandTimeout屬性的默認超時是30秒:) – rageit
+0
@rageit,這是真的,我只是把數字投入我的例子。 –
0
你c在應用程序配置文件中使用CommandTimeout屬性和Time Paramater。
connection = Factory.CreateConnection();
connection.ConnectionString = ConnectionString.ConnectionString;
connection.Open();
this.cmd = connection.CreateCommand();
cmd.CommandTimeout = ConfigurationManager.AppSettings["TimeConfigParam"];
相關問題
- 1. 從C#自定義配置文件
- 2. Codeigniter:從自定義庫中獲取自定義配置文件?
- 3. 自定義配置文件
- 4. asp.net中的自定義配置文件
- 5. junit配置文件中的bean定義
- 6. Rails - 環境配置文件中的自定義配置?
- 7. 從配置文件中定義數據結構
- 8. 配置未定義特定的配置文件
- 9. Maven - 我可以在配置文件定義中引用配置文件ID嗎?
- 10. AppHarbor自定義配置文件轉換
- 11. 解析自定義XML配置文件
- 12. 定義Maven的配置文件POM
- 13. Restlet自定義配置屬性文件
- 14. Rails OAuth配置文件圖片定義
- 15. 用戶配置文件自定義css
- 16. CKEDITOR與CKFinder自定義配置文件
- 17. 自定義配置文件解析
- 18. 自定義配置文件屬性
- 19. 自定義配置文件錯誤
- 20. JobDSL自定義配置文件
- 21. 自定義Drupal用戶配置文件
- 22. Codeigniter創建自定義配置文件
- 23. PassportJS配置文件未定義名稱
- 24. 定義sourceDirectory在Maven的配置文件
- 25. ASP.NET自定義配置文件錯誤
- 26. Selenium - 自定義Firefox配置文件
- 27. 笨 - 自定義配置文件
- 28. 加載自定義配置文件
- 29. 自定義配置文件asp.net
- 30. 未定義的顏色配置文件
有點主觀的問題,但我沒有看到它的問題。 –
但是,您是否想要更改應用程序中使用的每個SqlCommand的超時時間或者只是爲了一個命令? – Steve
@KevinDiTraglia而不是一個絕對的問題,它只是我的一個難題,並且想知道解決它的最佳方法:) – rageit