我编辑了一个main程序,想逐步将数据库操作加上去,当我将#include“sqlext.h”加入main程序中后,编译出现很多错误,其中第一个是"typedef Hwnd SQLHWND"语句,错误是“error C2061: syntax error : identifier 'SQLHWND'”,不知道究竟是什么原因?

解决方案 »

  1.   

    不认识sqlhwnd 
    typedef newtypename oldtype
      

  2.   

    #include <stdio.h>
    //#include <afxdisp.h>#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
                                      if (FAILED(_hr)) _com_issue_error(_hr); }#define RsITEM(rs,x) rs->Fields->Item[_variant_t(x)]->Value
    #define UC (char *)
    struct InitOle {
        InitOle()  { ::CoInitialize(NULL); }
        ~InitOle() { ::CoUninitialize();   }
    } _init_InitOle_;       // Global Instance to force load/unload of OLEvoid main(){    _RecordsetPtr   spRS;
        _RecordsetPtr   spRSCopy;
        _ConnectionPtr  spCON;
        try{
            CREATEiNSTANCE(spCON,Connection);
            spCON->ConnectionString = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Index.mdb;"
                                      L"Persist Security Info=False"; 
            spCON->Open( "", "", "", -1 );
            CREATEiNSTANCE(spRS,Recordset) 
            spRS->PutRefActiveConnection( spCON );
            spRS->Open("select STATIONID,STATNAME from Station", vtMissing, adOpenKeyset,
                        adLockBatchOptimistic, adCmdUnspecified);        CREATEiNSTANCE(spRSCopy,Recordset) 
            spRSCopy->PutRefActiveConnection( spCON );
            spRSCopy->Open("select STATIONID,STATNAME from Copy", vtMissing, adOpenKeyset,
                        adLockOptimistic, adCmdUnspecified);
            
            while(spRS->adoEOF == false){
                printf("StationID = %s  StationName = %s \n", UC _bstr_t(RsITEM(spRS,"STATIONID")),
                                                     UC _bstr_t(RsITEM(spRS,"STATNAME")));
                spRSCopy->AddNew();
    RsITEM(spRSCopy,"STATIONID") = _bstr_t(UC _bstr_t(RsITEM(spRS,"STATIONID")));
    RsITEM(spRSCopy,"STATNAME") = _bstr_t(UC _bstr_t(RsITEM(spRS,"STATNAME"))); spRSCopy->Update();
    spRS->MoveNext();
            }
            spRS->Close();
    spRSCopy->Close();
            spCON->Close();
            
        }
        catch( _com_error &e){
            _bstr_t bstrSource(e.Source());
            _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
                + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
                + _bstr_t(e.Description());
            
            MessageBox(0,bs,bstrSource, MB_OK);
        }           
    }
    #undef UC
      

  3.   

    Compiler Error C2061
    syntax error : identifier 'identifier'The compiler found identifier where it was not expected. This error can be caused by enclosing an initializer in parentheses. The error can be avoided by enclosing the declarator in parentheses or making it a typedef. The following example causes this error and shows two fixes:
    翻译出上面这段文字就知道原因了。
      

  4.   

    谢谢众位帮忙,还有一事请教,我将该历程修改了一下,如下:
    #include <stdio.h>
    //#include <afxdisp.h>#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
                                      if (FAILED(_hr)) _com_issue_error(_hr); }#define RsITEM(rs,x) rs->Fields->Item[_variant_t(x)]->Value
    #define UC (char *)
    struct InitOle {
        InitOle()  { ::CoInitialize(NULL); }
        ~InitOle() { ::CoUninitialize();   }
    } _init_InitOle_;       // Global Instance to force load/unload of OLEvoid main(){    _RecordsetPtr   spRS;
        _RecordsetPtr   spRSCopy;
        _ConnectionPtr  spCON;
        try{
            CREATEiNSTANCE(spCON,Connection);
            spCON->ConnectionString = L"DSN=hgdb;User ID=sa;pwd=compaq;Provider=MSDASQL;"
                                      L"Persist Security Info=False"; 
            spCON->Open( "", "", "", -1 );
            CREATEiNSTANCE(spRS,Recordset) 
            spRS->PutRefActiveConnection( spCON );
            spRS->Open("select * from ta", vtMissing, adOpenKeyset,
                        adLockBatchOptimistic, adCmdUnspecified);
    /*
            CREATEiNSTANCE(spRSCopy,Recordset)
            spRSCopy->PutRefActiveConnection( spCON );
            spRSCopy->Open("select STATIONID,STATNAME from Copy", vtMissing, adOpenKeyset,
                        adLockOptimistic, adCmdUnspecified);
    */
            while(spRS->adoEOF == false){
                printf("StationID = %s  StationName = %s \n", UC _bstr_t(RsITEM(spRS,"a1")),
                                                     UC _bstr_t(RsITEM(spRS,"b")));
    /*
                spRSCopy->AddNew();
    RsITEM(spRSCopy,"STATIONID") = _bstr_t(UC _bstr_t(RsITEM(spRS,"STATIONID")));
    RsITEM(spRSCopy,"STATNAME") = _bstr_t(UC _bstr_t(RsITEM(spRS,"STATNAME"))); spRSCopy->Update();
    */
    spRS->MoveNext();
        catch( _com_error &e){
            _bstr_t bstrSource(e.Source());
    if (e.Error!=0){
    _bstr_t bs =  _bstr_t(" 23Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
    + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
    + _bstr_t(e.Description());
            
    MessageBox(0,bs,bstrSource, MB_OK);
    }
    }
        }
            spRS->Close();
    spRSCopy->Close();
            spCON->Close();
        }
        catch( _com_error &e){
            _bstr_t bstrSource(e.Source());
            _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
                + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
                + _bstr_t(e.Description());
            
            MessageBox(0,bs,bstrSource, MB_OK);
        }           
    }
    #undef UC
    即在每一次读去数据后,判断一下操作数据库是否有误,如有错误则另行处理。但编译时为何报“no try block associated with this catch handler”等很多错误,不知道为什么?
      

  5.   

    将异常处理 写为下面情况,如何?
    catch(—com—error e) //异常处理 { CString Error = e-〉ErrorMessage(); AfxMessageBox(e-〉ErrorMessage()); }