2011-09-02 54 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 
相關問題