[英]How to do “INSERT INTO table1 (…) SELECT (…) FROM table2” in LINQ?
[英]sum the values from Datagridview1(Table1) and save it to Datagridview2(Table2) Using Linq and MySql
我正在尝试对Datagridview1(Table1)
的值求和并将其保存到Datagridview2(Table2)
两个DGVs
是Databound
两者具有相同的2列Name, Quantity
。
在Childform1(DGV1)
我有一个Textbox1
和一个Post
按钮。
用户必须在Textbox1
上输入一个Name
,然后单击Post
,然后必须对DGV1
上的所有“ Quantity
”进行汇总,其Name
与在Textbox1
输入的Name
相同。
例如:
表格1
Name | Quantity
a 10
b 5
a 10
b 5
按POST
按钮后
表2
Name | Quantity
a 20
b 10
代号
Dim occurences As New Dictionary(Of String, Double)
Dim oTempObjects As New List(Of DataGridViewRow)
MasterForm.oTransferRows = oTempObjects
For Each xRow As DataGridViewRow In Datagridview1.Rows
If (Not xRow.Cells("GVName").Value Is Nothing AndAlso xRow.Cells("GVName").Value.ToString() = TextBox1.Text) Then
oTempObjects.Add(xRow)
End If
Next
Dim _Name As New List(Of String)
For Each xRows In MasterForm.oTransferRows
_Name.Add("'" & xRows.Cells("GVName").Value.ToString() & "'")
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE tested SET Posted = @Posted WHERE Name IN ({0})", inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = _sqlUpdate
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("@Posted", "Yes")
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
_conn.Close()
End Using
End Using
If (occurences.ContainsKey(xRows.Cells(1).Value.ToString())) Then
occurences(xRows.Cells(1).Value.ToString()) = Double.Parse(occurences(xRows.Cells(1).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(4).Value.ToString())
Else
occurences.Add(xRows.Cells(1).Value.ToString(), Double.Parse(xRows.Cells(4).Value.ToString()))
End If
Next
For Each KVP As KeyValuePair(Of String, Double) In occurences
oChildForm2.DataGridView2.Rows.Add(New Object() {KVP.Key, KVP.Value})
Next
Dim xlist As List(Of KeyValuePair(Of String, Double)) = occurences.ToList
For Each pair As KeyValuePair(Of String, Double) In occurences
Dim oUpdateString = String.Format("Insert Into testing (Name, Quantity) VALUES (@iName, @iQty)")
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = oUpdateString
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("@iName", pair.Key)
.Parameters.AddWithValue("@iQty", pair.Value)
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
_conn.Close()
End Using
End Using
Next
第一次单击时,此代码仅将DGV1
上的相同名称DGV1
,然后将其保存在DGV2
。
但是例如,我在DGV1
上添加了另一个同名数据,
我希望将新添加的数据的数量添加到DGV2上现有数据的当前总和中。
一点帮助也真的很棒。**
例如:
表2
Name | Quantity
a 20
b 10
然后我在表1上添加数据
表格1
Name | Quantity
a 20
b 10
按下后
表2
Name | Quantity
a 40
b 20
遍历行时检查“已发布”列,以查看是否已发布行:
For Each xRows In MasterForm.oTransferRows.Where(Function(x) x.Cells("Posted").Value.ToString() = "No")
然后使用以下psuedo-logic编写代码:
'code to select data - select name from testing where name = 'Product1'
'if(dr.Read())
'update goes here
'Else
' insert goes here
'End if
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.