查找给定范围内的缺失数字

时间:2012-12-20 04:07:01

标签: vb.net sql-server-2008

所以我的编码有问题,并且想知道是否有人可以帮助我。

基本上我正在使用VB.NET和MSSQL来创建一个程序,用于在用户设置的给定范围之间找到缺失的数字。程序将从表中读取并在文本框中输出。到目前为止,上述代码是我能想到的。但问题是,我输错了而不是我想要的。这是输出的image

Function FindingMissingNumber() As String

    Dim intX As Integer = Nothing
    Dim intY As Integer = Nothing
    Dim strSting As String = Nothing
    Dim strSqlQUery As String = Nothing
    Dim cmdSqlCommand As Data.SqlClient.SqlCommand = Nothing
    Dim rdrDataReader As Data.SqlClient.SqlDataReader = Nothing

    '------------------------------------------------------------------------------------------------------------------------
    '-> Process
    '------------------------------------------------------------------------------------------------------------------------
    strSqlQUery = "Select ExReportPolicyNo From DBReport Order by ExReportPolicyNo"
    Dim msSqlConnection As New Data.SqlClient.SqlConnection()
    'NOTE - You may need to CHECK your connection string!!! in the line below
    msSqlConnection.ConnectionString = "Data Source=SISBSQL\SISBSQL;Initial Catalog=ExceptionReport;User ID=sa;Password=123;"
    cmdSqlCommand = New Data.SqlClient.SqlCommand(strSqlQUery, msSqlConnection)
    If cmdSqlCommand.Connection.State = Data.ConnectionState.Closed Then cmdSqlCommand.Connection.Open()
    rdrDataReader = cmdSqlCommand.ExecuteReader()
    If rdrDataReader.HasRows Then
        Do While rdrDataReader.Read()
            intX = txtRangeLeft.Text
            intY = txtRangeRight.Text
            'intY = rdrDataReader.GetValue(rdrDataReader.GetOrdinal("ExReportPolicyNo"))

            Do While intX <> intY
                intX = intX + 1
                If intX <> intY Then
                    strSting = strSting & intX & ", "    'if it is not, then record the non sequential number into the string
                Else
                    Exit Do
                End If
            Loop
        Loop
    End If
    If cmdSqlCommand.Connection.State = Data.ConnectionState.Open Then cmdSqlCommand.Connection.Close()
    'return string
    Return strSting
    'tidy up
    intX = Nothing
    intY = Nothing
    strSting = Nothing
    strSqlQUery = Nothing
    cmdSqlCommand = Nothing
    rdrDataReader = Nothing

End Function

正如您所看到的,程序会多次循环,并输出错误的输出。输出应该只读“286118,286120,286121”。问题是我哪里出错了?

1 个答案:

答案 0 :(得分:1)

试试这个(使用linq)

更改查询以返回开始值和结束值之间的行

Select distinct ExReportPolicyNo From DBReport 
Where ExReportPolicyNo between @start and @end  
Order by ExReportPolicyNo

从您的查询中创建列表:

Dim originalList as List(Of Integer)
If rdrDataReader.HasRows Then
Do While rdrDataReader.Read()
originalList.Add(rdrDataReader.GetInt(0))
Loop
End If

创建开始和结束编号的范围列表

//Dim rangeList = Enumerable.Range(286117, 286121 - 286117 + 1).ToList()
Dim starti = Int32.Parse(txtRangeLeft.Text)
Dim endi = Int32.Parse(txtRangeRight.Text)
Dim rangeList = Enumerable.Range(starti, endi - starti + 1).ToList()

查找所有缺失的数字

Dim missingList = originalList.Except(rangelist)

从上面的列表中创建CSV字符串

strString = String.Join(",", missingList.Select(x => x.ToString()).ToArray())