简体   繁体   中英

ADODB - how to get only the total number of records

仅获得记录总数的最佳方法是什么?

Dim oRS as new ADODB.Recordset
dim recordCount as Long

oRS.Open "Select * FROM [tablename]", myConnection, adOpenStatic, adLockReadOnly
If Not oRS.EOF Then recordCount = oRS.RecordCount

The key here is adOpenStatic . It allows .RecordCount to get an actual count of the records in the recordset.

Of course, if all you're after is how many records in a table:

Dim oRS as new ADODB.Recordset
dim recordCount as Long

oRS.Open "Select Count(*) FROM [tablename]", myConnection, adOpenForwardOnly, adLockReadOnly
If Not oRS.EOF Then recordCount = oRS(0).Value

Try one of these solutions.

SELECT * FROM <tablename>

With rs
  .CursorType = 3
  .CursorLocation = 3
  .Open sql, RecEngine  
  MsgBox .RecordCount & " records"
End With

Assuming an already open Connection (Done in QTP/UFT):

Set RecEngine = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.recordset")
RecEngine.Open <connectionString>
set rs = RecEngine.Execute("SELECT count(*) FROM <tablename>")
MsgBox rs(0)

Or using same open connection:

If rs.Supports(adApproxPosition) = TRUE Then
  i = rs.RecordCount
  MsgBox i  
  'MsgBox rs.Fields(0).Value & " records"  'If doing select * rather than select count(*)   
End If

ADOQuery, SQL= "select count(*) from mytable where xyz"?

rs.RecordCount?

i = 0
while not rs.eof
  i = i + 1
  rs.next?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM