从vb.net的另一个表中的SQL中更新集合总和
我想更新设定总和,但是我的代码并不完美,也许我需要添加或修改SQL。 我尝试了“ UpdateTable2()”,但结果尚未符合我想要的输出结果 谢谢
TABLE1
INVNO QTY PRICE DIS
1000 10 10000 10
1000 20 20000 20
1001 15 10000 10
1001 30 20000 20
TABLE2
INVNO TOTAL
1000
1001
OUTPUT
TABLE2
INVNO TOTAL
1000 410000 QTY*PRICE*(1-DIS/100) for total from INVNO 1000
1001 615000 QTY*PRICE*(1-DIS/100) for total from INVNO 1001
Private Sub fillDataGrid()
Try
Dim query As String = "SELECT INVNO,SUM((QTY*PRICE)*(1-DIS/100)) AS TOTAL FROM TABLE1 GROUP BY INVNO"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
'cmd.Parameters.AddWithValue("@CODE", ComboBox1.SelectedValue)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source3.DataSource = dt
Me.DataGridView3.DataSource = source3
Me.DataGridView3.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Sub UpdateTABLE2()
Try
Dim sql As String = "UPDATE TABLE2 INNER JOIN TABLE1 ON TABLE1.[INVNO] = TABLE2.[INVNO] SET TABLE2.[TOTAL] = [QTY]*[PRICE]*(1-[DIS]/100)"
Using conn As New OleDbConnection(cn),
cmd As New OleDbCommand(sql, conn)
'cmd.Parameters.AddWithValue("@INVNO", ComboBox1.SelectedValue)
conn.Open()
cmd.ExecuteNonQuery()
End Using
I want to update the set sum but my code is not perfect, maybe I need to add or modify the sql.
I tried "UpdateTABLE2()" but the result is not yet in accordance with the output result I want
thanks
TABLE1
INVNO QTY PRICE DIS
1000 10 10000 10
1000 20 20000 20
1001 15 10000 10
1001 30 20000 20
TABLE2
INVNO TOTAL
1000
1001
OUTPUT
TABLE2
INVNO TOTAL
1000 410000 QTY*PRICE*(1-DIS/100) for total from INVNO 1000
1001 615000 QTY*PRICE*(1-DIS/100) for total from INVNO 1001
Private Sub fillDataGrid()
Try
Dim query As String = "SELECT INVNO,SUM((QTY*PRICE)*(1-DIS/100)) AS TOTAL FROM TABLE1 GROUP BY INVNO"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
'cmd.Parameters.AddWithValue("@CODE", ComboBox1.SelectedValue)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source3.DataSource = dt
Me.DataGridView3.DataSource = source3
Me.DataGridView3.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Sub UpdateTABLE2()
Try
Dim sql As String = "UPDATE TABLE2 INNER JOIN TABLE1 ON TABLE1.[INVNO] = TABLE2.[INVNO] SET TABLE2.[TOTAL] = [QTY]*[PRICE]*(1-[DIS]/100)"
Using conn As New OleDbConnection(cn),
cmd As New OleDbCommand(sql, conn)
'cmd.Parameters.AddWithValue("@INVNO", ComboBox1.SelectedValue)
conn.Open()
cmd.ExecuteNonQuery()
End Using
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有3个问题要解决:
计算表1中每一行的总数。您可以将计算出的字段直接添加到表格,创建一个为您完成的视图,也可以使用表达式使用Inline
进行。我提出了一个视图:
将数据分组。同样,可能只是查询或视图,在这里再次作为视图:
将其填充到表中。首先要挑战自己是否需要序列化,如果这是必需的,则可以使用以下代码之类的内容:(当心:它会暂时删除
table2
>))
如果您有其他数据您已经省略了订单号,客户或时间范围,并且仅应为该订单/客户/时间范围刷新更新从table2中删除... 。
There are 3 problems to solve:
Calculate the total for each row in TABLE1. You could add a calculated field directly to the table, create a view that does it for you or do it with an inline
WITH
expression. I propose a view:Group the data. Again, could be just a query or a view, here again as a view:
Fill that into a table. Challenge yourself first whether it really needs to be serialized, and if that is a requirement, you could use something like the following code: (Watch out: It temporarily drops everything of
TABLE2
)If you have additional data that you have omitted like an order number or a customer or a time range and the updates should only be refreshed for that order/customer/time range then you have to replace the
TRUNCATE TABLE
by aDELETE FROM TABLE2 WHERE ...
.