首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

数据库访问控件:递归 逻辑优化 望指教解决办法

2012-03-15 
数据库访问控件:递归 逻辑优化望指教实现思路:每调用一次递归函数,就查询一条记录并添加到treeview中(之前

数据库访问控件:递归 逻辑优化 望指教
实现思路:每调用一次递归函数,就查询一条记录并添加到treeview中(之前是把数据全查出来然后再递归)
相关数据仍是之前用到的数据结构

C/C++ code
void __fastcall TForm1::AddTree(AnsiString parentID,TTreeNode *Node){    SQLQuery1->SQL->Text=        "select a.PDMObject_ID as Id,  \        ISNULL(b.PDMObject_ID_Parent,0) as pId, a.User_ID, a.User_Name  \        from PDM_Users_Roles_ORG a left join  \        Pdm_Organization_tree b on a.PDMObject_ID=b.PDMObject_ID_Son  \        where a.PDMObject_ID in  \        (select distinct PDMObject_ID_Son as id from PDM_Organization_Tree \         union \        select distinct PDMObject_ID_Parent as id from PDM_Organization_Tree) \        and a.PDMObject_ID="+parentID;    SQLQuery1->Open();    for(SQLQuery1->First();!SQLQuery1->Eof;SQLQuery1->Next())    {        int Id,pId;        Id = SQLQuery1->FieldByName("Id")->AsInteger;        AnsiString text;        text.sprintf("%d(%s %s)",Id,                    SQLQuery1->FieldByName("User_ID")->AsString,                    SQLQuery1->FieldByName("User_Name")->AsString                    );        TTreeNode *temp_Node; //临时节点        if((int)Node->Data == parentID)   //若 == 则为父节点        {            for(int i = 0;i<TreeView1->Items->Count;i++ )       //遍历树            {                if( (int)TreeView1->Items->Item[i]->Data == parentID)                    temp_Node = TreeView1->Items->Item[i];        //指向父节点            }            Node = temp_Node;        }        temp_Node = TreeView1->Items->AddChild(Node,text);        temp_Node->Data = (void*)Id;        AddTree(SQLQuery1->FieldByName("pId")->AsString,Node);    }}

疑问:
1.对于表中的根节点(这里是3)由于根节点没有父亲(parentID)(处理后置0)该如何调用?
如:开始时调用AddTree(0,0);不会读取根节点的相关数据
2.进行断点调试时,到if((int)Node->Data == parentID) 这句没有报错,进行下一步时报错:
Exception class EAccessViolation with message 'Access violation at address 00401F5F in module 'recursion.exe'. Read of address 0000000C'. Process recursion.exe (3864)
请指点 谢谢!

[解决办法]
试试

C/C++ code
void __fastcall TForm1::AddTree(AnsiString parentID,TTreeNode *Node){    SQLQuery1->SQL->Text=        "select a.PDMObject_ID as Id,  \        ISNULL(b.PDMObject_ID_Parent,0) as pId, a.User_ID, a.User_Name  \        from PDM_Users_Roles_ORG a left join  \        Pdm_Organization_tree b on a.PDMObject_ID=b.PDMObject_ID_Son  \        where a.PDMObject_ID in  \        (select distinct PDMObject_ID_Son as id from PDM_Organization_Tree \         union \        select distinct PDMObject_ID_Parent as id from PDM_Organization_Tree) \        and pId ="+parentID;    SQLQuery1->Open();    for(SQLQuery1->First();!SQLQuery1->Eof;SQLQuery1->Next())    {        int Id,pId;        Id = SQLQuery1->FieldByName("Id")->AsInteger;        AnsiString text;        text.sprintf("%d(%s %s)",Id,                    SQLQuery1->FieldByName("User_ID")->AsString,                    SQLQuery1->FieldByName("User_Name")->AsString                    );        TTreeNode *temp_Node; //临时节点        if(Node && (int)Node->Data == parentID)   //若 == 则为父节点        {            for(int i = 0;i<TreeView1->Items->Count;i++ )       //遍历树            {                if( (int)TreeView1->Items->Item[i]->Data == parentID)                    temp_Node = TreeView1->Items->Item[i];        //指向父节点            }            Node = temp_Node;        }        temp_Node = TreeView1->Items->AddChild(Node,text);        temp_Node->Data = (void*)Id;        AddTree(SQLQuery1->FieldByName("pId")->AsString,Node);    }}
------解决方案--------------------


SQLQuery1->SQL->Text = .... ; 
SQLQuery1->Open();
for(SQLQuery1->First();!SQLQuery1->Eof;SQLQuery1->Next())
{
.....\\Find ParentNode
temp_Node = TreeView1->Items->AddChild(Node,text);
...
AddTree(SQLQuery1->FieldByName("pId")->AsString,Node);//这里递归?
}
//这样是不行的吧。。。SQLQuery1作为全局数据,如何遍历记录集?

"每调用一次递归函数,就查询一条记录",如果是确认只查询回一条记录,又何必 for ...



热点排行