Click here to Skip to main content
15,124,187 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>
   
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
adityarao31 30-Mar-12 3:32am
   
Actually i tried the code in MFC but got many errors
adityarao31 30-Mar-12 3:40am
   
error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(173): error C2065: '_ConnectionPtr' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(173): error C2146: syntax error : missing ';' before identifier 'pConnect'
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(173): error C3861: 'pConnect': identifier not found
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(174): error C2065: 'pConnect' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(174): error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(174): error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(174): error C2065: 'adConnectUnspecified' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(178): error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(178): error C2065: '_RecordsetPtr' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(178): error C2146: syntax error : missing ';' before identifier 'pRecSet'
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(178): error C3861: 'pRecSet': identifier not found
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(179): error C2065: 'pRecSet' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(179): error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(179): error C2065: 'pConnect' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(179): error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(179): error C2065: 'adOpenUnspecified' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(180): error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(180): error C2065: 'adLockUnspecified' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(180): error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(180): error C2065: 'adCmdText' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(183): error C2653: 'ADODB' : is not a class or namespace name
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(183): error C2065: 'Fields' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(183): error C2065: 'pFields' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(184): error C2065: 'pRecSet' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(184): error C2227: left of '->get_Fields' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(184): error C2065: 'pFields' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(186): error C2065: 'pRecSet' : undeclared identifier
1>c:\vcprograms\other trials\database2\database2\database2dlg.cpp(186): error C2227: left of '->AdoNSEOF' must point to class/struct/union/generic type
Resmi Anna 30-Mar-12 4:25am
   
#import is done as in the code?
please see all includes at the top
adityarao31 31-Mar-12 3:29am
   
Yes it worked correctly both in VC 6.0 and VC++ 2010,Thanks a lot.Thank you very much.

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