ADODBAPI与数据库的开放连接数 [英] ADODBAPI No. Of Open Connection with database

查看:113
本文介绍了ADODBAPI与数据库的开放连接数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想算不算. ms-access数据库当前打开的连接数.

例如,两个应用程序正在使用同一个数据库.那我该如何计算呢? pypyodbc中有ms-access功能或任何功能吗?

For example two applications are working with the same database. Then how can I get this count? Is there is ms-access function or any facility in pypyodbc?

使用adodbapi,我怎么会没有.与数据库的开放连接?

Using adodbapi, how can I get no. of open connections with a database??

我尝试了以下代码.

#importing adodbapi 
import adodbapi # success 
#connection to database using the DSN 'test'
myConn = adodbapi.connect('test') # success
#get no. of open connection using openschema 
myConn.connector.OpenSchema(-1, None,"{947bb102-5d43-11d1-bdbf-00c04fb92675}") #fail

出现以下错误.

pywintypes.com_error:(-2147352567,'发生了异常.',(0,u'ADODB.Connection',u'对象或提供者不具备
执行请求的操作.',u'C:\ WINDOWS \ HELP \ ADO270.CHM',
1240648,-2146825037),无)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'ADODB.Connection', u'Object or provider is not capable of
performing requested operation.', u'C:\WINDOWS\HELP\ADO270.CHM',
1240648, -2146825037), None)

有人可以提供解决方案吗?

Can anybody provide solution?

推荐答案

在这种情况下,我个人倾向于避免对adodbapi进行大惊小怪,而只是让我的Python脚本编写一些VBScript来创建使用制表符分隔的计算机列表打开连接,通过subprocess.Popen运行VBScript,然后解析结果:

Personally I would be inclined to avoid fussing with adodbapi in this case and just have my Python script write a little VBScript to create a tab-separated list of machines with open connections, run the VBScript via subprocess.Popen, and parse the results:

import os
import subprocess

## test data
databaseFileSpec = r"Z:\pyTest.mdb"

vbsFileSpec =  os.environ['TEMP'] + r"\mypytemp.vbs"

scriptCode = """Option Explicit
Dim con, rst, strOut, strSeparator
Const adSchemaProviderSpecific = -1
Set con = CreateObject("ADODB.Connection")
con.Open( _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source="""
scriptCode += databaseFileSpec
scriptCode += """")
Set rst = con.OpenSchema( _
        adSchemaProviderSpecific, _
        , _
        "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
strOut = ""
strSeparator = ""
Do While Not rst.EOF
    If rst(2).Value = "True" Then
        strOut = strOut & strSeparator & Left(rst(0).Value, Len(Trim(rst(0).Value)) - 1)
        strSeparator = vbTab
    End If
    rst.MoveNext
Loop
WScript.Echo strOut
rst.Close
con.Close"""

f = open(vbsFileSpec, 'w')
f.write(scriptCode)
f.close()

tabString = subprocess.Popen(
    "cscript /nologo \"" + vbsFileSpec + "\"",
    shell=True,
    stdout=subprocess.PIPE).stdout.read()
os.remove(vbsFileSpec)

print 'The following machines are connected to "' + databaseFileSpec + '":'
for x in tabString.split("\t"):
    print x

当我在两台不同的计算机上打开数据库并运行上面的脚本时,我得到

When I have the database open on two different machines and run the above script I get

The following machines are connected to "Z:\pyTest.mdb":
TESTPC
GORD01
GORD01

我的笔记本(GORD01)显示两次,因为我在Access中打开了数据库,而VBScript在运行时也打开了连接.

My notebook (GORD01) shows up twice because I have the database open in Access and the VBScript also has a connection open while it is running.

这篇关于ADODBAPI与数据库的开放连接数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆