0

我有一个连接到 Oracle 数据库的大型 VB 程序。

strCn = "Driver={Microsoft ODBC for Oracle};" & _
        "SERVER=PSPROD;"

Set Cn = New ADODB.Connection
Cn.ConnectionString = strCn
Cn.CursorLocation = adUseNone
Cn.Open

我的程序有很多用户,所以我有一个表,其中包含每个用户的登录名和他们对各个表的访问权限。我在程序启动时创建了所有用户的记录集,然后从找到 USERNAME 和 PASSWORD 的记录集中选择 USERNAME 和 GRANTED_ROLE。我使用“设置角色'GRANTED_ROLE'由'password'标识”语句和Cn.Execute 语句来设置用户的访问权限。这一切都在一个模块中完成。

在一个表单上,我想调用一个存储过程,它将 SELECT、INSERT 和 UPDATE 信息放入另一个模式的表中。当我使用以下代码创建与数据库的新连接时,我可以调用并运行存储过程:

暗淡 cmd5040 作为 ADODB.Command 暗淡 conn5040 作为 ADODB.Connection 暗淡 param5040 作为 ADODB.Parameter

Set conn5040 = New ADODB.Connection conn5040 = "Driver={Microsoft ODBC for Oracle};" & _ "SERVER=PSPROD;UID=XXXXXXX;PWD=XXXXXXXX" conn5040.Open

设置 cmd5040 = 新的 ADODB.Command

使用 cmd5040 .ActiveConnection = conn5040 .CommandType = adCmdStoredProc .CommandText = "S4115040_IMPORT_NEWBIDITEMSPES.S4115040_CheckTime"

.Parameters.Append .CreateParameter(, adInteger, adParamInputOutput, 5)
.Parameters.Append .CreateParameter(, adVarChar, adParamInputOutput, 400)

结束于

cmd5040(0) = 0 cmd5040(1) = "" cmd5040.CommandTimeout = 300

cmd5040.执行conn5040.关闭

但是,当我在程序首次启动时尝试使用相同的连接 ('Cn') 时,会收到错误消息“-2147217900 [Microsoft][ODCB driver for Oracle]语法错误或访问冲突”。我的代码是:

暗淡 cmd5040 作为 ADODB.Command 暗淡 param5040 作为 ADODB.Parameter

设置 cmd5040 = 新的 ADODB.Command

使用 cmd5040 .ActiveConnection = Cn .CommandType = adCmdStoredProc .CommandText = "S4115040_IMPORT_NEWBIDITEMSPES.S4115040_CheckTime"

.Parameters.Append .CreateParameter(, adInteger, adParamInputOutput, 5)
.Parameters.Append .CreateParameter(, adVarChar, adParamInputOutput, 400)

结束于

cmd5040(0) = 0 cmd5040(1) = ""

cmd5040.执行

我和我的 DBA 一起工作过。她给了我直接授权和直接执行权限,但我仍然收到错误消息。

我究竟做错了什么?我应该能够使用原始连接来运行存储过程吗?还是我必须创建第二个连接?

4

1 回答 1

0

edit: on reviewing your code, I notice that the original connection Cn specifies the driver and the server name whereas the second connection conn5040 specifies the driver, server name, user and password.

Therefore, it may be that the stored procedure you are calling requires a user and password which the original cn connection does not specify


Original answer:

Make sure that the variable cn is still in scope when you try to use it. If it is declared in a module then it should be declared outside of any Sub or Function and, if other modules should be able to access it, it should be declared as Public

Option Explicit

Public cn as ADODB.Connection

Sub foo()
...

Presuming that cn is still in scope, you could examine the State property of the object which cn references to see if the Connection is still open.

If (cn.State = adStateClosed) Then
    ' we have a problem
    ...
于 2010-01-09T03:55:56.443 回答