2

我有一个具有以下条件的存储过程: 我有一个填充参数(整数)的 ListBox,以及两个 DateTimePicker 控件,我 将我的列表框值设为:
WHERE (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate)
AND (Location_tbl.Locid IN (@locations))
@locations@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 参数传递,以便我可以在列表框中选择多个项目?

4

1 回答 1

0

You could try concatenating your selected integers into a comma-separated string, then pass that into your Stored Procedure.

You can then use this great SO post that will convert that CSV string into a TABLE for you.

You can then use that table variable with an INNER JOIN to your "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)
于 2013-10-25T14:24:44.087 回答