Persistent Objects in Windows XP/Delphi 7

前端 未结 4 2104
萌比男神i
萌比男神i 2021-01-06 03:43

I am trying to make an AlarmSystem in Delphi 7, Windows XP. I have to register alarms in a Database (MS SQL Server 2000). But what if the server is down??? Well, I can imagi

4条回答
  •  佛祖请我去吃肉
    2021-01-06 04:11

    You can inheriting from TPersistent and then you can use the TJvAppXMLFileStorage (JVCL) component to serialize the TAlarm class.

    Save a Object

    uses
      JvAppXMLStorage;
    
    Procedure SaveMyObject(MyAlarm : TAlarm)
    var
      MyStore: TJvAppXMLFileStorage;
    begin
      MyStore:= TJvAppXMLFileStorage.Create(nil);
      try
        MyStore.WritePersistent('', MyAlarm);
        MyStore.Xml.SaveToFile('C:\MyAlarm.xml');
      finally
        MyStore.Free;
      end;
    end;
    

    Restore a Object

    uses
      JvAppXMLStorage;
    
    Procedure LoadMyObject(MyAlarm : TAlarm)
    var
      MyStore: TJvAppXMLFileStorage;
    begin
      MyStore:= TJvAppXMLFileStorage.Create(nil);
      try
        MyStore.FileName:='C:\MyAlarm.xml';        
        MyStore.Xml.LoadFromFile('C:\MyAlarm.xml');
        MyStore.ReadPersistent('', MyAlarm);
      finally
        MyStore.Free;
      end;
    end;
    

    UPDATE

    If you need to persist more than one object to the XML file you must assign a path (unique id) to the WritePersistent and ReadPersistent methods.

    See this example,

    Multiple Persist

    Procedure SaveMyObjects(MyObjects : Array of TComponent);
    var
      MyStore: TJvAppXMLFileStorage;
      i      : integer;
    begin
      MyStore:= TJvAppXMLFileStorage.Create(nil);
      try
        for i := Low(MyObjects) to High(MyObjects) do
         MyStore.WritePersistent(MyObjects[i].Name, MyObjects[i]); //In this case i use the name property of the component.
        MyStore.Xml.SaveToFile('C:\Tools\MyAlarm.xml');
       finally
        MyStore.Free;
      end;
    end;
    

    to save the components

    SaveMyObjects([Button1,Button2,Edit1,Edit2]);
    

    Multiple LOAD

    Procedure LoadMyObjects(MyObjects:Array of TComponent);
    var
      MyStore    : TJvAppXMLFileStorage;
      i          : integer;
    
    begin
      MyStore:= TJvAppXMLFileStorage.Create(nil);
      try
        MyStore.FileName:='C:\Tools\MyAlarm.xml';
        MyStore.Xml.LoadFromFile('C:\Tools\MyAlarm.xml');
        for i := Low(MyObjects) to High(MyObjects) do
          MyStore.ReadPersistent(MyObjects[i].Name, MyObjects[i]);
      finally
        MyStore.Free;
      end;
    end;
    

    To restore the properties

    LoadMyObjects([Button1,Button2,Edit1,Edit2]);
    

    Another option to load

    Procedure LoadMyObjectById(Id:String;MyObject:TComponent); //using the id of the object
    var
      MyStore    : TJvAppXMLFileStorage;
      i          : integer;
    
    begin
      MyStore:= TJvAppXMLFileStorage.Create(nil);
      try
        MyStore.FileName:='C:\Tools\MyAlarm.xml';
        MyStore.Xml.LoadFromFile('C:\Tools\MyAlarm.xml');
       MyStore.ReadPersistent(id, MyObject);
      finally
        MyStore.Free;
      end;
    end;
    

    you must run it this way

    LoadMyObjectById(Button1.Name,Button1); //Again using the Name property.
    

    I hope this example will be useful ;)

提交回复
热议问题