ADO Events with win32com

angusm nmack at pacbell.net
Wed Jul 25 15:59:44 EDT 2001


> 
> However, I now don't ever seem to receive the OnConnectComplete event. This
> may be an ADO issue, but I've tested it in VB and it works as documented, so I
> suspect something with the Python side...
> 
> Any suggestions?
> Paul.

You might want to wait for a more convincing explanation from someone
else but I was able to duplicate the problem and the code below worked
for me...it ain't
pretty :-).

(the code may not work for you exactly as is since I'm doing something
weird with the ADO import etc.)

I encountered two problems based on your code:
1)It appears the event class sees a different 'finished' variable from
what you define in your module. You might try this by opening normally
(without adAsyncConnect) and the thing will hang even though it gets
the OnConnectMessage. I couldn't even get it to work when I declared
'finished' global - I had to resort to something like
globals()['finished']=1. Not too sure on this one.

2)adAsyncConnect doesn't seem to work because the infinite loop that
gets created to wait for the events hogs the entire thread. There is
no way for the dispatched event to break in asynchronously... Windows
does some event dispatching via PostMessage(?) hence the code below
which creates a temporary message pump via win32gui.PumpMessages()
which seems to be the correct way to idle in this instance. The event
handler sends a PostQuitMessage() to break the idle loop in the main
program.

hope this helps,
Angus

import win32com.client
from  win32com.gen_py.ADO21 import *
import time
global finished 
finished = 0
import win32ui
from win32con import *
import win32gui 
class onevent:
    def __init__(self,val):
        self.v = val
    def OnInfoMessage(self, pError=defaultNamedNotOptArg,
adStatus=defaultNamedNotOptArg, pConnection=defaultNamedNotOptArg):
        pass
    def OnWillConnect(self, ConnectionString=defaultNamedNotOptArg,
UserID=defaultNamedNotOptArg, Password=defaultNamedNotOptArg,
Options=defaultNamedNotOptArg, adStatus=defaultNamedNotOptArg,
pConnection=defaultNamedNotOptArg):
        print 'willconnect'
    def OnExecuteComplete(self, RecordsAffected=defaultNamedNotOptArg,
pError=defaultNamedNotOptArg, adStatus=defaultNamedNotOptArg,
pCommand=defaultNamedNotOptArg, pRecordset=defaultNamedNotOptArg,
pConnection=defaultNamedNotOptArg):
        pass
    def OnRollbackTransComplete(self, pError=defaultNamedNotOptArg,
adStatus=defaultNamedNotOptArg, pConnection=defaultNamedNotOptArg):
        pass
    def OnBeginTransComplete(self,
TransactionLevel=defaultNamedNotOptArg, pError=defaultNamedNotOptArg,
adStatus=defaultNamedNotOptArg, pConnection=defaultNamedNotOptArg):
        pass	
    def OnDisconnect(self, adStatus=defaultNamedNotOptArg,
pConnection=defaultNamedNotOptArg):
        pass	
    def OnWillExecute(self, Source=defaultNamedNotOptArg,
CursorType=defaultNamedNotOptArg, LockType=defaultNamedNotOptArg,
Options=defaultNamedNotOptArg, adStatus=defaultNamedNotOptArg,
pCommand=defaultNamedNotOptArg, pRecordset=defaultNamedNotOptArg,
pConnection=defaultNamedNotOptArg):
        pass	
    def OnCommitTransComplete(self, pError=defaultNamedNotOptArg,
adStatus=defaultNamedNotOptArg, pConnection=defaultNamedNotOptArg):
        pass	
    def OnConnectComplete(self, pError=defaultNamedNotOptArg,
adStatus=defaultNamedNotOptArg, pConnection=defaultNamedNotOptArg):
        print 'hooray'
        win32gui.PostQuitMessage(0)





o = win32com.client.DispatchWithEvents('ADODB.Connection',onevent)
print constants.adAsyncConnect
o.Open('Provider=MSDASQL;DSN=oladb;UID=FOO;PWD=BAR;','','',constants.adAsyncConnect)
win32gui.PumpMessages()



More information about the Python-list mailing list