【问题标题】:vb.net: how to count row of searched value from one columnvb.net:如何从一列中计算搜索值的行数
【发布时间】:2013-03-22 01:24:40
【问题描述】:

假设有两列 收到日期和项目

如果我搜索了一个有 5 行相同日期的日期

它将出现在文本框中

请大家帮忙

*注意,我使用oledb作为数据库连接

这是我的搜索代码

Private Sub TxSearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxSearch.TextChanged


    If Con.State = ConnectionState.Closed Then
        Con.Open()
    End If

    Dad = New OleDb.OleDbDataAdapter("select RecordDate, Item from inv where RecordDate LIKE '%" & TxSearch.Text & "%'", Con)
    Dim dt As New DataTable
    Dad.Fill(dt)

    Me.DataGridView1.DataSource = dt
    Con.Close()
    DataGridView1.RowsDefaultCellStyle.BackColor = Color.DeepSkyBlue
    DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White
    Con.Close()


    SortDatagridviewColumn(0)
End Sub

【问题讨论】:

    标签: vb.net ms-access count


    【解决方案1】:

    Private Sub TxSearch_TextChanged(sender As System.Object, e As System.EventArgs) 处理 TxSearch.TextChanged

    If Con.State = ConnectionState.Closed Then
        Con.Open()
    End If
    
    Dad = New OleDb.OleDbDataAdapter("select RecordDate, Item from inv where RecordDate LIKE '%" & TxSearch.Text & "%'", Con)
    Dim dt As New DataTable
    Dad.Fill(dt)
    
    Me.DataGridView1.DataSource = dt
    
        Dim counter As String
        counter = dt.Rows.Count.ToString
        textBoxRowCount.Text = counter
    
    
    Con.Close()
    DataGridView1.RowsDefaultCellStyle.BackColor = Color.DeepSkyBlue
    DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White
    Con.Close()
    
    
    SortDatagridviewColumn(0)
    
    
    Private Sub textBoxRowCount_TextChanged(sender As System.Object, e As System.EventArgs) Handles textBoxRowCount.TextChanged
    
    End Sub
    

    结束子

    【讨论】:

      【解决方案2】:

      可能就是这样:

      ....
      Dad.Fill(dt)
      textBoxRowCount.Text = "Found " + dt.Rows.Count + " rows"
      Me.DataGridView1.DataSource = dt
      ....
      

      编辑:更改您的查询以使用参数,不要尝试连接字符串来构建 sql 命令

      Dim sqlText = "select RecordDate, Item from inv where RecordDate LIKE @search"
      Dad = New OleDb.OleDbDataAdapter(sqlText, Con)
      Dad.SelectCommand.Parameters.AddWithValue("@search", "%" + TextSearch.Text + "%")
      Dim dt As New DataTable
      Dad.Fill(dt)
      

      但是,在上述情况下,RecordDate 被认为是一个文本字段,如果相反,RecordDate 是一个 DateTime 类型的数据库字段,那么使用 LIKE 是没有意义的。您应该使用等于 (=) 或 sql 子句 BETWEEN

      查看您的评论,然后您可以编写一个查询来计算记录,(关于 RecordDate 的注意事项仍然有效)

      Dim sqlText = "select COUNT(*) from inv where RecordDate LIKE @search"
      Dim cmd  = New OleDb.OleDbCommand(sqlText, Con)
      cmd.Parameters.AddWithValue("@search", "%" + TextSearch.Text + "%")
      Dim resultCount = cmd.ExecuteScalar()
      

      【讨论】:

      • 你有什么问题?您不知道如何计算检索到的行数或根本没有检索到任何行?
      • 不,不是这样,我只是想根据搜索的日期计算项目,例如,如果我从 2013 年 3 月 18 日收到了 5 个项目,那么下一个文本框的计数将显示为 5,如果我搜索了 2013 年 3 月 20 日收到的商品 w/c 是 6,然后计数框将查看 6
      • 您是否可能希望您的 sql 返回满足特定条件的 COUNT 行?
      • 我搜索没有问题,如果我在第一个文本框中输入日期,我会根据日期计算项目数,然后 datagridview 将查看 5 行,第二个文本框也将显示数字 5如果我已经成功完成了逻辑,那么我将使用它来代替 if else staement 来禁用按钮,如果这个数量等于
      猜你喜欢
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多