【问题标题】:pass multiple integer variable same time to the integer parameter将多个整数变量同时传递给整数参数
【发布时间】:2013-10-20 05:46:04
【问题描述】:

我有一个符合以下条件的存储过程:
WHERE (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate)
AND (Location_tbl.Locid IN (@locations))
我有一个填充 @locations 参数(整数)的 ListBox,以及用于 @fromDate 和 @toDate. 的两个 DateTimePicker 控件
我把我的列表框值是这样的:

 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 参数传递,以便我可以选择列表框中的多个项目?

【问题讨论】:

  • stackoverflow.com/questions/2377506/… 的可能重复项。那篇文章是针对 C# 的,但我猜它与 VB 基本相同。
  • 这里我必须传递整数变量。
  • 先生..我必须更改存储过程中的任何内容
  • 我认为你必须传递一个整数并不重要,因为值是一个对象。见:msdn.microsoft.com/en-us/library/…
  • 先生,我该如何重新编写我的代码..??

标签: vb.net listbox


【解决方案1】:

您可以尝试将您选择的整数连接成一个逗号分隔的字符串,然后将其传递给您的存储过程。

然后您可以使用此 great SO post 将 CSV 字符串转换为您的 TABLE。

然后您可以将该表变量与“Location_tbl.Locid”的 INNER JOIN 一起使用

    @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-01-29
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多