long CADOAccess::ExecuteScalar(LPCTSTR strSQL
        , CString& strResult
        , CommandTypeEnum enumCmdType
        , int iParaNum
        , _PARAMETER_INFO paraInfo[])
{
    _ParameterPtr   pPara;
    _variant_t      varResult;    if (m_pCommand == NULL)
    {
        m_pCommand.CreateInstance("ADODB.Command");
    }    m_pCommand->CommandText = strSQL;
    m_pCommand->ActiveConnection = m_pConnection;
    m_pCommand->CommandType = enumCmdType;    for (int i = 0; i < iParaNum; i++)
    {
        pPara = m_pCommand->CreateParameter(paraInfo[i].strParaName
            , paraInfo[i].enumDataType, paraInfo[i].enumParaDirec
            , paraInfo[i].lParaSize, paraInfo[i].strParaValue);
        m_pCommand->Parameters->Append((IDispatch*)pPara);
    }    m_pRecordset = m_pCommand->Execute(NULL, NULL, enumCmdType);    varResult = m_pRecordset->Collect[0];
    if (varResult.vt == VT_NULL || varResult.vt == VT_EMPTY)
    {
        strResult = "";
    }
    else
    {
        strResult = varResult;
    }    CloseRecordset();    return m_pRecordset->RecordCount;
}m_pRecordset->RecordCount始终为-1
并且在varResult = m_pRecordset->Collect[0];处报错....

解决方案 »

  1.   

    这么多数据库操作都不见一个try catch...
      

  2.   

    好  加上trylong CADOAccess::ExecuteScalar(LPCTSTR strSQL
            , CString& strResult
            , CommandTypeEnum enumCmdType
            , int iParaNum
            , _PARAMETER_INFO paraInfo[])
    {
        _ParameterPtr   pPara;
        _variant_t      varResult;    try
        {
            if (m_pCommand == NULL)
            {
                m_pCommand.CreateInstance("ADODB.Command");
            }        m_pCommand->CommandText = strSQL;
            m_pCommand->ActiveConnection = m_pConnection;
            m_pCommand->CommandType = enumCmdType;        for (int i = 0; i < iParaNum; i++)
            {
                pPara = m_pCommand->CreateParameter(paraInfo[i].strParaName
                    , paraInfo[i].enumDataType, paraInfo[i].enumParaDirec
                    , paraInfo[i].lParaSize, paraInfo[i].strParaValue);
                m_pCommand->Parameters->Append((IDispatch*)pPara);
            }        m_pRecordset = m_pCommand->Execute(NULL, NULL, enumCmdType);        m_pRecordset
            varResult = m_pRecordset->Collect[0];
            if (varResult.vt == VT_NULL || varResult.vt == VT_EMPTY)
            {
                strResult = "";
            }
            else
            {
                strResult = varResult;
            }        CloseRecordset();
        }
    catch (_com_error e)
    {
    TRACE(_T("Warning: GetMode 发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
    return -1;

        return m_pRecordset->RecordCount;
    }
      

  3.   

    long CADOAccess::ExecuteScalar(LPCTSTR strSQL
            , CString& strResult
            , CommandTypeEnum enumCmdType
            , int iParaNum
            , _PARAMETER_INFO paraInfo[])
    {
        _ParameterPtr   pPara;
        _variant_t      varResult;    try
        {
            if (m_pCommand == NULL)
            {
                m_pCommand.CreateInstance("ADODB.Command");
            }        m_pCommand->CommandText = strSQL;
            m_pCommand->ActiveConnection = m_pConnection;
            m_pCommand->CommandType = enumCmdType;        for (int i = 0; i < iParaNum; i++)
            {
                pPara = m_pCommand->CreateParameter(paraInfo[i].strParaName
                    , paraInfo[i].enumDataType, paraInfo[i].enumParaDirec
                    , paraInfo[i].lParaSize, paraInfo[i].strParaValue);
                m_pCommand->Parameters->Append((IDispatch*)pPara);
            }        m_pRecordset = m_pCommand->Execute(NULL, NULL, enumCmdType);        varResult = m_pRecordset->Collect[0];
            if (varResult.vt == VT_NULL || varResult.vt == VT_EMPTY)
            {
                strResult = "";
            }
            else
            {
                strResult = varResult;
            }        CloseRecordset();
        }
    catch (_com_error e)
    {
    TRACE(_T("Warning: GetMode 发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
    return -1;

        return m_pRecordset->RecordCount;
    }
      

  4.   

    m_pRecordset = m_pCommand->Execute(NULL, NULL, enumCmdType); 
    这一句可以正常执行,但是对m_pRecordset的操作出现异常
      

  5.   

    你先判断一下EOF BOF怀疑是没有返回结果...recordset是空的
      

  6.   

    判断过  recordset不为空
    m_pRecordset->adoBOF ,m_pRecordset->adoBOF都等于0
      

  7.   


    调试代码:
        CString strSql;
        CString strTmp;
        CADOAccess db;    strSql = "SELECT SYSDATETIME()";
        db.ConnectSQLServer("MyDB", "MyTstDB", "sa", "88888888");
        db.ExecuteScalar(strSql, strTmp);
        Msg.Company, Msg.Msg, Msg.MsgCount",strTmp);
        db.CloseConnection();    MessageBox(strTmp);//对象构造器:
    CADOAccess::CADOAccess()
    {
        try
        {
            CoInitialize(NULL);
            m_pConnection.CreateInstance("ADODB.Connection");
        }
        catch(...)
        {
            TRACE(_T("Warning: 发生未知错误:"));
        }
    }//数据库连接代码:
    BOOL CADOAccess::ConnectSQLServer(CString strDBSrc, CString strDBName
            , CString strUser, CString strPwd
            , ConnectOptionEnum enumConnectOption)
    {
    CString strConnect ="Provider=SQLOLEDB.1; Data Source=%s;";
        strConnect.Append(" Initial Catalog=%s; User ID=%s; PWD=%s");
        m_strConnection.Format(strConnect, strDBSrc, strDBName, strUser, strPwd);
    return OpenConnection(m_strConnection, enumConnectOption);
    }
    调试信息:
    “DBTest.exe”: 已加载“C:\Program Files\Common Files\System\msadc\msadcer.dll”,未加载任何符号。
    DBTest.exe 中的 0x7c812a6b 处最可能的异常: Microsoft C++ exception: _com_error @ 0x0012f114 。
    Warning: ExecuteScalar 发生异常. 错误信息: Unknown error 0x800A0CC1; 文件: e:\work\telstaff\prog\dbtest\adoaccess.cpp; 行: 333
      

  8.   

    要确保你的SQL语句正确,如果是SQL,请现在查询分析器中验证语句的正确性。
    一般语句正确打开不会出问题的。
      

  9.   

    不知道用try catch的同学不是好同学。
      

  10.   

    SQL语句没问题  在SQL server里面测试过
      

  11.   

    确信SQL能查到结果么?你好像没有对if(m_pRecordset->BOF)做判断,防止查到的为空!