在vb.net中为sql-query输出添加计数器

时间:2018-01-26 15:40:29

标签: sql vb.net counter

现在我正在研究vb.net中的一个简单程序。 在daytimepicker中选择2个日期,按一个按钮进行sql查询并将结果保存在某处。 这工作正常,但现在我想为查询添加一个计数器。 一列只有1,2,3的数字,我想计算每一个。 “输出”的小例子 `

x x x 1 -> count 1
x x x 1 -> count 2
x x x 2 -> count 1
x x x 1 -> count 3
x x x 2 -> count 2
x x x 3 -> count 1

` 然后,此计数器应作为新列添加到作为查询结果的数据表中。基本上是账单编号。

我已经尝试在sql的read函数中执行此操作,但我只是遇到错误,我不知道为什么......我猜测仍然是测试版。

    Do While myReader.Read()
        results = results & myReader.GetString("0") & ";" &
            myReader.GetString("1") & ";" &
            myReader.GetString("2") & ";" &
            myReader.GetDecimal("3") & ";" &
            myReader.GetInt32("4") & ";" &
            myReader.GetInt32("5") & ";" &
 ** thats basicly my reader, which puts an ; between everything because im saving it as .csv

        If myReader.GetInt32("5") = 1 Then
            nmb1 = nmb1 + 1
        **put nmb1 in the output"
        ElseIf myReader.GetInt32("5") = 2 Then
            nmb2 = nmb2 + 1
        **put nmb1 in the output"
        ElseIf myReader.GetInt32("5") = 3 Then
            nmb3 = nmb3 + 1
        **put nmb1 in the output"
        Else
            MsgBox("u failed")
        End If '& vbLf
    Loo

P

我希望你理解我的问题,因为我对编程很陌生。如果您需要进一步的说明,请询问。

1 个答案:

答案 0 :(得分:0)

启用OptionStrict。这将有助于避免类型不匹配和奇怪的错误。

Dim results String = ""
Dim nmb1 As Integer
Dim nmb2 As Integer
Dim nmb3 As Integer

Do While ...
...

        Select Case myreader.GetInt32("5")
            Case 1
                nmb1 += 1
                results &= $"; {nmb1}"
            Case 2
                nmb2 += 1
                results &= $"; {nmb2}"
            Case 3
                nmb3 += 1
                results &= $"; {nmb3}"
            Case Else
                MessageBox.Show("u failed")
        End Select
        'do you wand to put a hard return between records? 
        results &= vbCrLf
Loop