VBS确定SQL表中的记录数

VBS确定SQL表中的记录数,sql,vbscript,Sql,Vbscript,如何确定SQL表中的记录数,然后将该值保存在变量中?使用访问数据库: 从myTable查询执行简单的SELECT count0。 如果结果集不为空,则访问结果集并读取返回值。 未测试代码: Dim conn, rs, recordsCount recordsCount = -1 'initialize the connection set conn = ... 'run the query and retrieve the results set rs = conn.execute("SEL

如何确定SQL表中的记录数,然后将该值保存在变量中?

使用访问数据库:

从myTable查询执行简单的SELECT count0。 如果结果集不为空,则访问结果集并读取返回值。 未测试代码:

Dim conn, rs, recordsCount
recordsCount = -1

'initialize the connection
set conn = ...

'run the query and retrieve the results
set rs = conn.execute("SELECT count(0) as cnt FROM myTable")
if not rs.EOF then
  recordsCount = cint(rs("cnt"))
end if


'cleanup
rs.close
conn.close

set rs = nothing
set conn = nothing

获取查询记录集/结果大小的三种方法:

Option Explicit

' simple way to get a connection
Dim oDb : Set oDb = CreateObject("ADODB.Connection")
oDb.Open "dsn=NWIND"

' execute & !obtain result! of "SELECT COUNT()" query
WScript.Echo "Select Count(*):", oDb.Execute("SELECT COUNT(*) FROM Products").Fields(0).Value

' trying to use recordcount (fails, because non-static rs)
WScript.Echo "RecordCount (A):", oDb.Execute("SELECT * FROM Products").RecordCount

' use recordcount with static rs
Const adOpenStatic = 3
Dim oRs : Set oRS = CreateObject("ADODB.Recordset")
oRS.Open "SELECT * FROM Products", oDb, adOpenStatic
WScript.Echo "RecordCount (B):", oRs.RecordCount

' get rows from query and use UBound()
Dim aData : aData = oDb.Execute("SELECT * FROM Products").GetRows()
WScript.Echo "GetRows():", UBound(aData, 2) + 1

oDb.Close
输出:

cscript 04.vbs
Select Count(*): 77
RecordCount (A): -1
RecordCount (B): 77
GetRows(): 77

使用了解更多有关这些策略的信息。

这是我得到的最接近的答案:num_rows=Select COUNT*FROM receptor_sol请阅读,我的问题出了什么问题?如果你1简要解释你的目标2发布你尝试过的代码3解释它做错了什么,包括任何错误。否则,有些人会将其视为“给我代码”请求-无论你是不是这样想的。SELECT COUNT*返回长或更好的值,向下转换为Int是危险的。COUNT*是否总是返回int64?它不取决于文件数据类型吗?真的,我很好奇。感谢您的评论。很明显,返回类型取决于DBMS、驱动程序和语言,但是计数会很长或更好;所以不要将其向下转换为有符号的16位整数CInt。如果您只需要计数而不需要其他内容,那么除了从表中选择count0之外的所有内容都是浪费。不需要获取您不想使用的数据。