Friday 15 March 2013

database - How to change Clientdataset field datatype at runtime -



database - How to change Clientdataset field datatype at runtime -

for delphi clientdatasets fields have been define @ design time, there way @ runtime alter specific field's datatype ( alter cds.fields[n].datatype) ?

i have legacy delphi 7 programme both sqldataset , clientdataset fields set @ design time (in order override various properties).

these connected 3rd-party sybase sql anywhere 11 database.

recently vendor changed 'description' fields varchar(128) long varchar, of customers. so, code has back upwards both types of fields when query on these 'description' fields.

i hoping set conditional compilation on class field types (then add together fields before opening sql/client dataset), compiler ignores {$ifdef } conditionals in component definition section of class (which, more think it, makes sense)!

there dozens of modules, hundreds of fields affected, insight appreciated.

thanks.

cheers, edb

i have faced problem before, not cds, tadodataset using persistent fields @ design time. think code below help thought of how fix/patch cds datasets.

the thought query relevant table schema; actual fileds info type; , "change" persistent field type un-attaching dataset , adding new matching persistent filed instead:

// tdata class procedure tdata.datamodulecreate(sender: tobject); var i: integer; begin := 0 componentcount - 1 if (components[i] tcustomadodataset) datasetpreparememofields(tdataset(components[i])); end; procedure tdata.datasetpreparememofields(dataset: tdataset); var fld: tfield; i: integer; fldname, compname: string; aowner: tcomponent; begin // here need query actual table schema database // e.g. adoconnection.getfieldnames , deed accordingly // check dataset need alter // if (dataset = dsorders) or ... then... if dataset.fieldlist.count > 0 := dataset.fieldlist.count - 1 downto 0 begin if dataset.fieldlist.fields[i].classnameis('tmemofield') , (dataset.fieldlist.fields[i].fieldkind = fkdata) begin // save tmemofield properties aowner := dataset.fieldlist[i].owner; compname := dataset.fieldlist[i].name; fldname := dataset.fieldlist.fields[i].fieldname; // dispose of tmemofield dataset.fieldlist[i].dataset := nil; // un-attach dataset // create twideadomemofield instead fld := twideadomemofield.create(aowner); // create new persistent filed instead fld.name := compname + '_w'; fld.fieldname := fldname; fld.dataset := dataset; end; end; end;

that said, after have fixed issue, have never ever used persistent fields again. fields generated in run time. including calculated/lookup/internal fields.

database delphi dataset tclientdataset

No comments:

Post a Comment