Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created user DSN using odbcad32.exe. As it seems only connection with
office 2007 32 bit available drivers.But when for DataSource( while adding CRecordSet) class ,I mention this DSN,VC++ 2010 professional edition(original)
crashes without any warning,Please help me on this issue?
Is there any another simple way of database connection for VC++?(I found ADO is bit difficult for VC++)
Posted
Comments
Jochen Arndt 29-Mar-12 8:22am    
There are only 32 bit ODBC and ADO/JET drivers. If you want to use 64 bit, you must use ADO/ACE. If you want help on fixing your errors, please provide some more information if your app crashes (I don't think that VS crashes): The source code lines of connecting to the driver and the lines where the crash occurs. You should also use a debug build and check the trace window output (the ODBC drivers generate a lot of useful output).

1 solution

It is easy connecting using ADO. see the below code. It displays all the fields in a table called Annount from the access database MyDB

C++
#include <iostream>
#include <tchar.h>
#import <C:\\Program Files\\Common Files\\System\\ado\\msado15.dll>  \
    rename( "EOF", "AdoNSEOF" )
using namespace std;

// Connection string for ADO 
_bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\MyTrainingDB\\MyDB.accdb;";

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hResult = CoInitialize( 0 );
    if( FAILED( hResult ))
    {
        return 0;
    }
    try
    {
        ADODB::_ConnectionPtr pConnect("ADODB.Connection");
        hResult = pConnect->Open( bstrConnect, "admin", "", ADODB::adConnectUnspecified );
        if (SUCCEEDED(hResult))
        {
            _bstr_t query = "SELECT * FROM Account;";
            ADODB::_RecordsetPtr pRecSet( "ADODB.Recordset" );
            hResult = pRecSet->Open( query, _variant_t((IDispatch *) pConnect, true), ADODB::adOpenUnspecified,
                                     ADODB::adLockUnspecified, ADODB::adCmdText);
            if( SUCCEEDED( hResult ))
            {
                ADODB::Fields* pFields = NULL;
                hResult = pRecSet->get_Fields( &pFields );
                
                while( !pRecSet->AdoNSEOF )
                {
                    for( long lIndex=0; lIndex < pFields->GetCount(); lIndex++ )
                    {
                        cout<<" | ";
						cout << _bstr_t(pFields->GetItem(lIndex)->GetValue());
                    }
					cout<< "\n----------------------------------------------------------------------------\n";
                    pRecSet->MoveNext();
                }
            }
            pRecSet->Close();
            pConnect->Close();
        }
    }
    catch( _com_error& e )
    {
        // Handle Exception
    }

    // Release COM
    CoUninitialize();
	getchar();
    return 0;
}</tchar.h></iostream>
 
Share this answer
 
v2
Comments
adityarao31 29-Mar-12 8:52am    
Thank you very much for your reply I will use ADO
adityarao31 30-Mar-12 3:17am    
I have gone through your code it seems it is for turbo C++ or for C++ CLI but I am an MFC programmer ,will you please tell me How it could be done in MFC
Resmi Anna 30-Mar-12 3:21am    
you can use this code in MFC.Did you try?What issue u had?
Resmi Anna 30-Mar-12 3:19am    
It is so sad down voting without any specific reason :(
adityarao31 30-Mar-12 3:31am    
Actually I tried to vote but after pressing first star i was unable to press next stars

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900