简体   繁体   English

如果数据库需要太长时间才能响应,则如何退出应用程序

[英]How to exit application if database takes too long to respond

i have database query: datareader.execQuery("select * from table"); 我有数据库查询: datareader.execQuery("select * from table"); However sometimes database takes too long to respond and is stuck at this line. 但是,有时数据库需要很长时间才能响应,并且停留在这一行。 if this happens my application should exit 如果发生这种情况,我的应用程序应该退出

Thanks guys but i need to know: Is there any way i can achieve this using Stopwatch or Timer?? 谢谢大家,但我需要知道:使用秒表或计时器有什么方法可以实现?

You should set the CommandTimeout on your SqlCommand object. 您应该在SqlCommand对象上设置CommandTimeout。 If the timeout is exceeded, you will get an exception. 如果超过了超时,您将获得一个异常。 You can catch this, and exit your app, if that is what you want to do. 如果您要这样做,则可以捕获该错误并退出您的应用程序。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx http://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx

"Gets or sets the wait time before terminating the attempt to execute a command and generating an error." “获取或设置在终止执行命令并生成错误之前的等待时间。”

A cleaner way will be to set the SqlCommand.CommandTimeout property by default it is 30 secs 较干净的方法是将SqlCommand.CommandTimeout属性设置为默认30秒

You will have to handle the TimeOut Exception 您将必须处理TimeOut异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM