加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在VB.NET Windows应用程序中创建ADODB断开连接的记录集?

发布时间:2020-12-17 07:13:17 所属栏目:百科 来源:网络整理
导读:我正在使用与Sybase数据库的OLEDB连接,ADODB.dll文件版本为7.10.6070.0(来自Sybase 12.5软件包).我需要能够打开连接,使用命令对象从存储过程填充记录集,然后关闭连接并传回断开连接的记录集.到目前为止,我的尝试都失败了,因为每次关闭连接时,我的记录集也会
我正在使用与Sybase数据库的OLEDB连接,ADODB.dll文件版本为7.10.6070.0(来自Sybase 12.5软件包).我需要能够打开连接,使用命令对象从存储过程填充记录集,然后关闭连接并传回断开连接的记录集.到目前为止,我的尝试都失败了,因为每次关闭连接时,我的记录集也会关闭(这意味着它没有断开连接).

是否有一个属性我必须在某处设置以指示记录集应该断开连接?我无法设置Recordset.ActiveConnection = False,因为我得到一个异常(“无法更改具有Command对象作为其来源的Recordset对象的ActiveConnection属性.”).我设置Command.ActiveConnection = False,但是一旦我关闭连接对象,就不会停止记录集关闭.

片段:

Dim conn as New ADODB.Connection()
conn.Open("connectionString","UserID","Password")
Dim cmd as New ADODB.Command()
' Set some parameters on the command.
cmd.ActiveConnection = conn
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
Dim rs as New ADODB.Recordset()
rs.Open(cmd)
Dim clonedRS as ADODB.Recordset = rs.Clone()  ' one attempt to disconnect recordset
rs.Close()  ' Does not close cloned recordset
cmd.ActiveConnection = Nothing   ' another try at disconnecting recordset
conn.Close()  ' Always closes the recordset,even the cloned one
return clonedRS  ' Sadly,this is closed now.

解决方法

我不知道这是否可以解决您的问题,但我进行了Google搜索并找到了这篇文章 Disconnect an ADO Recordset generated from a Command object,您可以使用它来修改您的代码,如下所示:

Dim conn as New ADODB.Connection()
conn.Open("connectionString","Password")
Dim cmd as New ADODB.Command()
' Set some parameters on the command.
cmd.ActiveConnection = conn
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc

Dim rs As ADODB.Recordset

With rs
    .CursorLocation = adUseClient
    .Open cmd,CursorType:=adOpenStatic,Options:=adCmdStoredProc
    Set .ActiveConnection = Nothing
End With

Dim clonedRS As ADODB.Recordset = rs

Set cmd = Nothing

conn.Close()
rs.Close()
Set conn = Nothing
Set rs = Nothing

Return clonedRS

4GuysFromRolla Using Disconnected Recordsets的另一个例子具有相同的方法.

编辑

充实了这个例子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读