在ListControl中访问数据库数据时遇到问题 [英] Having problems accessing database data in ListControl

查看:134
本文介绍了在ListControl中访问数据库数据时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是VC ++编程和使用Microsoft Visual C ++ .NET的新手.我已经在文章中运行了该程序:使用CDatabase类读取Access数据库 [ ^ ].

但是它在构建时会产生错误.我修改了以下功能,如下所示. ResetListControl()函数保持原样.它可以正常运行,但不会显示数据库中的数据.

请帮忙..

Hi,

I am new in VC++ programming and using Microsoft Visual C++ .NET. I have run the program in the article: Using the CDatabase class to read an Access databases[^].

But it generates errors on building. I modified the function below as follows. The ResetListControl()function remains as it is. It runs without errors but does not show the data in the database.

Please help..

void CReadDBDlg::OnRead() 
{
    // TODO: Add your control notification handler code here
    CDatabase database;
    CString SqlString;
    CString sCatID, sCategory;
    CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
    CString sDsn;
    CString sFile = "C:\\Documents and Settings\\Owner\\Desktop\\" 
                    "mfcdataAccess\\readDb\\ReadDB_demo\\Test.mdb";
    int iRec = 0; 
    // Build ODBC connection string
    sDsn.Format("ODBC;DRIVER={%s};DSN='''';DBQ=%s",sDriver,sFile);
    TRY
    {
        // Open the database
        database.Open(NULL,false,false,sDsn);
        // Allocate the recordset
        CRecordset recset( &database );
        // Build the SQL statement
        SqlString = "SELECT CatID, Category "
                    "FROM Categories";
        // Execute the query
        recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
        // Reset List control if there is any data
        ResetListControl();
        // populate Grids
        ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
        // Column width and heading
        m_ListControl.InsertColumn(0,"Category Id",LVCFMT_LEFT,-1,0);
        m_ListControl.InsertColumn(1,"Category",LVCFMT_LEFT,-1,1);
        m_ListControl.SetColumnWidth(0, 120);
        m_ListControl.SetColumnWidth(1, 200);
        short nFields = recset.GetODBCFieldCount( ); //get total no. of fields
        CDBVariant varValue;
        // Loop through each record
        while( !recset.IsEOF() )
        {
            for( short index = 0; index < nFields; index++ )
            {
                // Copy each column into a variable
                recset.GetFieldValue( index, varValue );
                switch ( index ) {
                    case 0: // field no 1
                        if ( varValue.m_dwType == 8 )
                            //Insert values into the list control
                            iRec = m_ListControl.InsertItem(0,varValue.m_pstring->GetBuffer(),0);
                        break;
                    case 1: // field no 2
                        if ( varValue.m_dwType == 8 )
                            m_ListControl.SetItemText(0,1,varValue.m_pstring->GetBuffer());
                        break;
                }
            }
            // goto next record
            recset.MoveNext();
        }
        // Close the database
        recset.Close();
        database.Close();
    }
    CATCH(CDBException, e)
    {
        // If a database exception occured, show error msg
        AfxMessageBox("Database error: "+e->m_strError);
    }
    END_CATCH;
}

// Reset List control
void CReadDBDlg::ResetListControl()
{
    m_ListControl.DeleteAllItems();
    int iNbrOfColumns;
    CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
    if (pHeader)
    {
        iNbrOfColumns = pHeader->GetItemCount();
    }
    for (int i = iNbrOfColumns; i >= 0; i--)
    {
        m_ListControl.DeleteColumn(i);
    }
}

推荐答案

首先,您正在回答一篇文章……正如您明确指出的那样,因此您应该在该页面上发布您的问题.本文的底部是一个讨论此类问题的论坛.

其次,当您发布那么多代码时,您需要告诉我们正在生成哪些错误以及在哪些行上发生错误.否则,我们将不会仔细检查每一行,以试图找出问题所在.
First of all, you are responding to an article...as you clearly said, so you should post your question on that page. At the bottom of the article is a forum for questions such as this.

Secondly, when you post that much code, you need to tell us what errors are being generated and on which lines. Otherwise, we''re not going to go through each line and try to figure out the problem.


这篇关于在ListControl中访问数据库数据时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆