2010-06-10 61 views
2

當SQL批處理返回來自例如多個消息的多條消息時。打印語句,那麼我只能使用ADO連接的Errors集合檢索第一個。我如何獲得剩餘的消息?如何使用ADO從查詢中檢索所有錯誤和消息

如果我運行此腳本:

Option Explicit 
Dim conn 
Set conn = CreateObject("ADODB.Connection") 
conn.Provider = "SQLOLEDB" 
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master" 
conn.Open 

conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)") 

Dim error 
For Each error in conn.Errors 
    MsgBox error.Description 
Next 

然後我只得到 「富」 回來了,從來沒有 「酒吧」 或 「XYZ」。

有沒有辦法得到剩餘的消息?

回答

6

我想出了我自己的想法。

這工作:

Option Explicit 
Dim conn 
Set conn = CreateObject("ADODB.Connection") 
conn.Provider = "SQLOLEDB" 
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master" 
conn.Open 

Dim rs 
Set rs = conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)") 

Dim error 
While not (rs is nothing) 
    For Each error in conn.Errors 
     MsgBox error.Description 
    Next 
    Set rs = rs.NextRecordSet 
Wend 
+1

我知道我的復活死去很久的線程,但是這是非常有幫助我。我不得不更新一個糟糕的傳統VbScript,它將數據提供給SQL數據庫,即使我陷入了'Err'對象,我從來沒有看到任何錯誤。看起來我不得不將這種錯誤檢查添加到我的SQL命令中。謝謝! – 2017-07-11 15:53:43

相關問題