0

我有一个 for 循环,它显示了最后一项。我想获取所有项目并用作存储过程的参数。是否有一个基本原因,为什么它只会显示最后一项?

Dim Flipper As String
For x As Integer = 0 To EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles().Count - 1
        Flipper = EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles(x).Name

    Next

Stored Proc:
Select * From myTable Inner Join myOtherTalbe ON myTable.Id = myOtherTable.Id Where RoleId = @RoleId

VB CodeBehind:
Dim strConnection As String = "myDataSource"
        Dim con As New SqlConnection(strConnection)
        Dim cmd As New SqlCommand("mainForumA", con)
        cmd.CommandType = CommandType.StoredProcedure
        'params here if needed
        cmd.Parameters.Add(New SqlParameter("@RoleId", Flippers))
        con.Open()
        Dim ds = New DataSet()
        Dim da = New SqlDataAdapter(cmd)
        da.Fill(ds)

        cdcatalog.DataSource = ds
        cdcatalog.DataBind()
        con.Close()

如果我只有一个角色,那很好,但如果有多个角色,我需要:

Select * From myTable Inner Join myOtherTalbe ON myTable.Id = myOtherTable.Id Where RoleId = @RoleId1 Or RoleId = @RoleId2, etc
4

2 回答 2

1

您需要一个逗号分隔的字符串并In在 sql 文本中使用运算符。

Dim Flippers As New List(Of String)
For x As Integer = 0 To EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles().Count - 1
    Flippers.Add(EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles(x).Name)
Next
Dim values As String = String.Join(Flippers.ToArray, ",") 

Dim sqlText = "Select * From table Where Role In (" & values & ")"
于 2013-09-01T03:50:30.497 回答
0

正如上面的回复所说,你没有对 Flipper 做任何事情。您可以尝试将名称添加到列表中,然后将其绑定。此外,我会将 CustomerRoles 存储在列表中,而不是每次都解析 IWorkContext。它将帮助您提高可读性和可能的​​性能。

 Dim Flipper As String
    Dim names As New List(Of String)
    For x As Integer = 0 To EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles().Count - 1
            Flipper = EngineContext.Current.Resolve(Of IWorkContext)().CurrentCustomer.CustomerRoles(x).Name
            names.Add(Flipper)
        Next

    ListView1.DataSource = names
    ListView1.DataBind()
于 2013-09-01T03:53:36.767 回答