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
Must be as simple as possible
thanks
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.d ll" 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)]->Val ue
#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,Conne ction);
spCON->ConnectionString = L"driver={sql server};SERVER=(local);Dat abase=pubs ;"
L"UID=sa; PWD=;";
// alternative
//spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
// "DBQ=authors.MDB;DefaultDi r=C:\\test ;";
// alternative (using ODBC)
//spCON->ConnectionString = L"DSN=MyData;UID=MyUid;PWD =MyPwd";
spCON->Open( "", "", "", -1 );
CREATEiNSTANCE(spRS,Record set)
spRS->PutRefActiveConnecti on( 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_fn ame")));
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
#include <stdio.h>
#include <afxdisp.h>
#import "c:\program files\common files\system\ado\msado15.d
#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_
#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,Conne
spCON->ConnectionString = L"driver={sql server};SERVER=(local);Dat
L"UID=sa; PWD=;";
// alternative
//spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
// "DBQ=authors.MDB;DefaultDi
// alternative (using ODBC)
//spCON->ConnectionString = L"DSN=MyData;UID=MyUid;PWD
spCON->Open( "", "", "", -1 );
CREATEiNSTANCE(spRS,Record
spRS->PutRefActiveConnecti
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_fn
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
}
}
#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
ASKER
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)
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)
ASKER
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
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 ?
ASKER
venkataramanan
Give me the code if you can.. I would appreciate it.
Thanks
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
Bye
ASKER
venkataramanan
send to: grj@ync.net
Thanks
send to: grj@ync.net
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi pentomino,
Do you have any additional questions? Do any comments need clarification?
-- Dan
Do you have any additional questions? Do any comments need clarification?
-- Dan
ASKER