{"id":221,"date":"2009-09-08T12:51:35","date_gmt":"2009-09-08T10:51:35","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=221"},"modified":"2009-09-08T12:51:35","modified_gmt":"2009-09-08T10:51:35","slug":"force-sql-command-timeout-and-catch-it","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=221","title":{"rendered":"Force SQL Command Timeout and catch it"},"content":{"rendered":"<p>Sometimes you want to catch and specially handle an SQL-Timeout.<br \/>\nTo do this, just put a try-catch around your SQL-Call and catch the <code>SqlException<\/code>. Now check the Property <code>Number<\/code> if it is -2 (which stands for Timeout). That&#8217;s it.<\/p>\n<p>Here&#8217;s a little Code Example how that could look like:<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\r\ntry\r\n{\r\n\tRunSomeSqlQuery();\r\n}\r\ncatch (SqlException ex)\r\n{\r\n\t\/\/ Check if it's a TimeoutException\r\n\tif (ex.Number == -2)\r\n\t{\r\n\t\t\/\/ It is...\r\n\t\t\/\/ Do whatever you need\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/ Other Type of Exception, re-throw it or whatever\r\n\t\tthrow;\r\n\t}\r\n}\r\n<\/pre>\n<p>If you want to simulate a Timeout in your SQL-Statement, just use the <code>WAITFOR DELAY<\/code> SQL-Statement to block for some Time and therefore results in a Timeout.<\/p>\n<p>Here&#8217;s an example to wait for 1 Minute:<\/p>\n<pre class=\"brush: sql; gutter: false; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\r\nWAITFOR DELAY '00:01:00'\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to catch and specially handle an SQL-Timeout. To do this, just put a try-catch around your SQL-Call and catch the SqlException. Now check the Property Number if it is -2 (which stands for Timeout). That&#8217;s it. Here&#8217;s a little Code Example how that could look like: try { RunSomeSqlQuery(); } catch (SqlException &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3,12],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"post-221","6":"format-standard","7":"category-csharp","8":"category-programming","9":"category-sql"},"acf":[],"views":8592,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/221"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=221"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/221\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}