VBS将记录计数传递给变量

时间:2013-11-01 15:29:41

标签: sql-server vbscript

我想运行以下VB脚本并连接到某个SQL服务器,并运行以下查询来计算行数。然后将该值保存到变量中,稍后我可以使用它。

我做错了什么?

Dim tempSQLCount 
tempSQLCount = 0

Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=someServer;Initial Catalog=DB;Integrated Security=SSPI"

Set sqlConnection = CreateObject("ADODB.Connection") 
Set sqlRecordSet = CreateObject("ADODB.Recordset")

sqlConnection.Open DB_CONNECT_STRING 

Set sqlQuery = CreateObject("ADODB.Command" ) 
sqlQuery.ActiveConnection = sqlConnection 
sqlQuery.CommandText = "SELECT COUNT(*) FROM DB.dbo.Task" 
sqlQuery.Execute 

tempSQLCount = sqlRecordSet

Wscript.Echo sqlRecordSet

sqlRecordSet.Close
Set sqlRecordSet = nothing
sqlConnection.Close
Set sqlConnection = nothing

2 个答案:

答案 0 :(得分:0)

自从我使用VBScript以来已经有一段时间了,但也许是这样的......

Set sqlQuery = CreateObject("ADODB.Command" ) 
sqlQuery.ActiveConnection = sqlConnection 
sqlQuery.CommandText = "SELECT COUNT(*) as Counter FROM DB.dbo.Task" 

Set sqlRecordSet = sqlQuery.Execute
sqlRecordSet.MoveFirst
tempSQLCount = sqlRecordSet("Counter")

抱歉,我可能很生气!

答案 1 :(得分:0)

看起来你没有用值初始化记录集 - 你只是调用execute方法而不保存结果。

尝试:

sqlRecordSet.open sqlQuery
tempSQLCount = sqlRecordset.getRows()

Wscript.echo tempSQLCount(0,0)