Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共10 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:请教循环遍历结构问题

1楼
RandyBoy 发表于:2011/11/29 21:50:00

由于temptable表是在递归函数中动态增加记录的,所以不能用FOR each 遍历,会出现异常。

 

循环遍历temptable临时表,读取行记录后删除行,直到该表记录行为空,用以下格式遍历,应该怎么样写如下两个语句:

1.终止条件

2.下一条记录指令

 

do while  (终止条件【表中无记录时】)

    ’读取当前记录

    ’删除当前记录

    ’下一条记录指令

loop

2楼
狐狸爸爸 发表于:2011/11/29 22:16:00

For each dr dataRow in DataTables("xxx")

   '循环读取

next

DataTables("xxx").DataRows.Clear '全部删除

3楼
mr725 发表于:2011/11/29 22:17:00

这样可以吗:

for i as integer = tables("临时表").count-1 to 0 step -1

    ..........

next

4楼
RandyBoy 发表于:2011/11/29 22:22:00
呵呵,如果可以这样的话,就不必劳烦大家了
5楼
mr725 发表于:2011/11/29 22:38:00
不懂你的问题了,帮您顶····
6楼
RandyBoy 发表于:2011/11/29 22:42:00

For Each r As DataRow In DataTables("tempbom").DataRows
    If r IsNot Nothing Then
    Dim dt1 As DataTable
    Dim cmd1 As New SQLCommand
    Dim curgoid As Integer
    curgoid = r("goid")
    r.Delete
    cmd1.CommandText = "Select goid,singletotal,comgoid,sf_nonstock From {cp_singlebom} Inner JOIN {product} ON {product}.[_Identify] = {cp_singlebom}.[goid]  where {cp_singlebom}.[goid]=" & CSTR(curgoid)
    dt1 = cmd1.ExecuteReader()
  
    If dt1.DataRows.Count > 0 Then
    '循环调用自身函数
   
    Else
       '追加到数据表中
        Dim r1 As Row = Tables("bomdata").AddNew()
        r1("goid") = curgoid
       ' r("singletotal")=dr("singletotal")
       ' r("sf_summy")= dr("sf_nonstock")
       ' r("sortkey")= Initcnt

   
    End If
   
    End If
  Next

 

很奇怪,一样的代码,刚刚试是正确的,在之前就是在TEMPBOM中有一条记录无法删除。

7楼
狐狸爸爸 发表于:2011/11/29 23:33:00
For i As Integer = DataTables("tempbom").DataRows.Count -1  To 0 Step -1
    Dim r As DataRow = DataTables("tempbom").DataRow(i)
    Dim dt1 As DataTable
    Dim cmd1 As New SQLCommand
    Dim curgoid As Integer
    curgoid = r("goid")
    r.Delete
    cmd1.CommandText = "Select goid,singletotal,comgoid,sf_nonstock From {cp_singlebom} Inner JOIN {product} ON {product}.[_Identify] = {cp_singlebom}.[goid]  where {cp_singlebom}.[goid]=" & CSTR(curgoid)
    dt1 = cmd1.ExecuteReader()
    If dt1.DataRows.Count > 0 Then
        '循环调用自身函数
    Else
        '追加到数据表中
        Dim r1 As Row = Tables("bomdata").AddNew()
        r1("goid") = curgoid
        ' r("singletotal")=dr("singletotal")
        ' r("sf_summy")= dr("sf_nonstock")
        ' r("sortkey")= Initcnt
    End If
Next
[此贴子已经被作者于2011-11-29 23:32:49编辑过]
8楼
狐狸爸爸 发表于:2011/11/30 11:55:00

没有看懂,避免出错的方法:

 

Public Function GenProductBom(ByVal rootid As Integer,ByVal goid As Integer,ByVal formatstr As String,ByVal NeedTotal As Double,ByVal onetotal As Double) As Boolean
Dim cmd As New SQLCommand
Dim dt As DataTable
cmd.CommandText ="Select goid,singletotal,comgoid,sf_nonstock,ord From {cp_singlebom} Inner JOIN {product} ON {product}.[_Identify] = {cp_singlebom}.[goid]  where {cp_singlebom}.[goid]=" & Cstr(goid)
dt = cmd.ExecuteReader()
Dim r2 As Row = Tables("bomdata").AddNew()
r2("goid") = rootid
r2("nodegoid") = goid
r2("sortkey") = Formatstr
r2("total") = NeedTotal
r2("Singletotal") = onetotal

If dt.DataRows.Count > 0 Then
    For Each dr As DataRow In dt.DataRows
        Dim r As Row = Tables("tempbom").AddNew()
        r("goid")=dr("comgoid")
        r("singletotal") = dr("singletotal")
        r("total") = dr("singletotal") * NeedTotal
        r("sortkey")= Formatstr + "/" + Format(dr("ord"),"000")
    Next
End If

Dim cnt As Integer = DataTables("tempbom").DataRows.Count -1

Do While DataTables("tempbom").DataRows.Count > 0
    Dim r  As DataRow = DataTables("tempbom").DataRows(0)
    Dim dt1 As DataTable
    Dim cmd1 As New SQLCommand
    Dim curgoid As Integer
    Dim fstr As String
    Dim curSingletotal As Double = r("singletotal")
    Dim curTotal As Double = r("total")
    curgoid = r("goid")
    fstr = r("sortkey")
    'MessageBox.Show(cstr(curgoid),"info",MessageBoxButtons.OK,MessageBoxIcon.Information)
    r.Delete
    cmd1.CommandText = "Select goid,singletotal,comgoid,sf_nonstock,ord From {cp_singlebom} Inner JOIN {product} ON {product}.[_Identify] = {cp_singlebom}.[goid]  where {cp_singlebom}.[goid]=" & CSTR(curgoid)
    dt1 = cmd1.ExecuteReader()
   
    If dt1.DataRows.Count > 0 Then
        GenProductBom(rootid,curgoid,fstr,curtotal,cursingletotal)
    Else
        '追加到数据表中
        Dim r1 As Row = Tables("bomdata").AddNew()
        r1("goid") = rootid
        r1("nodegoid") = curgoid
        r1("sortkey") = fstr
        r1("singletotal")=cursingletotal
        'r1("sf_summy")= dr("sf_nonstock")
        r1("total")= curtotal
    End If
Loop
Return True
End Function

[此贴子已经被作者于2011-11-30 11:55:28编辑过]
9楼
RandyBoy 发表于:2011/11/30 12:05:00

Dim cnt As Integer = DataTables("tempbom").DataRows.Count -1

这句代码是用来做诱的吧。。

 

哈哈。。还是要感谢你的支持和帮助,只要不出错就OK~

 

 

10楼
狐狸爸爸 发表于:2011/11/30 12:15:00
这是多余的一行代码,删除即可。
共10 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .03320 s, 2 queries.