State 属性范例

该范例使用 State 属性,在异步连接正在打开和异步命令正在执行时显示消息。

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
   
   ' 创建两个命令对象。
   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'"
   
   ' 执行命令,在正在执行时显示消息。
   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.CloseEnd Sub
www.holmesian.org