此頁沒有內(nèi)容條目
內(nèi)容

State 屬性范例

該范例使用 State 屬性,在異步連接正在打開和異步命令正在執(zhí)行時顯示消息。

Public Sub StateX()

   Dim cnn1 As ADODB.Connection

   Dim cnn2 As ADODB.Connection

   Dim cmdChange As ADODB.Command

   Dim cmdRestore As ADODB.Command

   Dim strCnn As String

   ' 打開兩個異步連接,在連接時顯示消息。

   Set cnn1 = New ADODB.Connection

   Set cnn2 = New ADODB.Connection

   strCnn = "Provider=sqloledb;" & _

      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "

   cnn1.Open strCnn, , , adAsyncConnect

   While (cnn1.State = adStateConnecting)

      Debug.Print "Opening first connection...."

   Wend

   cnn2.Open strCnn, , , adAsyncConnect

   While (cnn2.State = adStateConnecting)

      Debug.Print "Opening second connection...."

   Wend

   ' 創(chuàng)建兩個命令對象。

   Set cmdChange = New ADODB.Command

   cmdChange.ActiveConnection = cnn1

   cmdChange.CommandText = "UPDATE titles SET type = 'self_help' " & _

      "WHERE type = 'psychology'"

   Set cmdRestore = New ADODB.Command

   cmdRestore.ActiveConnection = cnn2

   cmdRestore.CommandText = "UPDATE titles SET type = 'psychology' " & _

      "WHERE type = 'self_help'"

   ' 執(zhí)行命令,在正在執(zhí)行時顯示消息。

   cmdChange.Execute , , adAsyncExecute

   While (cmdChange.State = adStateExecuting)

      Debug.Print "Change command executing...."

   Wend

   cmdRestore.Execute , , adAsyncExecute

   While (cmdRestore.State = adStateExecuting)

      Debug.Print "Restore command executing...."

   Wend

   cnn1.Close

   cnn2.Close

End Sub