我有一个符合以下标准的存储过程:
WHERE (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate)
我有一个ListBox,用于填充
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 :(得分:0)
您可以尝试将选定的整数连接成以逗号分隔的字符串,然后将其传递到存储过程中。
然后,您可以使用此great SO post将该CSV字符串转换为适合您的表格。
然后,您可以将该表变量与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)