现在在这张表中我有像 1 和 2 这样的部门。
我想为每个部门打印单独的令牌。
这是从表中读取独特部门的代码(如果有)
Public Sub token_print_()
openconnection1()
Try
Dim searchQuery As String = "select distinct department as 'department' from tb_transactions where invoice_id='" & TextBox1.Text & "'"
Dim command As New SqlCommand(searchQuery, MYSQLCon)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable
adapter.Fill(table)
For Each row As DataRow In table.Rows
For Each colu In table.Columns
receipt_filldatagridview2(row(colu))
Next
Next
command.Dispose()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Receipt Fill Error 107")
End Try
End Sub
我想要的是阅读每个独特的部门(如1和2),然后用这个一一填写
datagridview
并打印令牌。 (将每个部门传递给receipt_filldatagridview2(row(colu))
)
但问题是 For 每个语句都没有按预期工作。 (
datagridview
只显示部门2的项目,并在每个语句中跳过部门1)
这是
datagridview
的代码
Public Sub receipt_filldatagridview2(ByVal dept As String)
openconnection1()
Try
DataGridView_thermal.AutoGenerateColumns = False
Dim searchQuery As String = "Select tb_transactions.product_name as 'product_name', cast(quantity as numeric(36,1)) as 'quantity', tb_transactions.rate as 'rate' from tb_transactions where tb_transactions.invoice_id = '" & TextBox1.Text & "' AND tb_transactions.department = '" & dept.ToString & "'"
Dim command As New SqlCommand(searchQuery, MYSQLCon)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable
adapter.Fill(table)
DataGridView_thermal.DataSource = table
BTPRINT.PerformClick()
table.Dispose()
adapter.Dispose()
command.Dispose()
Catch ex As Exception
MsgBox(ex.Message, "Receipt Fill Error 107")
End Try
End Sub
正如其他人所指出的,您需要参数化您的 SQL 查询。
你的问题是你为每个分支调用一次
receipt_filldatagridview2
。但是 receipt_filldatagridview2
替换了数据网格的内容。
如果您想使用
SqlDataAdapter
填充数据网格,则数据适配器需要返回网格的所有数据。否则,您必须自己编写代码来填充数据网格的数据表(即迭代每个查询的结果)。
显然前者更容易,并且似乎没有理由为每个查询单独完成第二个查询:您是否需要简单地
order by tb_transactions.department
以便结果在结果中按部门分组?