2015-05-08 73 views
1

我正在爲在線商店創建「相關項目」功能。我有一個SQL查詢將從數據庫中提取ITEM列表並將它們顯示在頁面上,但我試圖根據幾個變量來修改列表將會更改的系統。對於新查詢使用DataAdapter結果作爲字符串

的代碼如下,我有與越來越可用串出查詢1麻煩的一部分,在查詢中使用的「結果」 variale 2.

Public Shared Function GetExtraProducts(ByVal strAddOnCat As String) As DataSet 
    Dim connect As New SqlConnection 

    Dim Data1 As New DataSet 
    Dim data2 As New DataSet 
    connect.ConnectionString = "SERVER = SERVER-SQL01; Trusted_Connection=yes; DATABASE=GlobalPCSQL" 
    connect.Open() 

    Dim query1 As String = "" 
    Dim query2 As String = "" 
    query1 = "SELECT StockID FROM dbo.ADDONLISTS WHERE SubCategory = 'Acer-Desktops'" 

    Dim command1 = New SqlDataAdapter(query1, connect) 
    command1.Fill(Data1) 

    If Data1.Tables(0).Rows.Count > 0 Then 
     query1 = "SELECT StockID FROM dbo.ADDONLISTS WHERE SubCategory = 'Generic'" 
     Dim command3 = New SqlDataAdapter(query1, connect) 
     command3.Fill(Data1, "StockID") 
    End If 

    Dim results As String = "" 
    For Each row In Data1.Tables(0).Rows 

     results += row.ToString() + "','" 
    Next 
    If results.Length > 2 Then 
     results = results.Substring(0, results.Length - 2) 
    End If 




    'results = "'HD12047' , 'TV12008'" 

    query2 = "SELECT stock_items.Stockcode, STOCK_GROUPS.XW_URL as stockgroup, STOCK_GROUP2S.XW_URL as stockgroup2, STOCK_MAINGROUP.XW_URL as stockmaingroup, stock_items.Stockcode as pID, stock_items.description as pName, stock_web.sales_html as pdescription, stock_web.picture_url as pImage, stock_web.picture_url as pLargeimage, stock_items.sellprice1 as pPrice, stock_items.SELLPRICE1, stock_items.SELLPRICE2, stock_items.SELLPRICE3, stock_items.SELLPRICE4, stock_items.SELLPRICE5, stock_items.SELLPRICE6, stock_items.SELLPRICE7, stock_items.SELLPRICE8, stock_items.SELLPRICE9, stock_items.status as itemtype, stock_items.SELLPRICE10 as pListPrice, stock_items.x_totalstock as pInStock, stock_items.x_webhits as pHits, stock_items.ISACTIVE, stock_items.WEB_SHOW, stock_items.X_WebBlub as X_WebBlurb, stock_items.x_webpromo as X_PROMOPAGE, stock_items.last_updated as lastupdated, stock_items.x_stockleveloverride, isnull(stock_items.Restricted_item,'N') as Restricted_item " 
    query2 += "FROM stock_items Left OUTER Join STOCK_WEB ON (stock_items.Stockcode = STOCK_WEB.Stockcode) LEFT OUTER JOIN STOCK_GROUPS ON (STOCK_GROUPS.GROUPNO = STOCK_ITEMS.STOCKGROUP) LEFT OUTER JOIN STOCK_GROUP2S ON (STOCK_GROUP2S.GROUPNO = STOCK_ITEMS.STOCKGROUP2) LEFT OUTER JOIN STOCK_MAINGROUP ON (STOCK_MAINGROUP.GROUPNO = STOCK_GROUPS.XW_MAINGROUP)" 
    query2 += "WHERE stock_items.ISACTIVE='Y' AND stock_web.picture_url IS NOT NULL " 
    query2 += "AND stock_items.Stockcode IN ('" + results + "')" 


    query2 += results 


    Dim command2 = New SqlDataAdapter(query2, connect) 
    command2.Fill(data2) 
    connect.Close() 


    Return data2 

End Function 

一切工作正常,如果我勺子喂股票的ID號到查詢2(替代「+ +結果」部分的,但是當我嘗試使用字符串從查詢一個我得到的是

Incorrect syntax near 'System.Data.DataRow' 

這讓我覺得,即使我得到的語法排序,它只是搜索System.Data.Datarow而不是實際值領域的。

任何幫助,將不勝感激。

回答

3

A DataRow可以包含許多DataColumn s。即使你的回報DataRow只包含一個DataColumn,仍然必須指定DataColumn

Dim results As String = "" 
For Each row In Data1.Tables(0).Rows 

    results += row.Item(0).ToString() + "','" '<--- Added Item(0)' 
Next 
If results.Length > 2 Then 
    results = results.Substring(0, results.Length - 2) 
End If 

此外,仔細檢查你有第一個結果撇號。您可能需要:

Dim results As String = "'" 

最後,在一個不相關的音符,而+運算符可以用於字符串連接,我會建議你使用&操盤手,並只使用+的數字加法:

results &= row.Item(0).ToString() & "','" '<--- Added Item(0)' 
+0

你是一個紳士和學者。我知道這是正確的,因爲現在所有的語法錯誤都會說:「[random stock code]附近的語法錯誤,而之前它只是說我愚蠢。」 – Wompguinea

+0

這些撇號會讓我發瘋。每次添加或刪除一個,我都會遇到語法錯誤或未封閉的引用錯誤仍然,字符串正在工作!感謝堆。 – Wompguinea

+0

沒關係,它的所有工作,撇號和所有。謝謝堆! – Wompguinea

相關問題