在数据表中创建值的字符串列表

时间:2016-10-21 09:39:49

标签: vb.net string for-loop iteration concatenation

我试图在一个程序中创建一个费率列表,其中费率从今天开始一个月就用完了,以便可以向用户发送提醒以扩展它们。我这样做的代码在

之下
Try
    Dim rDt As New DataTable
    Dim r1Dt As New DataTable

    Using rDa = New OleDbDataAdapter("SELECT DISTINCT [Comm_Code] FROM [Acquisition Commission] WHERE DateTo=?", con)
        rDa.SelectCommand.Parameters.Add("@date", OleDbType.Date).Value = Date.Today.AddMonths(1)
        rDa.Fill(r1Dt)
    End Using

    Dim r2Dt As New DataTable
    Using r2Da = New OleDbDataAdapter("SELECT DISTINCT [Comm_Code] FROM [Commission Rates] WHERE DateTo=?", con)
        r2Da.SelectCommand.Parameters.Add("@date", OleDbType.Date).Value = Date.Today.AddMonths(1)
        r2Da.Fill(r2Dt)
    End Using

    Dim r3Dt As New DataTable
    Using r3Da As New OleDbDataAdapter("SELECT DISTINCT [Comm_Code] FROM [Customer Special Rates] WHERE DateTo=?", con)
         r3Da.SelectCommand.Parameters.Add("@date", OleDbType.Date).Value = Date.Today.AddMonths(1)
         r3Da.Fill(r3Dt)
    End Using

    rDt = r1Dt.Copy
    rDt.Merge(r2Dt)
    rDt.AcceptChanges()
    rDt.Merge(r3Dt)
    rDt.AcceptChanges()

    If rDt.Rows.Count > 0 Then
        Dim rates As String = ""
        For Each dr As DataRow In rDt.Rows
            rates = dr.Item("Comm_Code") & ", "
        Next
        If MsgBox("The following rates; " & rates & "are set to expire in 1 month. Would you like to automatically extend these rates by 6 months?", MsgBoxStyle.YesNo, "Extend Rates") = MsgBoxResult.No Then

        Else

但是,当单步执行此代码时,我可以看到rDt有3行(正如我所料,每个表中的1个速率作为测试),但For Each循环只迭代一次,所以3个项目中只有1个连接到字符串上。

为什么会这样,我该如何解决?

1 个答案:

答案 0 :(得分:1)

每次都覆盖rates

rates = dr.Item("Comm_Code") & ", "

您需要将其更改为

rates &= dr.Item("Comm_Code") & ", "

警惕额外的逗号。