用VC实现按数据库记录构建树控件
2008-11-13 19:30:38 来源:WEB开发网4.添加一个COM变量到CString变量的转换函数:
//-----------------实现了VARIANT类型的值转换成CString类型--------------
CString CBusinessView::VariantToCString(VARIANT var)
{
CString strValue;
_variant_t var_t;
_bstr_t bst_t;
time_t cur_time;
CTime time_value;
COleCurrency var_currency;
switch(var.vt)
{
case VT_EMPTY:strValue=_T("");break;
case VT_UI1:strValue.Format ("%d",var.bVal);break;
case VT_I2:strValue.Format ("%d",var.iVal );break;
case VT_I4:strValue.Format ("%d",var.lVal);break;
case VT_R4:strValue.Format ("%f",var.fltVal);break;
case VT_R8:strValue.Format ("%f",var.dblVal);break;
case VT_CY:
var_currency=var;
strValue=var_currency.Format(0);
break;
case VT_BSTR:
var_t=var;
bst_t=var_t;
strValue.Format ("%s",(const char*)bst_t);
break;
case VT_NULL: strValue=_T(""); break;
case VT_DATE:
cur_time=var.date;
time_value=cur_time;
strValue=time_value.Format("%A,%B%d,%Y");
break;
case VT_BOOL: strValue.Format ("%d",var.boolVal ); break;
default: strValue=_T(""); break;
}
return strValue;
}
5.同样的方法添加另外一个成员函数
TreeAddSubTree(CString ParTree,CString strChildTree,HTREEITEM hPartItem):
此成员函数是一个递归函数.
if (strChildTree!="0")
{
//----------------使用到的变量进行定义----------
_RecordsetPtr m_pTreeRecordset; //用于创建一个查询记录集
_variant_t vChild;
//--------------Tree控件操作变量------------------------
HTREEITEM hCurrent;
//----------------------------------------------
CString strSQL,strCurItem;
//-----------------------------------------------
strSQL="SELECT * FROM TreeItem where ParentItem like ''%" ;
strSQL=strSQL+ParTree+"%''";
try
{
HRESULT hTRes;
hTRes = m_pTreeRecordset.CreateInstance(_T("ADODB.Recordset"));
if (SUCCEEDED(hTRes))
{
//----------------------------------------------------
hTRes = m_pTreeRecordset->Open((LPTSTR)strSQL.GetBuffer(130),
_variant_t((IDispatch *)(((CBusinessApp*)AfxGetApp())->m_pTreeConnection),true),
adOpenDynamic,adLockPessimistic,adCmdText);
if(SUCCEEDED(hTRes))
{
TRACE(_T("连接成功!n"));
//------------------------------------------
m_pTreeRecordset->MoveFirst();
if (!(m_pTreeRecordset->adoEOF))
{
while(!m_pTreeRecordset->adoEOF)
{
hCurrent = m_ctrlTree.InsertItem((LPCTSTR)(_bstr_t)
(m_pTreeRecordset->GetCollect("Name")), hPartItem, NULL);
//---------------将内容添加到City的Combo控件中------------------
m_ctrlComboCity.AddString(VariantToCString(m_pTreeRecordset->GetCollect("Name")));
if (TreeSumRecordCount(VariantToCString
(m_pTreeRecordset->GetCollect("Name")))>0)
{
TreeAddSubTree(VariantToCString(m_pTreeRecordset->GetCollect("Name")),
(VariantToCString(m_pTreeRecordset->GetCollect("Name"))),
hCurrent);
}
if (!(m_pTreeRecordset->adoEOF))
{
m_pTreeRecordset->MoveNext();
}
}
}
//---------------------------------------
}
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
MessageBox("创建City记录集失败!",ParTree+strChildTree);
}
}
更多精彩
赞助商链接