CString CTestDG2View::GetTreeCatalog(int ParentID)
{
CString RetInfo;
RetInfo = "<Root>";    BOOL bFind = FALSE;
try 
{
int nIndex=0; //记录序号
CString strSQL = "";
strSQL.Format(" select * from PostsType where TypeID = %d ",ParentID);

m_pRecordSet->Open(_variant_t(strSQL),
m_pConnection.GetInterfacePtr(), 
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(!m_pRecordSet->adoEOF)
{
while (!m_pRecordSet->adoEOF)//记录尾
{
_variant_t var;
CString strName;
CString strTypeID;
CString strCatalogURL;
CString strParentID;
CString strDisplayOrder; RetInfo +="<RowItem>";
RetInfo +="<Name>";
var = m_pRecordSet->GetCollect("Name");
if (var.vt != VT_NULL)
{
strName = (LPCSTR)_bstr_t(var);
}
RetInfo +=strName+"</Name>"; RetInfo +="<CatalogURL>";
var = m_pRecordSet->GetCollect("CatalogURL");
if (var.vt != VT_NULL)
{
strCatalogURL = (LPCSTR)_bstr_t(var);
  }
RetInfo +=strCatalogURL+"</CatalogURL>"; RetInfo +="<TypeID>";
var = m_pRecordSet->GetCollect("TypeID");
if (var.vt != VT_NULL)
{
strTypeID = (LPCSTR)_bstr_t(var);
  }
                RetInfo +=strTypeID+"</TypeID>";                RetInfo +="<ParentID>";
var = m_pRecordSet->GetCollect("ParentID");
if (var.vt != VT_NULL)
{
strParentID =  (LPCSTR)_bstr_t(var);
  } 
                RetInfo +=strParentID+"</ParentID>"; RetInfo +="<DisplayOrder>";
var = m_pRecordSet->GetCollect("DisplayOrder");
if (var.vt != VT_NULL)
{
strDisplayOrder =  (LPCSTR)_bstr_t(var) ;
  }
RetInfo +=strDisplayOrder+"</DisplayOrder>"; RetInfo +="</RowItem>";
int TypeID = (int )atoi(strTypeID);
                RetInfo += GetSubCatalog( TypeID ) ;
m_pRecordSet->MoveNext(); nIndex++;                 //记录序号加1
}
}
else
{
                bFind = FALSE; 
}
m_pRecordSet->Close();//关闭
}
catch(_com_error *)
{
//AfxMessageBox(e->ErrorMessage());
//要用日志替换
}
catch(...)
{
AfxMessageBox("访问数据库时发生异常。");
//要用日志替换
//return "";

RetInfo+="</Root>";     return RetInfo;

}

解决方案 »

  1.   


    CString CTestDG2View::GetSubCatalog(int ParentID)
    {
        CString RetInfo;
    RetInfo= "";
    //  try 
    // {
    int nIndex=0; //记录序号
    CString strSQL = "";
    strSQL.Format( "select * from PostsType where ParentID = %d ", ParentID);

          m_pSubRecordSet->Open(_variant_t(strSQL),
    m_pConnection.GetInterfacePtr(), 
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    if(!m_pSubRecordSet->adoEOF)
    {
            cmom.LogsWrite("Enter");
    while (!m_pSubRecordSet->adoEOF)//记录尾
    {
    cmom.LogsWrite("Enter2");
    _variant_t var;
    CString strName;
    CString strTypeID;
    CString strCatalogURL;
    CString strParentID;
    CString strDisplayOrder; RetInfo +="<RowItem>";
    RetInfo +="<Name>";
    var = m_pSubRecordSet->GetCollect("Name");
    if (var.vt != VT_NULL)
    {
    strName = (LPCSTR)_bstr_t(var);
    }
    RetInfo +=strName+"</Name>";
    RetInfo +="<TypeID>";
    var = m_pSubRecordSet->GetCollect("TypeID");
    if (var.vt != VT_NULL)
    {
    strTypeID = (LPCSTR)_bstr_t(var);
      }
                    RetInfo +=strTypeID+"</TypeID>";
    RetInfo +="</RowItem>";
    int TypeID = (int )atoi(strTypeID);
                    RetInfo += GetSubCatalog( TypeID  ) ;
    m_pRecordSet->MoveNext();
    nIndex++;
    cmom.LogsWrite("logout");
    }
    cmom.LogsWrite("Enter3");
    }
    m_pSubRecordSet->Close();//关闭
    //  }
    //  catch(_com_error *e)
    //  {
    //  //AfxMessageBox(e->ErrorMessage()+"sub");
    //  AfxMessageBox("subException");
    //  //要用日志替换
    //  }
    //  catch(...)
    //  {
    //  AfxMessageBox("访问数据库时发生异常sub。");
    //  //要用日志替换
    //  }     return RetInfo;
    }