posts - 43,  comments - 20,  trackbacks - 0

确保发生异常时记录集被关闭

 1 #-*- coding: utf-8
 2 # python 3.0 required
 3 """确保发生异常时记录集被关闭"""
 4 
 5 from win32com.client import Dispatch
 6 try:
 7     rs=Dispatch("ADODB.Recordset")
 8     rs.ActiveConnection="PROVIDER=SQLOLEDB;"
 9     rs.Source="select * from ATable"
10     rs.Open()
11     while not rs.EOF:
12        #DO SOME WORKS
13         rs.MoveNext()
14 except Exception as e:
15     print(e)
16 finally:
17     if 'rs' in dir():
18         if rs.State==1:
19             rs.Close()
20         del rs

 

posted on 2009-03-11 12:54 玛瑙河 阅读(213) 评论(0) 编辑 收藏