Visual Basic:无法从记录集中读取记录/数据。

时间:2012-04-06 18:11:07

标签: sql vba asp-classic recordset

这是我的代码:

Dim CompanyName, _
    CompanyDomain, _
    CompanyEmail, _
    CompanySupportPhone

Call GetEmailList

Sub GetEmailList
    dim sql
    dim companydata

    sql = ""
    sql = sql & " DECLARE @CompanyName VARCHAR(100);"
    sql = sql & " DECLARE @CompanyDomain VARCHAR(100);"
    sql = sql & " DECLARE @CompanyActivityEmail VARCHAR(100);"
    sql = sql & " DECLARE @CompanySupportPhone VARCHAR(100);"

    sql = sql & " SELECT"
        sql = sql & " @CompanyName = CASE WHEN Setting = 'CompanyName'"
        sql = sql & " THEN StringValue ELSE @CompanyName END,"
        sql = sql & " @CompanyDomain = CASE WHEN Setting = 'CompanyDomain'"
        sql = sql & " THEN StringValue ELSE @CompanyDomain END,"
        sql = sql & " @CompanyActivityEmail = CASE WHEN Setting = 'CompanyActivityEmail'"
        sql = sql & " THEN StringValue ELSE @CompanyActivityEmail END,"
        sql = sql & " @CompanySupportPhone = CASE WHEN Setting = 'CompanySupportPhone'"
        sql = sql & " THEN StringValue ELSE @CompanySupportPhone END"
    sql = sql & " FROM ClientSettings"
    sql = sql & " WHERE Setting in ('CompanyDomain','CompanyActivityEmail','CompanySupportPhone','CompanyName')"

    sql = sql & " SELECT ISNULL(@CompanyName, '') AS CompanyName, ISNULL(@CompanyDomain, '') AS CompanyDomain, ISNULL(@CompanyActivityEmail, '') AS CompanyEmail, ISNULL(@CompanySupportPhone, '') AS CompanySupportPhone"
    set companydata =  getRecordset(sql)

    CompanyName = companydata("CompanyName") ' LINE 80
    CompanyDomain = companydata("CompanyDomain")
    CompanyEmail = companydata("CompanyEmail")
        CompanySupportPhone = companydata("CompanySupportPhone")

        companydata.Close
        Set companydata = Nothing
End Sub

这会引发错误:

  

第80行

     

在与请求对应的集合中找不到项目   名称或序数。

我在上面标记了第80行。我在SQL Server管理器中运行这个完全相同的SQL,它返回结果:

CompanyName CompanyDomain   CompanyEmail    CompanySupportPhone
MyCompanyName   http://localhost    MyCompanyName@email.com 801-555-1212

知道我在这里做错了吗?

GetRecordSet正确加载并处理数据库调用,此函数可在其他1,000个地方使用。我确定问题不存在。

1 个答案:

答案 0 :(得分:5)

添加

sql = sql & " SET NOCOUNT ON;"

作为第一个SQL语句。

SET NOCOUNT ON usage