Sql server 在VB.NET中使用Do While循环复制数据
我只是想问为什么我的数据会重复,我该如何防止它 注意:我的SQL查询工作正常,唯一的问题是它保存的每个数据都是基于ctr的最后一个值复制的 这是我的密码:Sql server 在VB.NET中使用Do While循环复制数据,sql-server,vb.net,vb.net-2010,do-while,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
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
lvReportFormat2.Items中的每个lvi As ListViewItem的
Dim count=lvReportFormat2.Items.count
尺寸中心=0
Dim orderby=0
label.Text=lvReportFormat2.Items(ctr).子项(4).Text
当ctr<计数时执行
Label1.Text=lvReportFormat2.Items(ctr).子项(4).Text
执行(“插入到tblrptChad(accountcode、accountdesc、Type、class、Orderby、ReportType、Formula、Show)值(”)&IIf(lvReportFormat2.Items(ctr)。子项(0)。文本不为DBNull.Value,lvReportFormat2.Items(ctr)。文本“NULL”)&“,”&IIf(lvReportFormat2.Items(ctr)。子项(1)。文本不为DBNull.Value,lvReportFormat2.Items(ctr)。子项(1).Text,“NULL”)&“,”&&IIf(lvReportFormat2.Items(ctr).子项(2).Text不是DBNull.Value,lvReportFormat2.Items(ctr).子项(2).Text,“NULL”)&“,”&“,”&&IIf(lvReportFormat2.Items(ctr).子项(3).Text不是DBNull.Value,lvReportFormat2.Items(ctr).子项(3).Text,“NULL”)&“&“,”orderby&“,”和“,”IIf(lvReportFormat2.Items(ctr).子项(5) .Text不是DBNull.Value,Val(lvReportFormat2.Items(ctr).子项(5.Text),0)和“,”和IIf(lvReportFormat2.Items(ctr).子项(6).Text不是DBNull.Value,lvReportFormat2.Items(ctr).子项(6).Text,“NULL”)和“,”和Val(IIf(lvReportFormat2.Items(ctr).子项(7).文本不是DBNull.Value,Val(lvReportFormat2.Items(ctr).子项(7.Text),0)) & "')")
ctr=ctr+1
orderby=orderby+1
环
下一个
您正在插入记录的副本,因为您在ListView的Items集合上循环了两次。第一次使用每个lvi的语句,第二次使用Do While循环
完整回答您的问题需要向您介绍参数化查询,但这需要从根本上改变您的执行方法。
因此,我们需要立即的答案
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
for each中的lvi
是当前索引的ListViewItem
。
这意味着lvi是lvReportFormat2.Items(ctr)
作为补充说明,我非常确定像SubItems(x).Text
这样的字符串属性不能是DBNull值,因此所有这些IIF都非常无用,可以删除。我让您试试
话虽如此,您确实应该考虑简化您的命令文本,删除所有容易出错的字符串连接(例如,您是否检查了其中一个子项包含带撇号的文本时会发生什么情况?)并将其作为主向量。要解决此问题,只需添加“EXIT SUB”圈外
对于lvReportFormat2.Items中的每个lvi As ListViewItem
Dim count=lvReportFormat2.Items.count
尺寸中心=0
Dim orderby=0
label.Text=lvReportFormat2.Items(ctr).子项(4).Text
当ctr<计数时执行
Label1.Text=lvReportFormat2.Items(ctr).子项(4).Text
执行(“插入到tblrptChad(accountcode、accountdesc、Type、class、Orderby、ReportType、Formula、Show)值(”)&IIf(lvReportFormat2.Items(ctr)。子项(0)。文本不为DBNull.Value,lvReportFormat2.Items(ctr)。文本“NULL”)&“,”&IIf(lvReportFormat2.Items(ctr)。子项(1)。文本不为DBNull.Value,lvReportFormat2.Items(ctr)。子项(1).Text,“NULL”)&“,”&&IIf(lvReportFormat2.Items(ctr).子项(2).Text不是DBNull.Value,lvReportFormat2.Items(ctr).子项(2).Text,“NULL”)&“,”&“,”&&IIf(lvReportFormat2.Items(ctr).子项(3).Text不是DBNull.Value,lvReportFormat2.Items(ctr).子项(3).Text,“NULL”)&“&“,”orderby&“,”和“,”IIf(lvReportFormat2.Items(ctr).子项(5) .Text不是DBNull.Value,Val(lvReportFormat2.Items(ctr).子项(5.Text),0)和“,”和IIf(lvReportFormat2.Items(ctr).子项(6).Text不是DBNull.Value,lvReportFormat2.Items(ctr).子项(6).Text,“NULL”)和“,”和Val(IIf(lvReportFormat2.Items(ctr).子项(7).文本不是DBNull.Value,Val(lvReportFormat2.Items(ctr).子项(7.Text),0)) & "')")
ctr=ctr+1
orderby=orderby+1
环
出口接头
下一个
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