Adding field to open recordset - ado

I there a simple way to append a new field to an existing open ADO RecordSet?
fields.append() won't work if the RecordSet is open, and closing appears to kill the existing data.
NB: I'm using Microsoft ActiveX DataObject 2.8 Library

You can't append fields to a recordset while it's open.
You can create a clone of the recordset, append your required fields, open it and copy the data from the original.
The other option is to persist the recordset as xml, modify the rowset schema, add required fields & then load xml into a new recordset.

Related

How to get Image displayed in DBWPRichText component which inturn connected to Memotype Field?

I am working with Client server architecture in Delphi with SQL server 2014 as the database.
I have a database fieldname "Document" that is of type "Text" which contains rtf data which contains an image data. I am using ado dataset which gets the field as "Memo". This ado dataset is in turn connected to a client dataset and connects to a DBWPRichText control. The "Document" field data that is fetched is getting truncated which results in loss of image data being displayed in DBWPRichText.
I tried adding the field in dataset as Blob and could get the Image displayed in the DBWPRichText control. Alas! while saving the data via dataset post method getting error message "Text is incompatible with image".
I think this could be because of field being changed to BLOB manually by me to display the Image where db field is of the type "Text" .
Tried with WPRichText which works fine where I am loading data from fieldname "Document" however while loading into control I am converting string to stream and while saving converting the WPRichText control data from stream to string.
In my case I have to use DBWPRichText control connected to a database as there are few mail merge fields involved and dont want to loose those.
By the way cannot change the field type as it is used in almost many places and dont want to break anything.
If any one can guide a way through to go achieve with DBWPRichText without changing the db field type would be really appreciated.
Thanks

Create a .mdb without MS Access or ADOX

I'm using dephi 2010, which is getting difficult with me about installing the ADOX components. So I was wondering if there is a way to create a .mdb file without the use of the ADOXCatalog.
-Thank you.
Yes, this can be done without using an ADOXCatalog.
Place a TAdoConnection and e.g. a TAdoCommand on a form or datamodule. Set the TAdoCommand's Connection property to the TAdoConnection.
Then, in the AdoConnection's ConnectionString builder, select Microsoft OLE DB Driver for ODBC. Then, follow the ODBC wizard to set up a new MDB database. As you follow that through you will be able to create a File DSN (unless you are running Delphi as adminstrator), select the Access Jet driver, specify the database path (making sure it is somewhere writeable) and name, and then the wizard presents you with a button to click to create the MDB file.
Although it is not in English, there is a video here
https://www.youtube.com/watch?v=E_2hrER9oho
which shows you exactly how to do this. The ODBC connection string wizard should give you the option to create a new datasource and present you with a list like this to choose from:
Set the TAdoCommand's CommandText to something like
create table ATable (AName TEXT(40))
and call its Execute method at r/time to create a one-column table.
Btw, you could equally well use a TAdoQuery instead of the TAdoCommand component using its Sql property instead of the TAdoCommand's CommandTextproperty, and you should be able to use any valid DDL statements to define the tables in the database.

How do I add data to a database in Delphi by using edits instead of a DBNavigator?

My interface is very basic. It just includes edits for the user to input data into a database, when they click the button i want it to add the data into my database.
You can easily do this.
Go to the Data Controls tab of the Component palette.
Select a TDBEdit and place it on the same form as your DBNavigator. The IDE will name this DBEdit1
Set the Datasource property of your DBEdit1 to the same datasource as your DBNavigator.
Set the DataField property of DBEdit1 to the name of a field in your dataset.
Compile and run.
That's it. Leave your DBNavigator on your form because you will find that when you make a change to the contents of DBEdit1, its Save and Cancel buttons automatically enable to let you save or cancel the change.
Also, you'll find that if you click your DBNavigator's '+' button, which begins the insertion of a new record into your table, you can then type the field values for the new record into your DBEdits.
Don't use normal non-DB-aware TEdit components and a dynamically-created Sql statement which concatenates the TEdits's contents with other Sql as suggested in the other answer which briefly appeared here and now seems to have been deleted - it is a waste of time, but much more importantly renders your app vulnerable to Sql-Injection - see https://en.wikipedia.org/wiki/SQL_injection. By sending the server an unverified Sql statement which includes what the user has typed into a TEdit, you're effectively providing the user with an opportunity to type additional Sql statements into the TEdit and that is exactly how Sql injection can occur. On the other hand, when you use TDBEdits, the Sql for updating the database record is automatically generated by Delphi's TDataSet framework in a way which does not provide a similar opportunity for Sql Injection.
If some reason you absolutely have to generate your own Sql Update statements, to minimise the risk of Sql Injection, make sure that you use a parameterised Update statement, that is, one where the changed field values are specified as values of parameters in your TDataSet-descendant's Parameters object, rather than in the Update Sql itself. An example of a parameterised Update statement might be:
Update MyTable set FieldA =:FieldA, FieldB=:FieldB where RowID =:RowID
where :FieldA, :FieldB and :RowID are the parameters.

How can I set a TClientDataSet's data in the design view?

I have a TClientDataSet object that I've added to my form.
I've managed to define the fields through the object inspector. However, is there a way to define the data the TClientDataSet contains without doing it in code?
I've managed to do it by adding some AppendRecord statements to the ShowForm event; but I'd rather keep the definition of the TClientDataSet all in one place.
Prior to v10.2 (Seattle, Berlin, etc.), there were no facilities to do this.
You can store and load a file into the ClientDataSet or you can use the Assign Local Data (right click on the ClientDataSet) to load data from an existing source. However, you cannot append or edit the data from inside the IDE directly.
As of v10.2 (Tokyo) has recently added this as a feature.

ADODataset: how to load XML (saved beforehand in DB in ADO schema) data without temporary files?

Warning: total rewrite.
Scenario:
I loaded some data from database on a TCustomADODataset descendant. After that, I saved this data on XML temp file (using TCustomADODataset.SaveToFile) to allow getting the XML data as a string and store it on a database table as text blob - it's an exports table.
Another program (different from the one that stored the XML) will take that data, show the elements inside, and allow an user to select which element to import to the main database schema.
Problem:
The problem with the approach above is the need of temporary files to allow TCustomADODataset use the LoadFromFile method.
There's any other way to load that XML data stored as text in the database exports table into a TCustomADODataset that don't need temporary files?
Notes:
TClientDataset is not an option in this case.
Check this example. It is probably exactly what you are looking for. Using the RecordsetFromXML from that example you can simply assign the recordset to your TCustomADODataSet.Recordset property.

Resources