在VB.NET中使用Do While Loop复制数据

时间:2015-02-27 08:49:36

标签: sql-server vb.net vb.net-2010 do-while

我只是想问为什么我的数据重复,我该如何防止它?

注意:我的SQL查询工作正常,唯一的问题是它保存的每个数据都是基于ctr的最后一个值重复的

这是我的代码:

  For Each lvi As ListViewItem In lvReportFormat2.Items
            Dim count = lvReportFormat2.Items.Count
            Dim ctr = 0
            Dim orderby = 0

            label.Text = lvReportFormat2.Items(ctr).SubItems(4).Text

            Do While ctr < count
                Label1.Text = lvReportFormat2.Items(ctr).SubItems(4).Text
                Execute("INSERT INTO tblrptChad (accountcode,accountdesc,Type,class,Orderby,ReportType,Formula,Show)VALUES ('" & IIf(lvReportFormat2.Items(ctr).SubItems(0).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(0).Text, "NULL") & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(1).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(1).Text, "NULL") & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(2).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(2).Text, "NULL") & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(3).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(3).Text, "NULL") & "','" & orderby & "','" & Val(IIf(lvReportFormat2.Items(ctr).SubItems(5).Text IsNot DBNull.Value, Val(lvReportFormat2.Items(ctr).SubItems(5).Text), 0)) & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(6).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(6).Text, "NULL") & "','" & Val(IIf(lvReportFormat2.Items(ctr).SubItems(7).Text IsNot DBNull.Value, Val(lvReportFormat2.Items(ctr).SubItems(7).Text), 0)) & "')")
                ctr = ctr + 1
                orderby = orderby + 1
            Loop


        Next

2 个答案:

答案 0 :(得分:0)

您正在插入记录的重复项,因为您在ListView的Items集合上循环了两次。第一次使用foreach lvi ... Next语句,第二次使用Do While loop

对您的问题的完整答案需要向您介绍参数化查询,但这涉及从根本上改变您的Execute方法。
所以立即回答

Dim orderby = 0
For Each lvi As ListViewItem In lvReportFormat2.Items

    Label1.Text = lvi.SubItems(4).Text
    Execute("INSERT INTO tblrptChad "& _
    "(accountcode,accountdesc,Type,class,Orderby," & _
    "ReportType,Formula,Show) VALUES " & _
    "('" & _
       If(lvi.SubItems(0).Text IsNot DBNull.Value, _ 
           lvi.SubItems(0).Text, "NULL") & "','" & _ 
       If(lvi.SubItems(1).Text IsNot DBNull.Value, _
           lvi.SubItems(1).Text, "NULL") & "','" & _
       If(lvi.SubItems(2).Text IsNot DBNull.Value, _
           lvi.SubItems(2).Text, "NULL") & "','" & _
       If(lvi.SubItems(3).Text IsNot DBNull.Value, _
           lvi.SubItems(3).Text, "NULL") & "','" & _
       orderby & "','" & _
       Val(If(lvi.SubItems(5).Text IsNot DBNull.Value, _
           Val(lvi.SubItems(5).Text), 0)) & "','" & _
       If(lvi.SubItems(6).Text IsNot DBNull.Value,  _
           lvi.SubItems(6).Text, "NULL") & "','" & _
       Val(If(lvi.SubItems(7).Text IsNot DBNull.Value, _
           Val(lvi.SubItems(7).Text), 0)) & "')")
   orderby = orderby + 1
Next
每个中的

lvi是当前已编入索引的ListViewItem 这意味着lvi is lvReportFormat2.Items(ctr)

作为旁注,我很确定像SubItems(x).Text这样的字符串属性不能是DBNull值,因此所有这些IIF都是无用的,可以删除。我试试看。

说,你真的应该看看parameterized queries来简化你的命令文本,删除所有那些容易出错的字符串连接(例如,如果你的一个子项包含带有撇号的文本?)并且是Sql Injection attacks的主要载体。

答案 1 :(得分:0)

TO SOLVE THE PROBLE YOU JUST NEED TO ADD "EXIT SUB" OUTSIDE THE LOOP

For Each lvi As ListViewItem In lvReportFormat2.Items
            Dim count = lvReportFormat2.Items.Count
            Dim ctr = 0
            Dim orderby = 0

            label.Text = lvReportFormat2.Items(ctr).SubItems(4).Text

            Do While ctr < count
                Label1.Text = lvReportFormat2.Items(ctr).SubItems(4).Text
                Execute("INSERT INTO tblrptChad (accountcode,accountdesc,Type,class,Orderby,ReportType,Formula,Show)VALUES ('" & IIf(lvReportFormat2.Items(ctr).SubItems(0).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(0).Text, "NULL") & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(1).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(1).Text, "NULL") & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(2).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(2).Text, "NULL") & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(3).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(3).Text, "NULL") & "','" & orderby & "','" & Val(IIf(lvReportFormat2.Items(ctr).SubItems(5).Text IsNot DBNull.Value, Val(lvReportFormat2.Items(ctr).SubItems(5).Text), 0)) & "','" & IIf(lvReportFormat2.Items(ctr).SubItems(6).Text IsNot DBNull.Value, lvReportFormat2.Items(ctr).SubItems(6).Text, "NULL") & "','" & Val(IIf(lvReportFormat2.Items(ctr).SubItems(7).Text IsNot DBNull.Value, Val(lvReportFormat2.Items(ctr).SubItems(7).Text), 0)) & "')")
                ctr = ctr + 1
                orderby = orderby + 1
            Loop

               EXIT SUB

        Next