2011-02-16 46 views
0

好吧,我有一個在try..catch使得:包含在try..catch環路

Try 
    Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & table, myConn) 
Catch ex As Exception 
    sheet = InputBox("Re-enter table:") 
    Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & table, myConn) 
End Try 

以上將只對一審工作。 我想要做的是繼續顯示輸入框,直到博士成功(即數值已從數據庫中檢索到)

我知道我需要某種時間在捕獲,但我不確定如何繼續

回答

2

從catch塊內部刪除performSelect,否則你也可能在那裏拋出一個異常,它需要被捕獲等等。把整個try catch塊放到while循環中,並且當你有一個值。表

+0

+1:看起來像一個愚蠢的問題..很容易。感謝您的解決方案。 – waqasahmed 2011-02-16 20:31:14

1

這可能會實現:

Dim inputOk As Boolean = False 

    Do Until inputOk = True 
     Try 
      Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & table, myConn) 
      inputOk = True 
     Catch ex As Exception 
      sheet = InputBox("Re-enter table:") 

     End Try 
     End 
    Loop 
2

看一看here爲什麼這不是一個好主意。如果你想要更多(無限 - > < = 0),改變

Dim retries As Integer = 3 
    While True 
     Try 
      Dim sheet as String = InputBox("Enter table:") 
      Dim dr As OleDbDataReader = performSelect("SELECT * FROM " & sheet, myConn) 
      Exit While 
     Catch 
      retries -= 1 
      If retries = 0 Then 
       Throw 
      Else 
       System.Threading.Thread.Sleep(1000) 
      End If 
     End Try 
    End While 

更改或重試,如果要重試刪除了Thread.Sleep:

如果你仍然有興趣,你可以試試這個立即。

0

捕獲塊的設計目的是處理異常,而不是執行數據庫查詢......您應該使用Jeff的方式。我寫的是一樣的,但他比我更快