⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adocore.cpp

📁 vc ADO 连接数据库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ADOCORE:  Implementation functions for demonstrating ADO via 
//           Visual C++ and OLE SDK.
//
// Non-ADO Native and Native ADO Error Handling is embedded
// within each exception handling function.  AdoErrorEx handles
// processing errors raised to the Ado Errors Collection

#include "stdafx.h"

// Include ADO Interface/Class Descriptions (ADO 1.5 Version)
#include <initguid.h>
#include "adoid.h"
#include "adoint.h"

#include "hlb.h"
#include "log.h"

#include "AdoUtils.h"

#include "VCADO.h"
#include "inputbox.h"

//-----------------------
// Dialog Event Handlers
//-----------------------

    void OpenAccessDatabase( CHorzListBox &List1 )
    {   
        ADOConnection*  Conn1   = NULL;
        ADOCommand*     Cmd1    = NULL;
        ADOParameters*  Params1 = NULL;
        ADOParameter*   Param1  = NULL;
        ADORecordset*   Rs1     = NULL;
        ADOFields*      Flds1   = NULL;
        ADOField*       Fld1    = NULL;
        ADOField*       Fld2    = NULL;

        CString         strTmp  = "";
        VARIANT_BOOL    bEOF    = VARIANT_FALSE;

        HRESULT         hr      = S_OK;

        COleVariant     v1;
        COleVariant     v2;

        long            nCount;

        // Trap any error/exception
        try
        {
            //------------------------
            // Open Connection Object
            //------------------------

            // Warm & Fuzzy for user
            List1.ResetContent();
            List1.AddString( "Opening Access Database ADODEMO.MDB..." );
            List1.AddString( "\t...Assumes ADODEMO.MDB is in the same directory" );
            List1.AddString( "\t...With Error Handling Using Connection Object"  );

            // Create Connection Object (1.5 Version )
            hr = CoCreateInstance( CLSID_CADOConnection, 
                                   NULL, 
                                   CLSCTX_INPROC_SERVER, 
                                   IID_IADOConnection, 
                                   (LPVOID *) &Conn1 );
            if( SUCCEEDED( hr ) ) hr = Conn1->put_ConnectionString( bstrAccessConnect );
            if( SUCCEEDED( hr ) ) hr = Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );

            //-----------------------------------
            // Open Parameterized Command Object
            //-----------------------------------

            if( SUCCEEDED( hr ) ) List1.AddString( "\t...Parameterized Command Object" );

            // Create Command Object
            if( SUCCEEDED( hr ) ) hr = CoCreateInstance( CLSID_CADOCommand, 
                                                         NULL, 
                                                         CLSCTX_INPROC_SERVER, 
                                                         IID_IADOCommand, 
                                                         (LPVOID *) &Cmd1 );

            if( SUCCEEDED( hr ) ) hr = Cmd1->putref_ActiveConnection( Conn1 );
            if( SUCCEEDED( hr ) ) hr = Cmd1->put_CommandText( bstrOpenAccessWithParam ); 

            // Create Parameter Object
            if( SUCCEEDED( hr ) ) hr = Cmd1->CreateParameter( bstrEmpty,
                                                              adInteger,
                                                              adParamInput,
                                                              -1,
                                                              COleVariant( (long) 5 ),
                                                              &Param1 );
            if( SUCCEEDED( hr ) ) hr = Param1->put_Value( COleVariant( (long) 5 ) );
            if( SUCCEEDED( hr ) ) hr = Cmd1->get_Parameters( &Params1 );
            if( SUCCEEDED( hr ) ) hr = Params1->Append( Param1 );

            // Open Recordset Object
            if( SUCCEEDED( hr ) ) hr = Cmd1->Execute( &vtEmpty, &vtEmpty2, adCmdText, &Rs1 );
   
            //----------------------------------------
            // Manipulate Recordset/Fields Collection
            //----------------------------------------

            if( SUCCEEDED( hr ) ) List1.AddString( "\t...Forward-Only Recordset" );
            if( SUCCEEDED( hr ) ) List1.AddString( "Dumping contents of each record..." );


            // While not on EOF...
            if( SUCCEEDED( hr ) ) hr = Rs1->get_EOF( &bEOF );
            while ( ( SUCCEEDED( hr ) ) && ( bEOF == VARIANT_FALSE ) )
            {
                // Get Fields 1 & 2
                if( SUCCEEDED ( hr ) ) hr = Rs1->get_Fields( & Flds1 );
                if( SUCCEEDED ( hr ) ) hr = Flds1->get_Item( COleVariant((long) 0 ), &Fld1 );
                if( SUCCEEDED ( hr ) ) hr = Flds1->get_Item( COleVariant((long) 1 ), &Fld2 );
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_Value( &v1 );
                if( SUCCEEDED ( hr ) ) hr = Fld2->get_Value( &v2 );

                // Display value in each field
                if( SUCCEEDED( hr ) ) strTmp.Format( "\t %s | %s ", CrackStrVariant( v1 ), CrackStrVariant( v2 ) );
                if( SUCCEEDED( hr ) ) List1.AddString( strTmp );

                // Move to next record
                if( SUCCEEDED( hr ) ) hr = Rs1->MoveNext();
                if( SUCCEEDED( hr ) ) hr = Rs1->get_EOF( &bEOF );
            }

            // Warm & Fuzzy for user
            if( SUCCEEDED( hr ) ) List1.AddString( "Dumping Properties of each column in the recordset..." );

            // Reset recordset (1.5 Version)
            if( SUCCEEDED( hr ) ) hr = Rs1->Requery(-1 );
            if( SUCCEEDED( hr ) ) hr = Flds1->Refresh();
            if( SUCCEEDED( hr ) ) hr = Flds1->get_Count( &nCount );

            // Iterate through columns
            for ( int i=0; SUCCEEDED( hr ) && (i < nCount); i++ )
            {
                long            nTmp;
                BSTR            bstrTmp = NULL;
                VARIANT         v;
                DataTypeEnum    e;

                if( SUCCEEDED ( hr ) ) hr = Flds1->get_Item( COleVariant((long) i ), &Fld1 );

                // Display Field #
                strTmp.Format("\tADOField* #%d", i + 1 );
                List1.AddString( strTmp );

                // Display Actual Size
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_ActualSize( &nTmp );
                if( SUCCEEDED ( hr ) ) 
                {
                    strTmp.Format( "\t\t...Actual Size  = %ld", nTmp ); 
                    List1.AddString( strTmp );
                }

                // Display Attributes
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_Attributes( &nTmp );
                if( SUCCEEDED ( hr ) ) 
                {
                    strTmp.Format( "\t\t...Attributes   = %ld", nTmp ); 
                    List1.AddString( strTmp );
                }

                // Display Defined Size
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_DefinedSize( &nTmp );
                if( SUCCEEDED ( hr ) ) 
                {
                    strTmp.Format( "\t\t...Defined Size = %ld", nTmp ); 
                    List1.AddString( strTmp );
                }

                // Display Name
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_Name( &bstrTmp );
                if( SUCCEEDED ( hr ) ) 
                {
                    strTmp.Format( "\t\t...Name         = %s",  CString( (LPCWSTR) bstrTmp ));
                    List1.AddString( strTmp );
                    SysFreeString(bstrTmp);
                }

                // Display Type
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_Type( &e );
                if( SUCCEEDED ( hr ) ) 
                {
                    strTmp.Format( "\t\t...Type         = %s",  GetType( (int) e ) );
                    List1.AddString( strTmp );
                }

                // Display Value
                if( SUCCEEDED ( hr ) ) hr = Fld1->get_Value( &v );
                if( SUCCEEDED ( hr ) ) 
                {
                    if( e == adGUID )
                    {
                        List1.AddString( "\t\t...Value      = <GUID>" );
                    }
                    else
                    {
                        strTmp.Format  ( "\t\t...Value      = %s",  CrackStrVariant( v ));
                        List1.AddString( strTmp );
                    }
                }
            }

            // Successful Shutdown
            if( SUCCEEDED( hr ) ) List1.AddString( "*** Success! ***" );
        }
        // Catch Blocks
        catch( CException *e )
        {
            MfcErrorEx( e, List1, Conn1 );
        }
        catch( SEH_Exception &e )
        {
            Win32ErrorEx( e, List1, Conn1 );
        }
        catch(...)
        {
            UnknownErrorEx( List1, Conn1 );
        }

        // Better check that HRESULT!
        if( FAILED( hr ) )
        {
            List1.AddString("*** Failed HRESULT ***" );
            strTmp = LogCrackHR( hr );
            List1.AddString( strTmp );

            // Dump Errors Collection, since if no exception was raised
            // This is only way we can get at it!
            if( Conn1 != NULL )
            {
                AdoErrorEx( List1, Conn1 );
            }
        }

        //----------------------------------
        // Miscellaneous (graceful) Cleanup
        // with quiet error trapping
        //----------------------------------

        // Close any open objects
        if( Rs1                != NULL ) LOGQ( Rs1->Close();   )
        if( Conn1              != NULL ) LOGQ( Conn1->Close(); )

        // Force Release of objects;
        if( Fld1    != NULL ) LOGQ( Fld1->Release();    )
        if( Fld2    != NULL ) LOGQ( Fld2->Release();    )
        if( Flds1   != NULL ) LOGQ( Flds1->Release();   )
        if( Rs1     != NULL ) LOGQ( Rs1->Release();     )
        if( Param1  != NULL ) LOGQ( Param1->Release();  )
        if( Params1 != NULL ) LOGQ( Params1->Release(); )
        if( Cmd1    != NULL ) LOGQ( Cmd1->Release();    )
        if( Conn1   != NULL ) LOGQ( Conn1->Release();   )
    }

    void ProviderProperties( CHorzListBox &List1 )
    {  
        ADOConnection*  Conn1   = NULL;
        ADOCommand*     Cmd1    = NULL;
        ADORecordset*   Rs1     = NULL;
        ADOProperties*  Props1 = NULL;
        ADOFields*      Flds1   = NULL;
        ADOField*       Fld1    = NULL;

        CString         strTmp  = "";;

        HRESULT         hr      = S_OK;

        // Trap any error/exception
        try
        {
            //-------------------
            // Connection Object
            //-------------------

            // Warm & Fuzzy for user
            List1.ResetContent();

            // Create Connection Object (1.5 Version )
            hr = CoCreateInstance( CLSID_CADOConnection, 
                                   NULL, 
                                   CLSCTX_INPROC_SERVER, 
                                   IID_IADOConnection, 
                                   (LPVOID *) &Conn1 );
            if( SUCCEEDED( hr ) ) hr = Conn1->put_ConnectionString( bstrAccessConnect );
            if( SUCCEEDED( hr ) ) hr = Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );

            // Dump connection properties
            if( SUCCEEDED( hr ) ) List1.AddString( "*** *** *** *** *** Dumping contents of the ADOProperties* Collection for the Connection object *** *** *** *** ***" );
            if( SUCCEEDED( hr ) ) hr = Conn1->get_Properties( &Props1 );

            if( SUCCEEDED( hr ) )
            {
                DumpProperty( List1, Conn1, Props1 );
                Props1->Release();
                Props1 = NULL;
            }

            //----------------
            // Command Object
            //----------------

            // Create Command Object
            if( SUCCEEDED( hr ) ) hr = CoCreateInstance( CLSID_CADOCommand, 
                                                         NULL, 
                                                         CLSCTX_INPROC_SERVER, 
                                                         IID_IADOCommand, 
                                                         (LPVOID *) &Cmd1 );
            if( SUCCEEDED( hr ) ) hr = Cmd1->putref_ActiveConnection( Conn1 );

            // Dump Command Properties
            if( SUCCEEDED( hr ) ) List1.AddString( "*** *** *** *** *** Dumping contents of the ADOProperties* Collection for the Command object *** *** *** *** ***" );
            if( SUCCEEDED( hr ) ) hr = Cmd1->get_Properties( &Props1 );
            if( SUCCEEDED( hr ) )
            {
                DumpProperty( List1, Conn1, Props1 );
                Props1->Release();
                Props1 = NULL;
            }

            //------------------
            // Recordset Object
            //------------------

            // Create Recordset Object
            if( SUCCEEDED( hr ) ) hr = Cmd1->put_CommandText( bstrOpenAccess ); 
            if( SUCCEEDED( hr ) ) hr = Cmd1->Execute( &vtEmpty, &vtEmpty2, adCmdText, &Rs1 );

            // Dump Recordset Properties
            if( SUCCEEDED( hr ) ) List1.AddString( "*** *** *** *** *** Dumping contents of the ADOProperties* Collection for the Recordset object *** *** *** *** ***" );
            if( SUCCEEDED( hr ) ) hr = Rs1->get_Properties( &Props1 );

            if( SUCCEEDED( hr ) )
            {
                DumpProperty( List1, Conn1, Props1 );
                Props1->Release();
                Props1 = NULL;
            }

            //--------------
            // Field Object
            //--------------

            // Create Field Object (if necessary)
            if( SUCCEEDED( hr ) ) hr = Rs1->get_Fields( &Flds1 );
            if( SUCCEEDED( hr ) ) hr = Flds1->get_Item( COleVariant((long) 0 ), &Fld1 );
            if( SUCCEEDED( hr ) ) hr = Fld1->get_Properties( &Props1 );

            // Dump Field Properties
            if( SUCCEEDED( hr ) ) List1.AddString( "*** *** *** *** *** Dumping contents of the ADOProperties* Collection for the ADOField* object *** *** *** *** ***" );
            if( SUCCEEDED( hr ) )
            {
                DumpProperty( List1, Conn1, Props1 );
                Props1->Release();
                Props1 = NULL;
            }

            // Successful Shutdown
            if( SUCCEEDED( hr ) ) List1.AddString( "*** Success! ***" );

        }
        // Catch Blocks
        catch( CException *e )
        {
            MfcErrorEx( e, List1, Conn1 );
        }
        catch( SEH_Exception &e )
        {
            Win32ErrorEx( e, List1, Conn1 );
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -