http://svkhere.blogspot.com/ \\narmada\melodies immadishero@yahoo.

com //mahesh chandh you can access databases using mfc dao classes without help of mfc appwizard and classwizard. this article explains how to access data using cdaodatabase and cdaorecordset classes at runtime. here are few simple steps: 1. connect to the database the cdaodatabase class is used to connect to a database. cdaodatabase::open method creates a connection to the database. first parameter of cdatabase::open() is an access database name, say "c:\mcb.mdb". cdaodatabase m_db; m_db.open( "d:\\mcb.krz", false, false, "" ) ; 2. create instance of cdaorecordset and attached to the cdaodatabase cdaorecordset class is used to create a connection to a table in the database. i am accessing data from 'captions' table. cdaorecordset* pcaptionset; pcaptionset = new cdaorecordset(mydb ); 3. open recordset open the recordset if it is not open with dynaset type and captions table. if ( !pcaptionset->isopen() ) pcaptionset->open( dbopendynaset , "select * from captions", 0 ); 4. get data now you can use getfieldvalue of cdaorecordset to get value of a column. if you know the column name, you can direct pass column name as a parameter in getfieldvalue else you can pass field index. both returns colevariant. colevariant var; if ( ! (pcaptionset->isbof() && pcaptionset->iseof()) ) { pcaptionset->movefirst(); } while ( ! pcaptionset->iseof() ) { // var = pcaptionset->getfieldvalue("fieldname"); pcaptionset->getfieldvalue( 5, var ); pcaptionset->movenext(); } 5. update data you can add data by using setfieldvalue with edit and update member functions of cdaorecordset. it takes two parameters, first is field index and second is the value you want to add in colevariant format.

pcaptionset->edit(); pcaptionset->setfieldvalue(6,tempole); pcaptionset->update(); you can read/write data for all fields in a loop. m_nfields of cdaorecordset contains number of total fields in the table. nfields = pcaptionset->m_nfields; pcaptionset->edit(); for( int nfieldindex = 1; nfieldindex < nfields; nfieldindex++ ) { pcaptionset.getfieldvalue( nfieldindex, var); pcaptionset.setfieldvalue( nfieldindex, var); } m_rstotable.update(); 6. close recordset connection cdaodatabase::close() and cdaorecordset::close() close these connections. recset.close(); m_db.close(); -------------------------------------------------------------------------

cdaodatabase --------------------#include <afxdao.h> cdaodatabase cdb; colevariant cole; cdb.open("",false,false,null); cdaorecordset cset(&cdb); cset.open(""); cset.getfieldvalue(1,cole); cole.vt cset.close(); cdb.close();

------------------------------------inorder to connect with database create a new recordset class for the dialog box and in ur corresponding dialog box function { myrecordset set; //myrecorset is the recordset class set.open(); cstring str=set.m_pname; setdlgitemtext(id,str); updatedata(false);

} include need files //---------------------//leap year //================== public function isleapyear(iyear as integer) '-- check for leap year if (iyear mod 4 = 0) and _ ((iyear mod 100 <> 0) or (iyear mod 400 = 0)) then isleapyear = true else isleapyear = false end if end function //-----------------//random number static unsigned long int next = 1; int rand(void) // rand_max assumed to be 32767 { next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768; } //-----------------------------//---------------------------------------//sprintf---------char *temp; idstart = atol(str); sprintf(temp,"%d",idstart); messagebox(temp,"testing"); //------------------------------------------//data validation //-----------------------------------------cedit * edit; cstring str; cstring message; long ids; int range1; int range2; edit = (cedit *) getdlgitem(idc_edit1); edit->getwindowtext(str); if(str.isempty()) message += "enter starting consumer id \n"; edit = (cedit *) getdlgitem(idc_edit2); edit->getwindowtext(str); if(str.isempty())

message += "enter ending consumer id \n"; edit = (cedit *) getdlgitem(idc_edit3); edit->getwindowtext(str); if(str.isempty()) message += "enter starting serial number \n"; edit = (cedit *) getdlgitem(idc_edit4); edit->getwindowtext(str); if(str.isempty()) message += "enter meter constant range 1 \n"; edit = (cedit *) getdlgitem(idc_edit5); edit->getwindowtext(str); if(str.isempty()) message += "enter meter constant range 1 \n"; if(message.isempty()) { messagebox("data for meter details generated successfully","meter details data"); } else messagebox(message,"meter details data"); ---------------------------------------------------//new and delete ----------------------------------------------------void ctableciddetailsview::onaddnew() { cedit *edit; cstring str; int pos_or_digits=0; int flag = 0; int valid_record = 0; cstring message; //total_records = m_pset->getrecordcount(); if( m_pset->iseof() ) messagebox("no record","about new entry"); else{ m_pset->movefirst(); edit = (cedit *) getdlgitem(idc_edit1); edit->getwindowtext(str); while(true) { if(str == m_pset->m_szcompanyname) { flag = 1;

break; } m_pset->movenext(); if( m_pset->iseof() ) break; }//while ends }//else if(flag == 1) { messagebox("already in database","about new entry"); } else{ //record addition_code edit = (cedit *) getdlgitem(idc_edit1); edit->getwindowtext(str); m_pset->m_szcompanyname = str; if(str=="") { valid_record = 1; message = "enter company name\n"; } edit = (cedit *) getdlgitem(idc_edit2); edit->getwindowtext(str); m_pset->m_szlevelname = str; if(str=="") { valid_record = 1; message += "enter level name\n"; } edit = (cedit *) getdlgitem(idc_edit3); edit->getwindowtext(str); pos_or_digits = atoi(str); m_pset->m_iposition = pos_or_digits; if(str=="") { valid_record = 1; message += "enter position\n"; } edit = (cedit *) getdlgitem(idc_edit4); edit->getwindowtext(str); pos_or_digits = atoi(str); m_pset->m_idigits = pos_or_digits; if(str=="") {

valid_record = 1; message += "enter digits\n"; } updatedata(false); //record save if(valid_record==0) { m_pset->addnew(); updatedata(true); if (m_pset->canupdate()) m_pset->update(); m_pset->requery(); updatedata(false); messagebox("new record created successfully !!!","new record"); }//if ends else messagebox(message,"new record"); }//else ends }//function ends //---------------------------//delete //--------------------------void ctableciddetailsview::ondelete() { //record delete m_pset->delete(); m_pset->moveprev(); m_pset->requery(); updatedata(false); messagebox("record deleted successfully !!!","delete record"); } ///////////////////////////////////////////////////////////// //========================================================== on_generate() //==================================================== void cgeneratemeterdetailsview::ongenerate() { cedit * edit; cstring str,id,serial,str1; cstring message; long howmany; int digitsinhowmany; int range1; int range2; int meterconstant; edit = (cedit *) getdlgitem(idc_edit1); edit->getwindowtext(str);

str1+=str; str1+="\n"; if(str.isempty()) message += "enter starting consumer id \n"; else id = str; edit = (cedit *) getdlgitem(idc_edit2); edit->getwindowtext(str); str1+=str; str1+="\n"; if(str.isempty()) message += "enter how many consumer id's you want \n"; else howmany = atol(str); digitsinhowmany = str.getlength(); edit = (cedit *) getdlgitem(idc_edit3); edit->getwindowtext(str); str1+=str; str1+="\n"; if(str.isempty()) message += "enter starting serial number \n"; else serial = str; edit = (cedit *) getdlgitem(idc_edit4); edit->getwindowtext(str); str1+=str; str1+="\n"; if(str.isempty()) message += "enter meter constant range 1 \n"; else range1 = atoi(str); edit = (cedit *) getdlgitem(idc_edit5); edit->getwindowtext(str); str1+=str; str1+="\n"; if(str.isempty()) message += "enter meter constant range 2 \n"; else range2 = atoi(str); /* //sprintf //sprintf---------char *temp;

sprintf(temp,"%d",digitsinhowmany); messagebox(temp,"testing"); //---------------*/ if(message.isempty()) // if all the data is valid and present { //===================================================== // enter the first id and other details to the database //===================================================== m_pset->m_iserialno = serial; m_pset->m_iconsumerid = id; meterconstant = rand(range2); while(meterconstant<range1) //meter constant should be b/w range1 and range2 { meterconstant = rand(range2); } m_pset->m_fmeterconstant = meterconstant; updatedata(false); //record save m_pset->addnew(); updatedata(true); if (m_pset->canupdate()) m_pset->update(); m_pset->requery(); updatedata(false); //first one is saved. //========================================================== //generate and save the remaining id's.ie upto howmany id's. //========================================================== int i = 1; while( i < howmany ) { id = generateid(id,digitsinhowmany); serial = generateserial(serial,digitsinhowmany); addnewrecord(id,serial,range1,range2); i++; } messagebox("data for meter details generated successfully","meter details data"); } else messagebox(message,"meter details data"); } cstring cgeneratemeterdetailsview::generateid(cstring id, int digitsinhowmany) { // extract the cstring consumer id into two parts and increment one // to the right part. after that concatinate both strings cstring part1,part2;

int int int int pos int

part1_len,part2_len; i =0; idlength = id.getlength(); pos = idlength - digitsinhowmany; // represents where to split = pos - 1; // array index starts from zero. new_id;

while ( i < pos ) { part1 += id.getat(i); i++; } part1_len = part1.getlength(); while ( pos < idlength ) { part2 += id.getat(pos); pos++; } new_id = atoi(part2); new_id = new_id + 1; part2 = ""; part2.format("%d",new_id); part2_len = part2.getlength(); // in case of zeros in the middle we will get incorrect serial numbers. // for example if 1000 is the starting serial number then we will get // 11,12,13 etc as the remaining serial numbers. to avoid this add corresponding // zeros in the middle // if( ( part1_len + part2_len ) < idlength ) { cstring temp; part2 = ""; int noofzeros = idlength - ( part1_len + part2_len ); for ( i = 0; i< noofzeros; i++ ) temp += "0"; part2.format("%d",new_id); temp += part2; part2 =temp; } id = part1 + part2; return id; } cstring cgeneratemeterdetailsview::generateserial(cstring serial ,int digitsinhowmany) { // extract the cstring serial number into two parts and increment one

// to the right part. after that concatinate both strings cstring part1,part2; int part1_len,part2_len; int i =0; int seriallength = serial.getlength(); int pos = seriallength - digitsinhowmany; // represents where to split pos = pos - 1; // array index starts from zero. int new_serial; while ( i < pos ) { part1 += serial.getat(i); i++; } part1_len = part1.getlength(); while ( pos < seriallength ) { part2 += serial.getat(pos); pos++; } new_serial = atoi(part2); new_serial = new_serial + 1; part2 = ""; part2.format("%d",new_serial); part2_len = part2.getlength(); // in case of zeros in the middle we will get incorrect serial numbers. // for example if 1000 is the starting serial number then we will get // 11,12,13 etc as the remaining serial numbers. to avoid this add corresponding // zeros in the middle // if( ( part1_len + part2_len ) < seriallength ) { cstring temp; part2 = ""; int noofzeros = seriallength - ( part1_len + part2_len ); for ( i = 0; i< noofzeros; i++ ) temp += "0"; part2.format("%d",new_serial); temp += part2; part2 =temp; } serial = part1 + part2; return serial; } int cgeneratemeterdetailsview::rand(int max) { next = next * 110351524 + 12345; return (unsigned int)(next/65536) % max;

} cgeneratemeterdetailsview::addnewrecord(cstring id, cstring serial, int range1, int range2) { //===================================================== // enter the consumer id and other details to the database //===================================================== int meterconstant; m_pset->m_iserialno = serial; m_pset->m_iconsumerid = id; meterconstant = rand(range2); while(meterconstant<range1) //meter constant should be b/w range1 and range2 { meterconstant = rand(range2); } m_pset->m_fmeterconstant = meterconstant; updatedata(false); //record save m_pset->addnew(); updatedata(true); if (m_pset->canupdate()) m_pset->update(); m_pset->requery(); updatedata(false); }

Sign up to vote on this title
UsefulNot useful