vb.net:如何从一列中计算搜索值的行数
假设有两列 收到日期和项目 如果我搜索了一个日期,其中有5行是相同的日期 它将出现在文本框中 我不能帮助plss *注意,我使用oledb作为数据库连接 这是我的搜索码vb.net:如何从一列中计算搜索值的行数,vb.net,ms-access,count,Vb.net,Ms Access,Count,假设有两列 收到日期和项目 如果我搜索了一个日期,其中有5行是相同的日期 它将出现在文本框中 我不能帮助plss *注意,我使用oledb作为数据库连接 这是我的搜索码 Private Sub TxSearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxSearch.TextChanged If Con.State = ConnectionState.Closed Then
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
可能就是这样:
....
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。您应该使用equal(=)或sql子句BETWEEN
查看您的评论,然后您可以编写一个查询来统计记录(RecordDate上的注意事项仍然有效)
私有子TxSearch_TextChanged(发送方作为System.Object,e作为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
结束Sub您有什么问题?您不知道如何计算检索到的行数,或者根本不检索任何行?不,与此不同,我只想根据搜索的日期对项目进行计数。例如,如果我从2013年3月18日收到5个项目,则下一个文本框中的计数将显示为5如果我搜索2013年3月20日收到的项目,w/c为6,则计数框将查看6您是否希望sql返回满足特定条件的行数条件?我在搜索方面没有问题,如果我在第一个文本框中键入日期,则我会记录基于日期的项目计数,然后datagridview将查看5行,第二个文本框还将显示数字5。如果我已成功完成逻辑,则这是出于此目的,然后,我将使用它来代替if-else状态,以禁用按钮,前提是该值等于
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