【发布时间】:2014-10-30 16:34:56
【问题描述】:
我需要一个解决方案。我想从一个表中减去/添加另一个字段值。我正在提供详细信息。
我有两个表(结构相同)。
Table1 - purchase 字段:id, purchase_from, product_name, quantity, rate
表 2 - sales 字段:id, sale_to, product_name, quantity, rate
现在我想将purchase 表中的数量减去sales 表并将数据显示到datagridview 中。
我尝试过,但失败了。我给你代码sn-p。
Dim PurchasePrdName, SalesPrdName As String
Dim PurchaseQty, SalesQty As Double
Private Sub LoadPurchase()
Try
OpenConnection()
Dim dcommand As New MySqlCommand
Dim qry As String = "SELECT ProductName,SUM(Quantity) FROM stockitems GROUP BY ProductId"
dcommand.Connection = conn
dcommand.CommandText = qry
Dim dbread As MySqlDataReader = dcommand.ExecuteReader
DataGridView1.Rows.Clear()
While dbread.Read
If dbread.HasRows Then
PurchasePrdName = dbread("ProductName").ToString
PurchaseQty = dbread("SUM(Quantity)").ToString
End If
End While
dbread.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
CloseConnection()
End Try
End Sub
Private Sub LoadSales()
Try
OpenConnection()
Dim dcommand As New MySqlCommand
Dim qry As String = "SELECT SUM(Quantity) FROM salesitems WHERE ProductName='" & PurchasePrdName & "' GROUP BY ProductId"
dcommand.Connection = conn
dcommand.CommandText = qry
Dim dbread As MySqlDataReader = dcommand.ExecuteReader
DataGridView1.Rows.Clear()
While dbread.Read
If dbread.HasRows Then
SalesQty = dbread("SUM(Quantity)").ToString
End If
End While
dbread.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
CloseConnection()
End Try
End Sub
然后将此数据加载到 DataGridView...
Private Sub LoadData()
Try
LoadPurchase()
LoadSales()
Dim PrdName As String = PurchasePrdName
Dim Qty As Double = PurchaseQty - SalesQty
DataGridView1.Rows.Add(PrdName, Qty)
DbGridCustomize()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
但它只显示表中的一种产品。我很困惑为什么!!!
你能帮帮我吗???
我会非常感谢你
【问题讨论】:
标签: mysql vb.net datagridview