我試圖避免發生超時情況。目前該腳本在測試環境中運行100%,但一切都在本地運行。因此,現在安裝在實時環境中,它需要更長的時間。但是,它會出現VB超時默認設置爲30秒。然而,不像SQL那樣100%熟悉VB,那麼我不確定要設置它的代碼。當前的代碼如下:SQL和VB腳本超時
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim dateRows As Variant
Dim i As Integer
Dim today As Date
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = Workbooks(1).FullName
strCon = "Provider=SQLOLEDB.1; Data Source=ABC;Initial catalog=ABC;Integrated Security=ABC;"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
range1 = Sheets("Line data (Do not alter)").Range("AA9")
cn.Open strCon
strSQL = "Select "order by userid, appointmentdate "
rs.Open strSQL, cn
Sheets("Line data (Do not alter)").Range("AA9").CopyFromRecordset rs
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
我發現代碼其中規定comm.CommandTimeout = 10,但不確定放在哪裏..
任何幫助表示讚賞。
在你的情況下,它會' cn.CommandTimeout = 10'(或者任何你想設定的超時時間),因爲cn是連接對象的變量,而CommandTimeout是這個對象的一個屬性。由於您使用的是早期綁定,因此您可以在'cn'後輸入'.'來查看Connection對象的所有屬性。 –
完美 - 謝謝。你知道如何編寫一個按鈕,如果我將它設置爲0並且它需要很長時間,將強制退出sql腳本? – HAS
*如果我將它設置爲0並且需要很長的時間* - 只要設置它,無論什麼時候您覺得「花費太長時間」而且您很棒。如果您想使其更清潔並繼續前進,而不會將錯誤傳遞給用戶,則還可能會出現錯誤信息。 –