×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

熟悉SQL Server和ADO的大侠进来看看

我用一段VBS去连接到SQL上,运行一个system stored procedure,然后把结果打印出来。
代码如下
strDBServerName = "DBSRV"
ConnectionString = "Provider=sqloledb;" & _
"Data Source=" & strDBServerName & ";" & _
"Initial Catalog=master;" & _
"Integrated Security=SSPI"
err.clear
Set dbCon = CreateObject("ADODB.Connection")
dbCon.Open ConnectionString

sqlstring = "sp_helprotect"
Set vDBSet = CreateObject("ADODB.Recordset")
vDBSet.open sqlstring, dbCon
Do until vDBSet.eof
For j = 0 to vDBSet.Fields.Count - 1
wscript.echo CStr(vDBSet.Fields.item(j))
Next
vDBSet.MoveNext
Loop
总是得到错误
ADODB.Recordset: Operation is
not allowed when the object is closed.

发现只有运行sp_helprotect有问题,其他,比如sp_configure, sp_helpdb, sp_helprole等等都没问题。

另外,还有一个问题,怎么得到recordset每一个field的名字?

什么建议?多谢!
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 熟悉SQL Server和ADO的大侠进来看看
    我用一段VBS去连接到SQL上,运行一个system stored procedure,然后把结果打印出来。
    代码如下
    strDBServerName = "DBSRV"
    ConnectionString = "Provider=sqloledb;" & _
    "Data Source=" & strDBServerName & ";" & _
    "Initial Catalog=master;" & _
    "Integrated Security=SSPI"
    err.clear
    Set dbCon = CreateObject("ADODB.Connection")
    dbCon.Open ConnectionString

    sqlstring = "sp_helprotect"
    Set vDBSet = CreateObject("ADODB.Recordset")
    vDBSet.open sqlstring, dbCon
    Do until vDBSet.eof
    For j = 0 to vDBSet.Fields.Count - 1
    wscript.echo CStr(vDBSet.Fields.item(j))
    Next
    vDBSet.MoveNext
    Loop
    总是得到错误
    ADODB.Recordset: Operation is
    not allowed when the object is closed.

    发现只有运行sp_helprotect有问题,其他,比如sp_configure, sp_helpdb, sp_helprole等等都没问题。

    另外,还有一个问题,怎么得到recordset每一个field的名字?

    什么建议?多谢!
    • sp_helprotect might have empty results. before you do any loop, check if the recordset is null or nothing first.
      • 这个问题想到过了。但好像不是。每次出错是在Do until vDBSet.eof时候出的错。做了个判断,vDBset这个recordset居然在vDBSet.open sqlstring, dbCon 之后是null。就是说没有任何东西回来。真是奇怪。
    • wscript.echo CStr(vDBSet.Fields.(j).Name)?
      • 这个问题解决了。for each vName in dbrecordset.fields wscript.echo vName.name next