以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  遍历复制数据,数据条数对,但是数据内容是最后一行复制的多条……怎么修正按照样本表直接全部复制呢?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=184458)

--  作者:cnsjroom
--  发布时间:2022/12/8 10:53:00
--  遍历复制数据,数据条数对,但是数据内容是最后一行复制的多条……怎么修正按照样本表直接全部复制呢?

遍历复制数据,数据条数对,但是数据内容是最后一行复制的多条……怎么修正按照样本表直接全部复制呢?

当前样本表:

 
图片点击可在新窗口打开查看此主题相关图片如下:1.png
图片点击可在新窗口打开查看

当前代码运行后得到的数据表:

 
图片点击可在新窗口打开查看此主题相关图片如下:2.png
图片点击可在新窗口打开查看

 

当前代码:

Dim cmd As New SQLC ommand
cmd.Conn ectio nName = "主数据源"
cmd.CommandText = "Sel ect * From 登记台账"
Dim dt11 As DataTable = cmd.ExecuteReader()
cmd.CommandText = "delet e from {登记台账汇总表}"
cmd.ExecuteNonQuery()
cmd.CommandText = "Selec t * From 登记台账汇总表"
Dim dt12 As DataTable = cmd.ExecuteReader(True)
Dim Products As List(Of String)
Products =  dt11.GetValues("guid")
For Each Product As String In Products
    Output.Show(Product)
    Dim dr1 As DataRow = dt12.AddNew
    For Each dc As DataCol In dt11.DataCols
        Output.Show(dc.Name)
        For Each dr As DataRow In dt11.datarows
            If dr.isnull(dc.Name)=False
                dr1(dc.Name) = dr(dc.Name)
            End If
        Next
    Next
    dt12.save()
Next

 

 

写法二:【运行后还是一样的效果】

Dim cmd As New SQ LCommand
cmd.Connecti
cmd.CommandText = "Sele ct * From 日常监督检查登记台账"
Dim dt11 As DataTable = cmd.ExecuteReader()
cmd.CommandText = "dele te from {日常监督检查登记台账汇总表}"
cmd.ExecuteNonQuery()
cmd.CommandText = "Sele ct * From 日常监督检查登记台账汇总表"
Dim dt12 As DataTable = cmd.ExecuteReader(True)
Dim Products As List(Of String)
Products =  dt11.GetValues("guid")
For Each Product As String In Products
    Output.Show(Product)
    Dim dr1 As DataRow = dt12.AddNew
    For Each dr As DataRow In dt11.Se lect("guid=‘“& Product &"\'")
        For Each dc As DataCol In dt11.DataCols
            Output.Show(dc.Name)
            If dr.isnull(dc.Name)=False
                dr1(dc.Name) = dr(dc.Name)
            End If
        Next
    Next
    dt12.save()
Next

[此贴子已经被作者于2022/12/8 10:57:32编辑过]

--  作者:有点蓝
--  发布时间:2022/12/8 10:59:00
--  
Dim dt12 As DataTable = cmd.ExecuteReader(True)
        For Each dr As DataRow In dt11.datarows
    Dim dr1 As DataRow = dt12.AddNew
    For Each dc As DataCol In dt11.DataCols
        Output.Show(dc.Name)
            If dr.isnull(dc.Name)=False
                dr1(dc.Name) = dr(dc.Name)
            End If
        Next
    Next
    dt12.save()