【问题标题】:Pass multiple selected item id from list box to a stored procedure将多个选定项 id 从列表框中传递到存储过程
【发布时间】:2013-11-04 05:42:32
【问题描述】:

我有一个像这样的表值参数

CREATE TYPE dbo.ss AS TABLE(ss1 integer);

我的存储过程如下所示:

ALTER PROCEDURE [dbo].[T_TransactionSummary] 
    @locations dbo.ss readonly
as
begin
    ...............
    .............
    AND (Location_tbl.Locid IN (@locations))

我有一个包含多个项目的列表框。我可以从我的列表框中选择多个项目。如果我总是从列表框中选择一个位置,如何将多个 Locationid 传递给我的存储过程?

我这样传递它们(仅选择一项时):

dim locid as integer = Lstbox.selectedItem
cmd.parameters.add("@locations", locid)

但我不知道如何将多个选定项目一起从列表框传递到存储过程。如果我从列表框中选择了多个项目,我想将所有项目 id 一起传递给我的存储过程

我正在开发 vb.net windows 窗体.. 先生,在我的情况下,我正在使用列表框选择的项目值,如下所示:

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

我正在传递给存储过程的这个 locid..

【问题讨论】:

    标签: sql-server vb.net


    【解决方案1】:

    尝试使用For each获取所有选定的项目,然后Using这样每次循环使用后都会丢弃对象,例如:

    For Each selectedItem As Object In ListBox1.SelectedItems
        Dim cmdText As String = "T_TransactionSummary"
        Dim locid As Integer = Int32.Parse(selectedItem.ToString())
        Using con As New SqlConnection("connection string here"), comm As New SqlCommand(cmdText), da As New SqlDataAdapter(comm)
                comm.CommandType = CommandType.StoredProcedure
                comm.Parameters.Add("@location", SqlDbType.Int).Value = locid
                comm.ExecuteNonQuery()
                Dim dst As New DataSet
                da.Fill(dst)
        End Using
    Next
    

    【讨论】:

    • 先生……为什么要更新“SET Field1='test'”
    • 这只是一个例子。你可以随心所欲地改变它。但是你真的想要吗?您想返回记录或返回单个值,或者什么都不做,只是简单地更新您的数据库(这里就是这种情况)?
    • 先生..我不明白你为什么要更新数据库,我编辑了我的问题
    • @user2951524:更新了我的答案。老实说,我忘记了您之前指的是存储过程;-)
    • 先生,,,在此之后我如何将这个值填充到一个数据集?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2014-08-10
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多