2006
02-04

void CDAOView::OnStuffView()
{
 // TODO: Add your command handler code here
 //设置当前操作的数据库表名称
 m_strTableName=_T(“物品”);
 //监督DAO数据库对象的有效性,并在记录集对象打开时关闭记录集
 if(!m_pDatabase->IsOpen())return;
 if(!m_pRecordset)return;
 if(m_pRecordset->IsOpen())m_pRecordset->Close();
 //清除所有视图上的显示
 CListCtrl& ctlList=GetListCtrl();
 ctlList.DeleteAllItems();
 ctlList.ModifyStyle(0, LVS_ALIGNLEFT | LVS_REPORT);



 //取表的结构信息
 CDaoFieldInfo fieldInfo;
 int nFields;
 LV_COLUMN lvc;



 CDaoTableDef td(m_pDatabase);
 try
 {
  td.Open(m_strTableName);
  nFields=td.GetFieldCount();
   lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  for(int j=0;j<nFields;j++)
  {char * sd;
   td.GetFieldInfo(j,fieldInfo);
   int nWidth=ctlList.GetStringWidth(fieldInfo.m_strName)+15;
   lvc.iSubItem =j;
  // lvc.pszText = _T(“fieldInfo”);
   sd=new char[100];
    sd= strcpy(sd,fieldInfo.m_strName.GetBuffer);
   lvc.pszText=sd;


   //.GetBuffer;
   lvc.cx =nWidth;
   lvc.fmt = LVCFMT_LEFT;
    delete sd;
   ctlList.InsertColumn(j,&lvc);
  }
 
 }
 catch(CDaoException * e)
 {
  e->ReportError();
  e->Delete();
  return;
 }
 td.Close();
 //取表的数据
 int nItem=0;
 try
 {
  CString strSelect(_T(“Select * From ["));
  strSelect+=m_strTableName;
  strSelect += _T("]“);
  m_pRecordset->Open(dbOpenDynaset,strSelect);
  while(!m_pRecordset->IsEOF())
  {
   COleVariant var;
   var=m_pRecordset->GetFieldValue(0);
   ctlList.InsertItem(nItem, strVARIANT(var));
 //  ctlList.InsertItem(nItem,var);
   for(int i=0;i<nFields;i++)
   {
    var=m_pRecordset->GetFieldValue(i );
    ctlList.SetItemText(nItem,i, strVARIANT(var));
 //   ctlList.SetItemText(nItem,i,var);


   }
   nItem++;
   m_pRecordset->MoveNext();
  }
 }
 catch(CDaoException *e)
 {
  e->ReportError();
  e->Delete();
  return;
 }
 //显示信息
 CString strRecCount;
 strRecCount.Format(_T(“显示了%d条记录。”),nItem);
 UpdateWindow();
 if(nItem>=20) MessageBox(strRecCount);
 ((CFrameWnd *)AfxGetMainWnd())->SetMessageText(strRecCount);
 
}


程序出错》有 3 条评论

  1. lisypro 说:

        strcpy(sd,fieldInfo.m_strName.GetBuffer); //本句不能通过

  2. xstar 说:

    sd = strcpy(sd,fieldInfo.m_strName.GetBuffer());

  3. lisypro 说:

    改成这样也OK了,去掉sd变量

       lvc.pszText=(LPSTR)(LPCTSTR)fieldInfo.m_st rName;

留下一个回复