您的位置:程序门 -> vb -> 数据库(包含打印,安装,报表)



treeview与数据库连接显示问题,有还在用vb6的同仁吗?急……


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


treeview与数据库连接显示问题,有还在用vb6的同仁吗?急……
发表于:2008-01-18 10:07:14 楼主
有个数据表:   房间信息表
里面有字段:nodeid,id
表中内容有:ss,1
                    ss,2
                    ss,3
                    ss,4
                    ss,5
                    ss,6
                    qq,1
                    qq,2
                    qq,3
                    qq,4
                    qq,5
                    qq,6
我现在想实现这个样子的树状结构:温度监控管理系统(主树)
。。。。。。。。。。。。。。。。。。。。。ss(第一棵树)
。。。。。。。。。。。。。。。。。。。。。。。。1(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。2(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。3(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。4(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。5(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。6(第二棵树)
。。。。。。。。。。。。。。。。。。。。。qq(第一棵树)
。。。。。。。。。。。。。。。。。。。。。。。。1(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。2(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。3(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。4(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。5(第二棵树)
。。。。。。。。。。。。。。。。。。。。。。。。6(第二棵树)
现程序是:
  nodekey   =   "温度监控管理系统"
  nodetext   =   "温度监控管理系统"
set   node1   =   treeview1.nodes.add(,   ,   nodekey,   nodetext,   0)
          sqlstr   =   "select     distinct   nodeid,id   from   房间信息表   "
          set   rs   =   esql(sqlstr)
         
              if   rs.recordcount   >   0   then
                      rs.movefirst
                          do   while   not   rs.eof
                                nodekey   =   trim(rs!nodeid)
                                nodetext   =   rs!nodeid
                                set   node2   =   treeview1.nodes.add(node1.index,   tvwchild,   nodekey,   nodetext,   0)
                                  sqlstr1   =   "select     distinct   nodeid,id   from   房间信息表   where   id="   &   rs.fields("id")
                                  set   rs1   =   esql(sqlstr1)
                                        if   rs1.recordcount   >   0   then
                                              rs1.movefirst
                                            do   while   not   rs1.eof
                                                        nodekey   =   trim(rs1!id)
                                                        nodetext   =   rs1!id
                                                        set   node3   =   treeview1.nodes.add(node2.index,   tvwchild,   nodekey,   nodetext,   0)
                                                rs1.movenext
                                            loop
                                          end   if
                                  rs.movenext
                          loop
        end   if

望各位帮个忙,祝大家新年快乐
                                                     
发表于:2008-01-18 10:12:421楼 得分:0
现在treeview中只出现
温度监控管理系统
.....ss
然后就没了
发表于:2008-01-18 10:50:002楼 得分:0
为什么冷清??
发表于:2008-01-18 14:11:043楼 得分:0
怎么没人??
set   node1   =   treeview1.nodes.add(,   ,   nodekey,   nodetext,   0)
          sqlstr   =   "select     distinct   nodeid   from   房间信息表   "
          set   rs   =   esql(sqlstr)

              if   rs.recordcount   >   0   then
                      rs.movefirst
                          do   while   not   rs.eof
                                nodekey   =   trim(rs!nodeid)
                                nodetext   =   rs!nodeid
                                set   node2   =   treeview1.nodes.add(node1.index,   tvwchild,   "nodeid"   &   nodekey,   nodetext,   0)

                                  rs.movenext
                          loop
        end   if
这些能得到主树和第一棵树:
温度监控管理系统(主树)  
。。。。。。ss(第一棵树)  
。。。。。。qq(第一棵树)

发表于:2008-01-18 14:12:224楼 得分:0
后面的1,2,3,4,5,6循环怎么不行了???
发表于:2008-01-18 15:18:505楼 得分:0
set   node1   =   treeview1.nodes.add(,   ,   nodekey,   nodetext,   0)
          sqlstr   =   "select     distinct   nodeid   from   房间信息表   "
          set   rs   =   esql(sqlstr)

              if   rs.recordcount   >   0   then
                      rs.movefirst
                          do   while   not   rs.eof
                                nodekey   =   trim(rs!nodeid)
                                nodetext   =   trim(rs!nodeid)
                                set   node2   =   treeview1.nodes.add(node1.index,   tvwchild,   "nodeid"   &   nodekey,   nodetext,   0)
                                  sqlstr1   =   "select       nodeid,id   from   房间信息表   "   '''where   nodeid="   &   rs.fields("nodeid")
                                  set   rs1   =   esql(sqlstr1)
                                        if   rs1.recordcount   >   0   then
                                              rs1.movefirst
                                            do   while   not   rs1.eof
                                                  if   trim(rs1!nodeid)   =   trim(rs!nodeid)   then
                                                        nodekey   =   trim(rs1!id)
                                                        nodetext   =   trim(rs1!id)
                                                        set   node3   =   treeview1.nodes.add(node2.index,   tvwchild,   "id"   &   nodekey,   nodetext,   0)
                                                    end   if
                                                rs1.movenext
                                            loop
                                          end   if
                                  rs.movenext
                          loop
        end   if
现在能出现:
温度监控管理系统(主树)      
。。。。。。ss(第一棵树)
。。。。。。。1    
。。。。。。。2
。。。。。。。3
。。。。。。。4
。。。。。。。5
。。。。。。。6  
。。。。。。qq(第一棵树)  
这个树没出来
发表于:2008-01-18 15:55:226楼 得分:0
5555555
发表于:2008-01-18 16:00:127楼 得分:0
csdn那csdn怎么连支一声的人都没有哎
发表于:2008-01-21 11:10:118楼 得分:0
哪位吼一吼类
给俺个安慰类
发表于:2008-01-21 12:45:529楼 得分:0
哪位吼一吼类  
给俺个安慰类
发表于:2008-01-21 16:50:5710楼 得分:0
我顶一下,等下再看问题,呵呵
发表于:2008-01-22 10:55:0911楼 得分:0
问题出在rs的第一次循环中rs1的所有循环已经完成,但是rs有二次循环
就是想不到解决方案
发表于:2008-01-22 19:01:4412楼 得分:0
以下为添加目录树的源码,希望对你有帮助:


'添加下一级
private   sub   cmdchild_click()
        dim   onodex   as   node
        dim   skey   as   string
        dim   iindex   as   integer
                lb   =   ""
        iindex   =   treeview1.selecteditem.index   'check   to   see   if   a   node   is   selected
        skey   =   getnextkey()   '   get   a   key   for   the   new   node
        frmlb.show   vbmodal
        if   lb   &   ""   =   ""   then
                exit   sub
        else
                set   onodex   =   treeview1.nodes.add(iindex,   tvwchild,   skey,   lb,   1,   2)
                cmdsave.enabled   =   true
                exit   sub
        end   if
end   sub

'添加同一级
private   sub   cmdfirst_click()
        dim   skey   as   string
        dim   iindex   as   integer
        lb   =   ""
if   addnode   =   "物品分类"   then
        msgbox   "对不起!不能再次添加根目录!"
        exit   sub
else
        iindex   =   treeview1.selecteditem.index   'check   to   see   if   a   node   is   selected
        skey   =   getnextkey()   '   get   a   key   for   the   new   node
end   if
        frmlb.show   vbmodal
        if   lb   &   ""   =   ""   then
        else
        treeview1.nodes.add   iindex,   tvwlast,   skey,   lb,   1,   2
        cmdsave.enabled   =   true
        exit   sub
        end   if
end   sub
private   sub   getfirstparent()
'find   the   first   parent   node   in   the   treeview
        on   error   goto   myerr
        dim   i   as   integer
        dim   ntmp   as   integer
       
        for   i   =   1   to   treeview1.nodes.count
                'this   will   give   an   error   if   there   is   no   parent
                ntmp   =   treeview1.nodes(i).parent.index
        next
        exit   sub
       
myerr:
        mnindex   =   i
        exit   sub
end   sub

private   function   getnextkey()   as   string
'returns   a   new   key   value   for   each   node   being   added   to   the   treeview
'this   algorithm   is   very   simple   and   will   limit   you   to   adding   a   total   of   999   nodes
'each   node   needs   a   unique   key.   if   you   allow   users   to   remove   nodes   you   can't   use
'the   nodes   count   +1   as   the   key   for   a   new   node.

        dim   snewkey   as   string
        dim   ihold   as   integer
        dim   i   as   integer
        on   error   goto   myerr
        'the   next   line   will   return   error   #35600   if   there   are   no   nodes   in   the   treeview
        ihold   =   val(treeview1.nodes(1).key)
        for   i   =   1   to   treeview1.nodes.count
                if   val(treeview1.nodes(i).key)   >   ihold   then
                        ihold   =   val(treeview1.nodes(i).key)
                end   if
        next
        ihold   =   ihold   +   1
        snewkey   =   cstr(ihold)   &   "_"
        getnextkey   =   snewkey   'return   a   unique   key
        exit   function
myerr:
        'because   the   treeview   is   empty   return   a   1   for   the   key   of   the   first   node
        getnextkey   =   "1_"
        exit   function
end   function
private   sub   showtree()
'asks   the   user   to   find   the   database   and   then   restores   the   nodes
'in   the   treeview   control   from   the   table
        dim   onodex   as   node
        dim   nimage   as   integer
        dim   nselectedimage   as   integer
        dim   stablenames   as   string
        'set   mdb   =   dbengine.workspaces(0).opendatabase(app.path   &   "\cx.mdb",   false,   false,   ";pwd=nbmidt920")
        treeview1.nodes.clear   'clear   the   treeview   of   any   nodes
        'set   mrs   =   mdb.openrecordset("splb")
        rs.open   "select   *   from   splb",   cn,   adopenstatic,   adlockoptimistic
        if   rs.recordcount   >   0   then   'make   sure   there   are   records   in   the   table
                rs.movefirst
                do   while   rs.eof   =   false
                        nimage   =   rs.fields("image")
                        nselectedimage   =   rs.fields("selectedimage")
                        if   trim(rs.fields("parent"))   =   "0_"   then   'all   root   nodes   have   0_   in   the   parent   field
                                set   onodex   =   treeview1.nodes.add(,   1,   trim(rs.fields("key")),   _
                                trim(rs.fields("text")),   nimage,   nselectedimage)
                        else   'all   child   nodes   will   have   the   parent   key   stored   in   the   parent   field
                                set   onodex   =   treeview1.nodes.add(trim(rs.fields("parent")),   tvwchild,   _
                                      trim(rs.fields("key")),   trim(rs.fields("text")),   nimage,   nselectedimage)
                                          'expend   the   treeview   so   all   nodes   are   visible
                        end   if
                        rs.movenext
                loop
        end   if
        treeview1.nodes.item(1).expanded   =   true
        rs.close   'close   the   table
        'mdb.close   'close   the   database
end   sub
sub   savetotable()
        dim   sresponse   as   string
        dim   smdbname   as   string
        dim   stablename   as   string
        dim   i   as   integer
        set   mrs   =   new   adodb.recordset
        mrs.open   "select   *   from   splb",   cn,   adopenstatic,   adlockoptimistic
                writetotable     'go   to   the   sub   that   writes   the   nodes   into   the   table
        mrs.close   'close   the   recordset
end   sub

private   sub   cmdok_click()
if   addnode   =   "物品分类"   or   addnode   =   ""   then
        msgbox   "请选择物品分类信息!"
else
        lb   =   addnode
        unload   me
end   if
end   sub

'删除所选类别
private   sub   cmdremove_click()
        dim   iindex   as   integer
if   addnode   =   "物品分类"   then
        msgbox   "无法删除当前选定分类!"
else
        if   msgbox("确定要删除当前选定分类吗?",   36)   =   6   then
                iindex   =   treeview1.selecteditem.index   'check   to   see   if   a   node   is   selected
                treeview1.nodes.remove   iindex   'removes   the   node   and   any   children   it   has
                savetotable
        end   if
end   if
end   sub
'保存
private   sub   cmdsave_click()
call   savetotable
end   sub
sub   writetotable()
        dim   itmp   as   integer
        dim   iindex   as   integer
        if   mrs.recordcount   >   0   then
                mrs.movefirst
                do   while   mrs.eof   =   false
                        mrs.delete
                        mrs.movenext
                loop
        end   if
        getfirstparent   'find   a   root   node   in   the   treeview
        'get   the   index   of   the   root   node   that   is   at   the   top   of   the   treeview
        iindex   =   treeview1.nodes(mnindex).firstsibling.index
        itmp   =   iindex
        mrs.addnew
        mrs("parent")   =   "0_"   'this   is   a   root   node
        mrs("key")   =   treeview1.nodes(iindex).key
        mrs("text")   =   treeview1.nodes(iindex).text
        mrs("image")   =   treeview1.nodes(iindex).image
        mrs("selectedimage")   =   treeview1.nodes(iindex).selectedimage
        mrs.update
        'if   the   node   has   children   call   the   sub   that   writes   the   children
        if   treeview1.nodes(iindex).children   >   0   then
                writechild   iindex
        end   if
       
        while   iindex   <>   treeview1.nodes(itmp).lastsibling.index
        'loop   through   all   the   root   nodes
                mrs.addnew
                mrs("parent")   =   "0_"   'this   is   a   root   node
                mrs("key")   =   treeview1.nodes(iindex).next.key
                mrs("text")   =   treeview1.nodes(iindex).next.text
                mrs("image")   =   treeview1.nodes(iindex).next.image
                mrs("selectedimage")   =   treeview1.nodes(iindex).next.selectedimage
                mrs.update
                  'if   the   node   has   children   call   the   sub   that   writes   the   children
                if   treeview1.nodes(iindex).next.children   >   0   then
                        writechild   treeview1.nodes(iindex).next.index
                end   if
                '   move   to   the   next   root   node
                iindex   =   treeview1.nodes(iindex).next.index
        wend
        cmdsave.enabled   =   false
end   sub
'-------------------------------------
private   sub   writechild(byval   inodeindex   as   integer)
        dim   itempindex   as   integer
        itempindex   =   treeview1.nodes(inodeindex).child.firstsibling.index
        for   i   =   1   to   treeview1.nodes(inodeindex).children
                mrs.addnew----
                mrs("parent")   =   treeview1.nodes(itempindex).parent.key
                mrs("key")   =   treeview1.nodes(itempindex).key
                mrs("text")   =   treeview1.nodes(itempindex).text
                mrs("image")   =   treeview1.nodes(itempindex).image
                mrs("selectedimage")   =   treeview1.nodes(itempindex).selectedimage
                mrs.update
                if   treeview1.nodes(itempindex).children   >   0   then
                        writechild   (itempindex)
                end   if
                if   i   <>   treeview1.nodes(inodeindex).children   then
                        itempindex   =   treeview1.nodes(itempindex).next.index
                end   if
        next   i
end   sub


快速检索

最新资讯
热门点击