结果始终为零

Result is always zero

在我的网络服务器上执行这段代码时,结果总是 0 (ZERO)

任何人都可以预感这个问题吗(更好的解决方案:-D)

Dim MyTotalPages
Dim Recordset
Dim Connection
Dim aspDBcount
Dim ShowRowCount

ShowRowCount = 3 ' Fixed size

Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

ConnString = "DRIVER={MySQL ODBC 5.3 Unicode Driver}; SERVER=server_address; UID=a_username;PASSWORD=a_password; OPTION=3; Port=a_port"

Connection.Open ConnString

SQL = "SELECT COUNT(*) AS MyRowCounts FROM `a_database`.`a_table`;"

Set Recordset = Connection.Execute(SQL)

aspDBcount = (Recordset("MyRowCounts") * 1) 

Recordset.Close

MyTotalPages = (aspDBcount * 1) / (ShowRowCount * 1))

Response.Write aspDBcount & "HTML_NEW_LINE" & ShowRowCount & "HTML_NEW_LINE" & MyTotalPages 
  • Output is 10, 3, 0

使用:

aspDBcount = cInt(Recordset("MyRowCounts")) * 1

在 MySQL 中,SELECT COUNT() return 是一个 BIGINT 整数类型,需要转换才能在 VBScript 中正确操作和使用它。

CInt只能转换-32,76732,767

之间的数字

如果您的 SELECT COUNT() 超出了这个范围,您需要使用 CLng 来代替,它的范围是 -2,147,483,6482,147,483,647

你也可以使用CDbl,这有点奇怪。它可以转换 HUGE 数字并且似乎没有溢出。如果你试图传递一个大得离谱的整数,它最终会 return 一个 Invalid number 错误。