2021年9月12日 星期日

Try..Except..End 例外狀況處理

Try
  ...
  ...
Except
  ...
End;


Exception

Try
  ...
  ...
Except
   on E:Exception do
  begin
    MessageBox(Handle, PWideChar(E.Message), PWideChar(E.Class), MB_ICONError);
  end
End;

2021年9月2日 星期四

Delphi DataSet和JSON互轉函式

 一、DataSet轉JSON

//1)資料集轉換為JSON字串:
//需USES System.JSON;
function DataSetToJson(ADataset: TDataSet): string;
var
  LRecord: string;
  LField: TField;
  i: integer;
begin
  Result := '';
  if (not ADataset.Active) or (ADataset.IsEmpty) then
    Exit;
  Result := '[';
  ADataset.DisableControls;
  ADataset.First;
  while not ADataset.Eof do
  begin
    for i := 0 to ADataset.FieldCount - 1 do
    begin
      LField := ADataset.Fields[i];
      if LRecord = '' then
        LRecord := '{"' + LField.FieldName + '":"' + LField.Text + '"'
      else
        LRecord := LRecord + ',"' + LField.FieldName + '":"' + LField.Text + '"';
      if i = ADataset.FieldCount - 1 then
      begin
        LRecord := LRecord + '}';
        if Result = '[' then
          Result := Result + LRecord
        else
          Result := Result + ',' + LRecord;
        LRecord := '';
      end;
    end;
    ADataset.Next;
  end;
  ADataset.EnableControls;
  Result := Result + ']';
end;

二、JSON轉DataSet

//2)JSON字串轉換為資料集:
procedure JsonToDataSet(AJson: string; ADataset: TDataSet);
var
  jDataSet: TJSONArray;
  jRecord: TJSONObject;
  i, j: Integer;
begin
  if (AJson = '') or (ADataset = nil) or (not ADataset.Active) then
    Exit;
  jDataSet := TJSONArray.Create;
  jDataSet := TJSONObject.ParseJSONValue(AJson, True) as TJSONArray;
  while not ADataset.Eof do
    ADataset.Delete;
  for i := 0 to jDataSet.Size - 1 do
  begin
    ADataset.Append;
    jRecord := jDataSet.Get(i) as TJSONObject;
    for j := 0 to ADataset.FieldCount - 1 do
    begin
      if ADataset.Fields[j].ReadOnly then
        Continue;
      ADataset.Fields[j].AsString := jRecord.GetValue(ADataset.Fields[j].FieldName).Value;
    end;
    ADataset.Post;
  end;
end;

轉貼至: https://www.itread01.com/content/1548927014.html,(因應語法及版本做適當的修正)


Json To ClientDataset

procedure JsonToClientDataset(AJson:String; AClientDataset:TClientDataset);
var
  jDataSet: TJSONArray;
  jRecord: TJSONObject;
  i, j: Integer;
  procedure CreateClientDataset;
  var k, iFieldCount:Integer;
    sFieldName:String;
  begin
    AClientDataset.Close;
    AClientDataset.Fields.Clear;
    AClientDataset.FieldDefs.Clear;
    jRecord := jDataSet.Get(0) as TJSONObject;
    iFieldCount := jRecord.Count; //欄位數
    for k := 0 to iFieldCount-1 do
    begin
      sFieldName := Trim(jRecord.Pairs[k].JsonString.Value);
      AClientDataset.FieldDefs.Add(sFieldName, ftString, 2000);
    end;
    AClientDataset.CreateDataSet;
  end;
begin
  if (AJson = '') or (AClientDataset = nil) then
    Exit;
  jDataSet := TJSONArray.Create;
  jDataSet := TJSONObject.ParseJSONValue(AJson, True) as TJSONArray;
  //
  CreateClientDataset;
  //
  for i := 0 to jDataSet.Size - 1 do
  begin
    AClientDataset.Append;
    jRecord := jDataSet.Get(i) as TJSONObject;
    for j := 0 to AClientDataset.FieldCount - 1 do
    begin
      AClientDataset.Fields[j].AsString := jRecord.GetValue(AClientDataset.Fields[j].FieldName).Value;
      jDataSet.Count;
    end;
    AClientDataset.Post;
  end;
end;

2021年9月1日 星期三

截取 ADO 錯誤訊息

 procedure ADOConnection1ExecuteComplete(Connection: TADOConnection; RecordsAffected: Integer;
  const Error: Error; var EventStatus: TEventStatus; const Command: _Command; const Recordset: _Recordset);
begin
  //ADO 錯誤訊息
  if EventStatus=esErrorsOccured then
  begin
    ShowMessage(Error.Description);
  end;
end;

組件資源引用

uses  AdoConEd;
if  AdoConEd.EditConnectionString(AdoConnection) then  ...




Path:
    C:\Program Files (x86)\Embarcadero\Studio\17.0\source\Property Editors

uses SQLEdit;