用VC实现按数据库记录构建树控件
2008-11-13 19:30:38 来源:WEB开发网6.添加一个求当前项子项串的成员函数
ReturnTreeChilds(CString strCurItem):
此成员函数也是递归函数.
//----------------提取当前所选择项的子项文本所组成的字符串------------------------
CString CTreeDataDlg::ReturnTreeChilds(CString strCurItem)
{
CString strTreeChildren;//记录子项文本所组成的字符串
if (TreeSumRecordCount(strCurItem) > 0)
{
//--------------------进入递归运算---------------------
_RecordsetPtr m_pTreeRecordset; //用于创建一个查询记录集
_variant_t vCur;
CString strSQL;
//-----------------------------------------------
strSQL="SELECT * FROM TreeItem where ParentItem like ''%" ;
strSQL=strSQL+strCurItem+"%''";
try
{
HRESULT hTRes;
hTRes = m_pTreeRecordset.CreateInstance(_T("ADODB.Recordset"));
if (SUCCEEDED(hTRes))
{
//----------------------------------------------------
hTRes = m_pTreeRecordset->Open((LPTSTR)strSQL.GetBuffer(130),
_variant_t((IDispatch *)(((CTreeDataApp*)AfxGetApp())->m_pTreeConn),true),
adOpenDynamic,adLockPessimistic,adCmdText);
if(SUCCEEDED(hTRes))
{
TRACE(_T("连接成功!n"));
//------------------------------------------
m_pTreeRecordset->MoveFirst();
vCur=(m_pTreeRecordset->GetCollect("Name"));
if (TreeSumRecordCount(VariantToCString(vCur))>=0)
{
while(!m_pTreeRecordset->adoEOF)
{
vCur=(m_pTreeRecordset->GetCollect("Name"));
strTreeChildren+=(",''"+VariantToCString(vCur)+"''");
if (TreeSumRecordCount(VariantToCString(vCur))!=0)
{
strTreeChildren+=ReturnTreeChilds(VariantToCString(vCur));
}
if (!(m_pTreeRecordset->adoEOF))
{
m_pTreeRecordset->MoveNext();
}
}
}
//---------------------------------------
}
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
AfxMessageBox("创建ChildTree记录集失败!"+strCurItem);
}
}
return strTreeChildren;
}
更多精彩
赞助商链接