VB.NET只在DB中选择有2个或更多条目

时间:2015-12-02 14:17:55

标签: sql database vb.net

当我执行此查询时,只有当两个或多个条目等于查询时才会返回。

当只有一个我得到一个null并且花了好几个小时试图计算它。请帮忙。

    Dim i = (From dr As DataRow In Test1DataSet.Tables("Data").Rows Select dr Where dr("S3N:").ToString.Contains(scanPNtb.Text.ToString)).Count
    For Each m3 In (From dr As DataRow In Test1DataSet.Tables("Data").Rows Select dr Where dr("S3N:").ToString.Contains(scanPNtb.Text.ToString))
        model = m3("model").ToString
    Next
    MsgBox(model & "  " & i)

1 个答案:

答案 0 :(得分:0)

以下是使用NorthWind数据的代码变体。它似乎工作正常。也许您的数据在模型中有一些空值

    Dim SQL As String = "SELECT TOP 10 CompanyName, City, Region, HomePage FROM Suppliers"
    Dim dt As DataTable = Gen.GetDataTable(SQL, sConnectNorthwind)
    dgv1.DataSource = dt ' see data in grid
    dgv1.AutoResizeColumns()
    Dim sFind As String = InputBox("Find keywword:")
    Dim sOut As String = ""
    For Each m3 In (From dr As DataRow In dt.Rows Select dr Where dr("City").ToString.Contains(sFind))
        sOut &= "City: " & m3("City").ToString & vbNewLine
    Next
    Dim i = (From dr As DataRow In dt.Rows Select dr Where dr("City").ToString.Contains(sFind)).Count
    MsgBox(i & vbNewLine & sOut)

我用我的库来获取数据表 - 你可以使用你的数据表。我在UI中使用了WinForm DataGridView(dgv1)。