RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStringField Class

TStringField represents a string field in a dataset.

Pascal
TStringField = class(TField);
C++
class TStringField : public TField;

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. Common uses for string fields are to store text, such as names and addresses. 

If you use the Fields editor at design time to create a persistent field component for the string field, you can access it by name at runtime. When using dynamic field components, you can access the TStringField instance using the dataset's Fields property or FieldByName method.  

Delphi Examples: 

 

{
This example requires two ClientDataSets, two DBGrids, two
DataSources and two DBNavigators.  Name the two
ClientDataSets CDS and CDS2. Set the DataSet property in the
first DataSource to CDS and name that DataSource DS.  Set the
DataSet property in the second DataSource to CDS2 and name
that DataSource DS2.  Set the DataSource in the first
DBNavigator to DS and the DataSource in the second DBNavigator
to DS2.  Set the DataSource in the first DBGrid to DS and the
DataSource in the second DBGrid to DS2.  Right click on CDS
and select the Fields Editor, then right click on the Field
Editor form and create two fields by selecting "new field".
In the field configuration window, set the name to HOST, the
Component value will automatically be set to CDSHOST.  Set
the filed type to String.  In the field configuration window
for the second field, set the name to PORTA, the Component
value will automatically be set to CDSPORTA.  Set the field
type to Integer.  The file used to load CDS must have an
attribute name of HOST with an attribute type of string and
an attribute name of PORTA with an attribute type of integer.
Add DB, DBCtrls, Grids, DBGrids, DBClient and SwSystem to the
uses clause.  "LoadFormFile" to start with the file, or 
"CreateDataSet" to start from scratch.  You must "CloseDataSet"
after a "LoadFromFile" before you can do another "CreateDataSet".
}
{$IFNDEF UNICODE}
uses SwSystem;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
begin
{$IFDEF UNICODE}
  CDS.LoadFromFile(GetCurrentDir + '\CDS.XML');
{$ELSE}
  CDS.LoadFromFile(gsAppPath + 'CDS.XML');
{$ENDIF}
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
{$IFDEF UNICODE}
  CDS.SaveToFile(GetCurrentDir + '\CDS.XML', dfXML);
{$ELSE}
  CDS.SaveToFile(gsAppPath + 'CDS.XML', dfXML);
{$ENDIF}
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  CDS.CreateDataSet;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  CDS2.Data := CDS.Data;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  CDS.Close;
end;
{
This example requires a button, a test edit, and a populated
ClientDataSet.  Pipe the ClientDataSet through a DataSource
to a DGGrid or DBNavigator to control the current field.
Cast the data correctly according to the field type when
assigning to the test edit.
}

{$IFNDEF UNICODE}
uses SwSystem;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
var MyBuffer: Pointer;
begin
{ Retrieve the "raw" data from Field1 }
with CDS.Fields[0] do
  begin
    if not IsBlob then { this does not work for BLOB fields }
    begin
      { Allocate space }
      MyBuffer:= GetMemory(DataSize);
      try
        if not GetData(MyBuffer) then
          MessageDlg(DisplayName + ' is NULL', mtInformation, [mbOK], 0)
        else
          { Do something with the data };
          Edit1.Text:= string(PAnsiChar(MyBuffer)); // for a stringfield
      finally
        { Free the space }
        FreeMem(MyBuffer, DataSize);
      end;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
{$IFDEF UNICODE}
  CDS.LoadFromFile(GetCurrentDir + '\CDS.XML');
{$ELSE}
  CDS.LoadFromFile(gsAppPath + 'CDS.XML');
{$ENDIF}
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!