2007. április 5., csütörtök
DataSet -> Strings -> DataSet
Problem/Question/Abstract:
DataSet -> Strings -> DataSet
Answer:
///////Begin Source
function StrLeft(const mStr: string; mDelimiter: string): string;
begin
Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end; { StrLeft }
function ListCount(mList: string; mDelimiter: string = ','): Integer;
var
I, L: Integer;
begin
Result := 0;
if mList = '' then
Exit;
L := Length(mList);
I := Pos(mDelimiter, mList);
while I > 0 do
begin
mList := Copy(mList, I + Length(mDelimiter), L);
I := Pos(mDelimiter, mList);
Inc(Result);
end;
Inc(Result);
end; { ListCount }
function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;
var
I, L, K: Integer;
begin
L := Length(mList);
I := Pos(mDelimiter, mList);
K := 0;
Result := '';
while (I > 0) and (K <> mIndex) do
begin
mList := Copy(mList, I + Length(mDelimiter), L);
I := Pos(mDelimiter, mList);
Inc(K);
end;
if K = mIndex then
Result := StrLeft(mList + mDelimiter, mDelimiter);
end; { ListValue }
function DataSetToText(mDataSet: TDataSet; mStrings: TStrings;
mDelimiter: string = #9): Boolean;
var
vBookmark: string;
I: Integer;
S: string;
begin
Result := False;
if (not Assigned(mDataSet)) or (not mDataSet.Active) or
(not Assigned(mStrings)) then
Exit;
mStrings.Text :=
StringReplace(Trim(mDataSet.FieldList.Text), #13#10, mDelimiter, [rfReplaceAll]);
vBookmark := mDataSet.Bookmark;
mDataSet.DisableControls;
try
mDataSet.First;
while not mDataSet.Eof do
begin
S := '';
for I := 0 to mDataSet.FieldList.Count - 1 do
S := S + mDelimiter + mDataSet.FieldList[I].AsString;
Delete(S, 1, Length(mDelimiter));
mStrings.Add(S);
mDataSet.Next;
end;
finally
mDataSet.Bookmark := vBookmark;
mDataSet.EnableControls;
end;
Result := True;
end; { DataSetToText }
function TextToDataSet(mStrings: TStrings; mDataSet: TDataSet;
mDelimiter: string = #9): Boolean;
var
I, J, C: Integer;
vFieldNames: string;
begin
Result := False;
if (not Assigned(mDataSet)) or (not mDataSet.Active) or
(mStrings.Count <= 0) then
Exit;
vFieldNames := mStrings[0];
C := ListCount(vFieldNames, mDelimiter);
for I := 1 to mStrings.Count - 1 do
try
mDataSet.Append;
for J := 0 to C - 1 do
if mDataSet.FieldList.IndexOf(ListValue(vFieldNames, J, mDelimiter)) >= 0 then
mDataSet[ListValue(vFieldNames, J, mDelimiter)] :=
ListValue(mStrings[I], J, mDelimiter);
mDataSet.Post;
except
Exit;
end;
Result := True;
end; { TextToDataSet }
///////End Source
///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
DataSetToText(Table1, Memo1.Lines);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
TextToDataSet(Memo1.Lines, Table1);
end;
///////End Demo
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése