-2

I have Insufficient memory error with TClientDataset with TStringField fields. I need to use length of strings from 0 to 8000 - but don't know what length will be until I fill the TClientDataset.

So, can TStringField be created with dynamic Size?

P.S. The initial task is to copy TDBGrid to TClientDataset.

Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • Isn't the question rather whether the TClientDataSet can allocate variable sized buffers for the records it reads from its provider, etc, because it has to allocate those en masse? It would be surprising it the answer to that were yes. It looks unlikely to me because of (in D7) function TCustomClientDataSet.AllocRecordBuffer: PChar; begin Result := AllocMem(FRecBufSize); end; - but that's virtual so you could always write your own custom descendant. – MartynA Jul 02 '14 at 12:09
  • I think you're trying to use the CDS in a terribly incorrect way, based on the two questions I've seen from you related to it. Neither of the issues you've asked about should occur if you were using the CDS as it's designed to be used. I'm afraid I can't tell what it is you're trying to accomplish, because all you're describing is the things that are going wrong with it. – Ken White Jul 02 '14 at 12:39
  • Ken, the issue is simple: too many TStringFields with Size = 8000 raises `Insufficient memory` error. My use of all components is totally CORRECT exactly regarding the customer's task. – Dmitry Jul 02 '14 at 12:55
  • Please don't tag your questions 'memory leak' just because you run out of memory. – Andy_D Jul 02 '14 at 12:57
  • 1
    Um... It may accomplish the "customer's task", but that doesn't mean you've chosen the proper class to do so (or are using it correctly). I can accomplish the task of driving a screw by using a hammer; just because I can make it work doesn't mean it's the proper way to do so. – Ken White Jul 02 '14 at 14:35
  • Where is your data coming from? Generally, when you load a clientDataSet directly from a query, the field types are determined automatically by the query results. Are you loading the data 'by hand' from some other source? If so, @SilverWarior has provided you with an answer on how to hanlde your situation. – Vector Jul 03 '14 at 11:39

2 Answers2

0

What kind of data are you storing in those fields to need up to 8000 characters?

Imagine you do partial search on those fields using wildcards. I bet such search would take ages and could posibly even crash your database server.

Besides not all databases tables support 8192 character sized StringFields.

http://docwiki.embarcadero.com/Libraries/XE6/en/Data.DB.TStringField

TStringField encapsulates the fundamental behavior common to fields that contain string data. A value of a string field is physically stored as a sequence of up to 8192 characters. However, some table types may only support string fields of smaller dimensions.

So why don't you use TMemoFields instead since they alow to have dynamical size of their text? http://docwiki.embarcadero.com/Libraries/XE6/en/Data.DB.TMemoField

SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • Fwiw MS Sql Server permitted unicode char fields of 4000 chars/8000 bytes even in the 2000 version. Istr that the first 900 bytes could be indexed. Also, TDataSets treat a TMemoField as a special case of a TBlobField, requiring creation/freeing of a TBlobStream fo every AsString access! – MartynA Jul 02 '14 at 18:34
  • +1 - But this really sounds like a database design issue, which is what you are hinting at. – Vector Jul 03 '14 at 09:16
  • Yes I'm pointing at that possibility. But in order to be sure I asked a question about what data he is storing in order to need 8000 character long strings. – SilverWarior Jul 03 '14 at 10:09
  • Did you print your answer for some other question here? I don't see any related information on it. – Dmitry Jul 03 '14 at 12:56
  • No I did not. Maybe you don't see how my answer relates to your question becouse I'm thinking out of the box and thus think about alternative approaches. In your question you are asking if it is posible to have dynamical sized StringFields. The answer is no becouse StringFields are fixed lenght data fields. And their size would have to be athleast the maximum size of the data you try to save in them even if it is only for one record. So what I'm proposing is the use of TMemo field which is similar to TStringList but it's size actually depends on the amount of data you store in it. – SilverWarior Jul 03 '14 at 13:18
-3

I have used the following solution. First, calculate maximum Sizes based on Length(aDBGrid.Columns[i].Field.DisplayText). Then create TStringFields with the calculated Sizes.

P.S. The initial task is to copy TDBGrid to TClientDataset.

Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • 3
    What do you mean - "Length(TStringField)"? If you have at least one item of size 8kb, then you get same problem again, even if all other items are small. So it is not a solution. – Andrei Galatyn Jul 02 '14 at 13:08
  • 2
    Not to mention that Length(TStringField) won't tell you the length of the string of a given instance of it. – MartynA Jul 02 '14 at 13:22
  • Sorry for misprint, fixed. At least one item of size 8kb won't be on non-strings fields. Huge memory usage by string fields depends on Embarcadero code. – Dmitry Jul 02 '14 at 14:26
  • As Ken White alluded to in a comment on your initial question, I don't think you're using the tools at your disposal properly and are making assumptions that simply aren't true. – Andy_D Jul 02 '14 at 15:49
  • -0.5 Why are you accessing the dataset's fields for this purpose through the columns of a TBGrid, why not access them via the dataset? It's an accident waiting to happen - not necessarily now, but eventually there may be fields of the dataset that aren't bound to grid columns. And, of course, SO answers are for the benefit of future readers. – MartynA Jul 02 '14 at 16:07
  • I don't like to downvote, but this time it's mandatory. This "answer" isn't... for all the reasons stated above. Wrong in every possible way. – Vector Jul 03 '14 at 09:11
  • Vector, are you all crazy here? It's the perfect solution to allocate as many memory as I need for my strict task. – Dmitry Jul 03 '14 at 12:55
  • @Altaveron - maybe you haven't explained yourself well, but right now you seem to be in the minority... :) – Vector Jul 04 '14 at 13:08