2011-09-02 52 views
4

我正在使用OLEDB连接到Sybase数据库,ADODB.dll文件版本为7.10.6070.0(来自Sybase 12.5软件包)。我需要能够打开连接,使用命令对象来填充存储过程中的记录集,然后关闭连接并传回断开连接的记录集。因为每次关闭连接,我的尝试都失败了,我的记录集也关闭(这意味着它没有断开连接)。如何在VB.NET Windows应用程序中创建ADODB断开连接的记录集?

是否有一个属性,我必须设置某处以指示记录集应该断开连接?我无法设置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. 
+0

使用Dataset对象是否可以接受?然后,您可以使用DataAdapter轻松填充它,而不必担心保持活动连接。 – N0Alias

+0

我必须从这个函数返回一个ADODB.Recordset - 它遍布整个地方。如果我可以使用ADO.NET做一些“现代”数据填充,那么用数据填充旧的ADODB.Recordset,这将是可以接受的,并避免我的问题。 –

回答

3

我不知道这是否会解决您的问题,但我做了谷歌搜索和本文Disconnect an ADO Recordset generated from a Command object,你可能能够使用修改代码来时如下:

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 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另一个例子具有相同的做法。

EDIT

充实的例子。

+0

我错过了记录集上的CursorLocation属性。那就是诀窍。谢谢! –

0

你可以尝试的东西沿着这条线

Set cmd = New ADODB.command 
With cmd 
    .ActiveConnection = "your connection" 
    .CommandText = "your proc or ssql" 
    .CommandType = adCmdStoredProc 
    ' .parameter.append create whether param you need 
End With 
Set rs = New ADODB.recordset 
With rs 
    .cursorlocation = adUseClient 
    .cursortype = adOpenStatic 
    .locktype = adLockBatchOptimistic 
    Set rs.Source = cmd 'this is the key 
.Open 
.ActiveConnection = Nothing 
End With 
' do what ever you need next 
相关问题