I contacted Microsoft support in China and they found a fix for this over the
weekend. Please see the revised code below.
Private Sub RestartPosition(ByVal adoRecordset As ADODB.Recordset)
Dim myAdoRecordsetConstruction As ADODB.ADORecordsetConstruction
myAdoRecordsetConstruction = CType(adoRecordset,
ADODB.ADORecordsetConstruction)
Dim myIRowset As IRowset
myIRowset = myAdoRecordsetConstruction.Rowset
myIRowset.RestartPosition(New
IntPtr(myAdoRecordsetConstruction.Chapter))
End Sub
<System.Runtime.InteropServices.ComImport(), _
System.Runtime.InteropServices.Guid("0C733A7C-2A1C-11CE-ADE5-00AA0044773D"), _
System.Runtime.InteropServices.InterfaceType(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)> _
Private Interface IRowset
<System.Obsolete("not defined", True)> Sub AddRefRows()
<System.Obsolete("not defined", True)> Sub GetData()
<System.Obsolete("not defined", True)> Sub GetNextRows()
<System.Obsolete("not defined", True)> Sub ReleaseRows()
'
http://msdn.microsoft.com/library/d...s/oledb/htm/oledbirowset__restartposition.asp
Sub RestartPosition(ByVal hChapter As System.IntPtr)
'alternate definition to see success codes is
'<System.Runtime.InteropServices.PreserveSigAttribute> function
RestartPosition(hChapter as System.IntPtr) as System.Int32
End Interface
Sub Main()
Dim myAdoConnection As ADODB.Connection = New ADODB.Connection
myAdoConnection.CursorLocation = ADODB.CursorLocationEnum.adUseClient
'change connection string as necessary
myAdoConnection.Open("my connection here.")
Dim myAdoRecordset As ADODB.Recordset = New ADODB.Recordset
myAdoRecordset.Open("SELECT * FROM titles", myAdoConnection,
ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1)
myAdoRecordset.ActiveConnection = Nothing
myAdoConnection.Close()
Console.WriteLine("Recordset.RecordCount: " &
myAdoRecordset.RecordCount)
Dim myDataTable As DataTable '= New DataTable
Dim rs As ADODB.Recordset
'fill datatable with ADODB.Recordset first time
myDataTable = AdoToDataTable(myAdoRecordset)
Console.WriteLine("(1) DataTable.Rows.Count: " &
myDataTable.Rows.Count)
'Add this line to reset position
RestartPosition(myAdoRecordset)
'fill datatable with ADODB.Recordset second time
myDataTable = AdoToDataTable(myAdoRecordset)
Console.WriteLine("(2) DataTable.Rows.Count: " &
myDataTable.Rows.Count)
'Add this line to reset position
RestartPosition(myAdoRecordset)
Console.ReadLine()
End Sub
Private Function AdoToDataTable(ByVal adoRecordset As ADODB.Recordset)
As DataTable
Dim myDataTable As DataTable = New DataTable
Dim myDataAdapter As OleDbDataAdapter = New OleDbDataAdapter
myDataAdapter.Fill(myDataTable, adoRecordset)
Return myDataTable
End Function
"Keith" เขียน: