DanRollins gave me this nice, console-based, piece of code (see below) that basically returns values taken from a database
it basically opens up a connection to the database using ADO and populates a recordset using a select sql statement
questions:
1. how do i modify this to do inserts or updates (sql statements) without opening up a recordset?
i guess i'm looking for the c++ sytax of the following vb code
connection.Execute "insert into table (...) values (...)", , adCmdText Or adExecuteNoRecords
i know i can just replace the select statement with an insert statement and not loop thru the recordset but i'd like to not open up a recordset while executing an insert statement, if that's possible
2. what does "vtMissing" and " -1" mean in
spRS->Open("select au_lname, au_fname from authors", vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);
any of you know where i can grab a nice documentation of all these
3. the code works fine but why do i get a linking error that reads:
LINK : warning LNK4098: defaultlib "LIBCMT" conflicts with use of other libs; use /NODEFAULTLIB:library
how do i eliminate this error?
4. i'm also looking for the c++ equivalent of the following vb code
Set cm = New ADODB.Command
cm.CommandText = "TrafficOnlyCheck"
cm.CommandType = adCmdStoredProc
cm.Parameters.Append cm.CreateParameter("@Packa
geId", adInteger, adParamInput, , CLng(strPackageId))
cm.Parameters.Append cm.CreateParameter("@CanTr
affic", adInteger, adParamOutput)
Set cm.ActiveConnection = connection
cm.Execute
blnCanTraffic = cm.Parameters("@CanTraffic
")
Set cm = Nothing
thanks,
f
DanRollins code:
#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=;";
spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
L"DBQ=authors.MDB;DefaultD
ir=C:\\tes
t;";
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
Start Free Trial