0

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

如果我搜索有 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
4

2 回答 2

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

结束子

于 2013-03-21T17:24:40.050 回答
0

可能只是这样:

....
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-03-21T14:06:06.937 回答