Link to home
Start Free TrialLog in
Avatar of pentomino
pentomino

asked on

Visual C++ ADO database with ODBC

I am looking for the source code to a simple Visual C++ program that reads a table form a database.. (most likely,  ACCESS)  and changes a field.. It hopefully uses ADO..  the program does not use and window.. just runs..  all errors will be written to a file..

Must be as simple as possible

thanks
Avatar of pentomino
pentomino

ASKER

I think a dialog app could be used with the dialog never apperaing..  maybe
Avatar of DanRollins
Here's a simple console-based program to do what you want:

#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 OLE

void main(){

    _RecordsetPtr   spRS;
    _ConnectionPtr  spCON;
    try{
        CREATEiNSTANCE(spCON,Connection);
        spCON->ConnectionString = L"driver={sql server};SERVER=(local);Database=pubs;"
                                  L"UID=sa; PWD=;";
         // alternative
         //spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
         //                     "DBQ=authors.MDB;DefaultDir=C:\\test;";

         // alternative (using ODBC)
         //spCON->ConnectionString = L"DSN=MyData;UID=MyUid;PWD=MyPwd";
             
        spCON->Open( "", "", "", -1 );
        CREATEiNSTANCE(spRS,Recordset)
        spRS->PutRefActiveConnection( spCON );
        spRS->Open("select au_lname, au_fname from authors", vtMissing, adOpenKeyset,
                    adLockBatchOptimistic, -1);
       
        while(spRS->adoEOF == false){
            printf("au_lname = %s  au_fname = %s \n", UC _bstr_t(RsITEM(spRS,0L)),
                                                 UC _bstr_t(RsITEM(spRS,"au_fname")));
            spRS->MoveNext();
        }
        spRS->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

-=-=-==-=-=-=-=-=--=
You will need to change your connection string, of course, so that it opens the database you want to use...

See this link for some details and to get the entire project:

http://support.microsoft.com/support/kb/articles/q220/1/52.asp

This example just displays some stuff.  But it if you can it running, I'll describe how to make changes to the datbase tables.

-- Dan

PS.  Regarding you EE nickname... for some fun, check out:

http://danRollins.com/pentom/top.htm

Hi Dan

Thanks,  this is close (very).  I tried to start with a dialog MFC project, and liked what I created.
I just commented out the calls to display the dialog, but got hung up on how to connect to the database and how to get the records.


I have to use the ODBC DSN  for both Access and SQQL server.
Could you tell me how this needs to be changed for SQR server?

Anyway,  when do the updated records get put back in the table? Don't you have to call update somewhere?

Eagerly awaiting your reply.  I will check out the pentom site.  Thanks

pentomino  (GRJ)

Dan

Yep,  that's where I got my nickname from.  you are the first person to actually mention something about my nickname  after over two years using it on several different sites and message boards.

I also like somacube and tangrams.

Regards
Gary
have u got the answer or i'll give u the code ?
venkataramanan

Give me the code if you can.. I would appreciate it.

Thanks
Sorry.  I didn't get enough time to respond u immediately.  I have the code developed upto my knowledge.  You please give me ur email id and i'll send it immediately.

Bye
venkataramanan

send to:  grj@ync.net


Thanks
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi pentomino,
Do you have any additional questions?  Do any comments need clarification?

-- Dan