C++ 从数据库读取数据,将数据显示在界面上的TreeCtrl上

oracle数据库读取数据如下所示:

C++ 从数据库读取数据,将数据显示在界面上的TreeCtrl上_第1张图片

代码如下:

//构造节点
struct AREA
{
   long id;
   long parent_area_id;
   string area_name;
     std::vector<AREA> children;
}

//从AREA表中根据id读一个结构体Area的数据
void Id2FindAreaTree(Session ses, AREA& Area, int id)
{
   Statement select(ses);
   select << "SELECT id, parent_area_id,area_name FROM AREA where id =:pid", into(Area.id), into(Area.parent_area_id), into(Area.area_name), use(id), now;
}
//从AREA表中读取树形数据
void Parent_id2FindAreaTree(Session ses, std::vector<AREA>& vArea, int parent_id)
{            
    vArea.clear();
    typedef Poco::Tuple<long, long, string> Area;
    typedef std::vector<Area> Are;
    Are ar;
    Statement select(ses);
    select << "SELECT id, parent_area_id,area_name FROM AREA where parent_area_id =:pid", into(ar), use(parent_id), now;
    Are::const_iterator it = ar.begin();
    for (; it != ar.end(); ++it)
    {
       AREA area;
       area.id = it->get<0>(); 
       area.parent_area_id = it->get<1>();
       area.area_name = it->get<2>();
       vArea.push_back(area);
    }
}
</span><pre name="code" class="cpp"><span style="font-size:14px;">//构建树结构(入口函数)    Session 是POCO库中连接oracle的工具
void CDlg::CreateTreeCtrl(Session ses, long id, AREA& area, HTREEITEM hItem)
{
		Id2FindAreaTree(ses, area, id);
		HTREEITEM  hCurrItem = m_tree.InsertItem(area.area_name.c_str(), 0, 0, hItem, TVI_LAST);
		std::vector<AREA> temp;
		 Parent_id2FindAreaTree(ses, temp, id);
 		std::vector<AREA>::iterator it;
 		for (it = temp.begin(); it != temp.end(); it++)
		{
			AREA a;
			 CreateTreeCtrl(ses, it->id, a, hCurrItem);
  			if (!a.area_name.empty())
 			{
				 area.children.push_back(a);
			 }
 		}
}</span>

//最后,在初始化函数中,调用入口函数
AREA area;
try {
     ODBC::Connector::registerConnector();
     Session ses("ODBC", "UID=用户名;PWD=密码;DATABASE=数据库名;dsn=配置好的dsn");

     CreateTreeCtrl(ses, 1, area, 0);

     ODBC::Connector::unregisterConnector();
   }
   catch (ConnectionFailedException e)
   {
   }


结果如下图:

C++ 从数据库读取数据,将数据显示在界面上的TreeCtrl上_第2张图片
运行,大功告成了!觉得有用的朋友,记得点赞哦~
 
 

你可能感兴趣的:(C++ 从数据库读取数据,将数据显示在界面上的TreeCtrl上)