学数据库,我先学odbc,我在CRecordset上建立个类,然后建立对象,启动默认连接,设了一个数据源Student Register,在菜单上加了一项Do(ID_DO)
      函数如下:
      void CODBCDemoView::OnDo() 
{
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.DeleteAllItems();
while(ctrlList.DeleteColumn(0));
UpdateWindow(); CString strSQL;
strSQL = _T("SELECT * FROM SalesByProduct;"); if(!ShowInformation(strSQL)) AfxMessageBox("数据获取失败!");
}
       BOOL CODBCDemoView::ShowInformation(CString strSQL)
{
CRect rect;
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.GetWindowRect(rect); try{
// get recordset field information
BeginWaitCursor();
if(m_pCommonRS->IsOpen()) m_pCommonRS->Close();  
m_pCommonRS->Open(CRecordset::dynaset, strSQL);
if(!m_pCommonRS->IsEOF()){
m_pCommonRS->MoveLast(); 
m_pCommonRS->MoveFirst(); 
}
int nFieldCount = m_pCommonRS->GetODBCFieldCount();
CODBCFieldInfo fieldinfo; 
for(int n=0;n<nFieldCount;n++){
m_pCommonRS->GetODBCFieldInfo(n, fieldinfo);
int nWidth = ctrlList.GetStringWidth(fieldinfo.m_strName) + 15;
ctrlList.InsertColumn(n, fieldinfo.m_strName, LVCFMT_LEFT, nWidth);
}
// get recordset data information
CString strValue; 
m_pCommonRS->MoveFirst(); 
int nCount = 0; while(!m_pCommonRS->IsEOF()){
ctrlList.InsertItem(nCount, strValue);
for(int j=0;j<nFieldCount;j++){
m_pCommonRS->GetFieldValue(j, strValue);
ctrlList.SetItemText(nCount, j, strValue);
}
m_pCommonRS->MoveNext(); 
nCount ++;
}
EndWaitCursor();
} catch(CDBException *e){
e->ReportError();
EndWaitCursor();
return FALSE;
} return TRUE;
}
但编译过后却报:“数据被截断”;
                “数据获取失败”;
???

解决方案 »

  1.   

    CString strValue; 
    m_pCommonRS->MoveFirst(); 
    int nCount = 0;while(!m_pCommonRS->IsEOF()){
    ctrlList.InsertItem(nCount, strValue);问题1,这时候strValue还有没值。
    for(int j=0;j<nFieldCount;j++)
    {
      m_pCommonRS->GetFieldValue(j, strValue);
      ctrlList.SetItemText(nCount, j, strValue);
    }
    问题2:ctrlList.InsertItem(nCount, strValue);插入第一列。for(int j=0;j<nFieldCount;j++)插入了nFieldCount列。而你InsertColumn的时候,一共也只有nFieldCount列。你却想插入nFieldCount+1列,所以出错,数据被截断。
      

  2.   

    正确写法应该是:
    while(!m_pCommonRS->IsEOF())
    {
          m_pCommonRS->GetFieldValue(short(0), strValue);
    ctrlList.InsertItem(nCount, strValue);问题1,这时候strValue还有没值。
    for(int j=1;j<nFieldCount;j++)
    {
      m_pCommonRS->GetFieldValue(j, strValue);
      ctrlList.SetItemText(nCount, j, strValue);
    }
      

  3.   

    SQL语句加‘;’能否正常运行.
      

  4.   

    我改了,但还是不行。错误还是一样,我在CODBCView::CODBCView{m_pCommonRS=NULL;},在CODBCView.cpp里声明的全局变量: CCommonRs *m_pCommonRS; 
    void CODBCDemoView::OnInitialUpdate()
    {
         CListView::OnInitialUpdate(); CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
    ctrlList.SetExtendedStyle(LVS_EX_FULLROWSELECT); 
    // TODO: You may populate your ListView with items by directly accessing
    //  its list control through a call to GetListCtrl().
        m_pCommonRS = new CCommonRs;
    }
     我觉得应该没问题,这是一本书上说的,而且还有例子,但他的例子比这复杂(也就是我这是那的一部分),但例子却没有一点问题。
      

  5.   

    SQL语句加不加;都一样。你的数据长度是不是大于255了?数据被截断。
    主要是这个函数,请看:
    void RFX_Text( CFieldExchange* pFX, const char* szName, CString& value, int nMaxLength = 255, int nColumnType = SQL_VARCHAR, short nScale = 0 );注意int nMaxLength = 255如果你不指定长度的话就只有255所以超过255就会出错,使用这个函数时,指定长度,就不会出错了.