c# - How to use try catch error for sql query timeout? -
how can use try catch if there error in connection sql query in c#?
if use simple code below work time takes error occur long. how can try catch handle error quicker?
try { //sql command } catch { //display connection error }
try-catch
catch exception occurs, don't except catch timeout exception before timeout occurs. sqlcommand
, default timeout 30 seconds, can change setting commandtimeout
property:
try { var command = new sqlcommand("..."); command.commandtimeout = 5; // 5 seconds command.executenonquery(); } catch(sqlexception ex) { // handle exception... }
Comments
Post a Comment