2013-10-20 33 views
2

我有一個存儲過程用以下標準:
WHERE (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate)
AND (Location_tbl.Locid IN (@locations))
我有其中填充@locations參數(一個整數)的列表框,以及用於兩個的DateTimePicker控制在@fromDate@toDate.
我把我的列表框的值是這樣的:通多個整數變量同時整數參數

cnt = LSTlocations.SelectedItems.Count 
     If cnt > 0 Then 
      For i = 0 To cnt - 1 
       Dim locationanme As String = LSTlocations.SelectedItems(i).ToString 
       locid = RecordID("Locid", "Location_tbl", "LocName", locationanme) 
       list.Add(locid) 
      Next 
      End If 

我想這個列表項的值傳遞給我的存儲過程...我怎麼能做到這一點?

cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value= startdate 
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate 
cmd23.Parameters.Add("@locations", SqlDbType.Int) ' <= ??? 

如何這段代碼進行修改,以通過幾個整數標識符爲@locations參數,以便我可以選擇我的列表框中幾個項目?

+0

http://stackoverflow.com/questions/2377506/pass-array-parameter-in的可能的複製-sqlcommand。這篇文章是針對C#的,但我猜測它對VB基本上是一樣的。 –

+0

在這裏我必須通過整數varibale。 – user2878851

+0

先生..我必須改變我的存儲過程中的任何東西 – user2878851

回答

0

您可以嘗試將選定的整數連接成逗號分隔的字符串,然後將其傳遞到您的存儲過程。

然後,您可以使用此great SO post將該CSV字符串轉換爲您的TABLE。

然後,您可以使用該表變量與INNER JOIN你的「Location_tbl.Locid」

@Locations VARCHAR(MAX) --Your inbound CSV list of LocID's 
    . 
    . 
    . 
    DECLARE @MyTable TABLE 
     (LocID VARCHAR(50)) 

    INSERT INTO @MyTable 
     (LocID) 
    SELECT dbo.Split(@Locations, ',') --This is the function from the aforementioned POST 
    . 
    . 
    SELECT * FROM [your list of tables] 
    INNER JOIN @MyTable L ON Location_tbl.Locid = L.LocID 
    WHERE (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate)