ADODB - 如何只获取记录总数

时间:2011-08-24 23:12:54

标签: sql adodb

获取JUST总记录数的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

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

这里的关键是adOpenStatic。它允许.RecordCount获取记录集中记录的实际计数。

当然,如果你所追求的只是表中的记录数:

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

答案 1 :(得分:0)

ADOQuery,SQL =“从mytable中选择count(*)xyz”?

rs.RecordCount?

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

答案 2 :(得分:0)

尝试以下解决方案之一。

SELECT * FROM <tablename>

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

假定连接已经打开(在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)

或使用相同的打开连接:

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