简体   繁体   中英

How to exit application if database takes too long to respond

i have database query: 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. 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

"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

You will have to handle the TimeOut Exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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