本文介绍了VB.NET:将两个数据表中的列添加到第三个数据表中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从两个SQL查询中获取两个数据表(dt3和dt4)。现在我想添加MA,QTY(从dt3)和STOCK(从dt4)到新数据表的列。
以下是我的代码,但这是错误的。我知道这是因为循环不好 - 它应该只循环10次,因为有10行。在我的情况下,它循环100x(dt3中的每一行,dt4中的每一行)......我无法弄明白。
提前感谢大家!
dt3:
MA QTY
MA00631 288
MA00631 240
MA00631 470,4
MA00959 1836
MA00959 1108,8
MA00959 1285,2
MA01070 2560
MA01070 1280
MA06195 989,4
MA06210 510
dt4:
MA STOCK
MA00631 19194
MA00631 19194
MA00631 19194
MA00959 34589
MA00959 34589
MA00959 34589
MA01070 72431
MA01070 72431
MA06195 11073
MA06210 17220
代码:
Dim newRow As DataRow
for t As Integer = 0到dt3.Rows.Count - 1
对于t2 As Integer = 0到dt4.Rows.Count - 1
newRow = dt10.NewRow()
dt10.Rows。添加(newRow)
Dim drow As DataRow = dt3.Rows(t)
Dim d row2 As DataRow = dt4.Rows(t2)
newRow(MA)= drow(MA)
newRow(QTY)= drow(QTY)
newRow(STOCK)= drow2(STOCK)
下一个
下一个
解决方案
I get two datatables (dt3 and dt4) from two SQL queries. Now i want to add columns "MA", "QTY" (from dt3) and "STOCK" (from dt4) to new datatable.
Below is my code, but it's wrong. I know it's because of bad looping - it should only loop 10x, because there is 10 rows. In my case it loops 100x (each row in dt3 with each row in dt4)... i can't figure it out.
Thanks everybody in advance!
dt3:
MA QTY MA00631 288 MA00631 240 MA00631 470,4 MA00959 1836 MA00959 1108,8 MA00959 1285,2 MA01070 2560 MA01070 1280 MA06195 989,4 MA06210 510
dt4:
MA STOCK MA00631 19194 MA00631 19194 MA00631 19194 MA00959 34589 MA00959 34589 MA00959 34589 MA01070 72431 MA01070 72431 MA06195 11073 MA06210 17220
Code:
Dim newRow As DataRow For t As Integer = 0 To dt3.Rows.Count - 1 For t2 As Integer = 0 To dt4.Rows.Count - 1 newRow = dt10.NewRow() dt10.Rows.Add(newRow) Dim drow As DataRow = dt3.Rows(t) Dim drow2 As DataRow = dt4.Rows(t2) newRow("MA") = drow("MA") newRow("QTY") = drow("QTY") newRow("STOCK") = drow2("STOCK") Next Next
解决方案
这篇关于VB.NET:将两个数据表中的列添加到第三个数据表中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!