2012-04-06 112 views
1

這裏是我的代碼:Visual Basic:無法從記錄集中讀取記錄/數據。

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 

這將引發一個錯誤:

Line 80

Item cannot be found in the collection corresponding to the requested name or ordinal.

我上面標線80。我在SQL Server管理器中運行完全相同的SQL並返回結果:

CompanyName CompanyDomain CompanyEmail CompanySupportPhone 
MyCompanyName http://localhost [email protected] 801-555-1212 

任何想法我在做什麼錯在這裏?

GetRecordSet正確加載和處理數據庫調用,該函數可以在其他1,000個地方使用。我相信這個問題不存在。

+2

請爲您的問題選擇一個更具描述性的標題。 「我在使用Visual Basic遇到問題」......誰不是? – 2012-04-06 18:46:22

回答

5

添加

sql = sql & " SET NOCOUNT ON;" 

作爲第一個SQL語句。

SET NOCOUNT ON usage

+0

就是這樣,謝謝。任何想法爲什麼不這樣做打破它? – 2012-04-06 18:18:19

+0

更新了評論,並附有關於此的詳細描述的鏈接。 – 2012-04-06 18:20:23

+0

非常感謝。 @amit_g – 2012-04-06 18:20:43