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

时间:2022-09-15 17:46:01

When I execute this query I only get a return when there are two or more entries that equal the query.

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

When there is only one I am getting a null and have spent hours trying to figure it. Please help.

当只有一个我得到一个空,并花了几个小时试图找到它。请帮忙。

    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 个解决方案

#1


0  

Here's a variation on your code using NorthWind data. It seems to work OK. Perhaps your data has some nulls in Model?

以下是使用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)

I used a my library to get the datatable - you can use your datatable. I used a WinForm DataGridView (dgv1) for the UI.

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

#1


0  

Here's a variation on your code using NorthWind data. It seems to work OK. Perhaps your data has some nulls in Model?

以下是使用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)

I used a my library to get the datatable - you can use your datatable. I used a WinForm DataGridView (dgv1) for the UI.

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