哪个是在Delphi中保存数据的最佳方法

时间:2021-11-27 15:14:47

I work on a program in Delphi that holds a lot of data, and I wonder which method is the best to save it to file. Now we use records and "file of" to save it but I think it should be better methods. I would prefer a system that makes it easy to migrate from the system we use now.

我在Delphi中处理一个包含大量数据的程序,我想知道哪种方法最好将其保存到文件中。现在我们使用记录和“文件”来保存它,但我认为它应该是更好的方法。我更喜欢一个系统,可以很容易地从我们现在使用的系统迁移。

EDIT: The application is a sort of a database application. The user use it to manage data.

编辑:应用程序是一种数据库应用程序。用户使用它来管理数据。

6 个解决方案

#1


6  

It depends on the data! Common means are databases (rows of lots of records), XML (structured data storage), ini files (simple data) or custom formats (graphic images etc). Delphi is very good with databases, with a range of third party options to allow fully compiled-in code, so for general data they will work well.

这取决于数据!常用方法是数据库(大量记录行),XML(结构化数据存储),ini文件(简单数据)或自定义格式(图形图像等)。 Delphi非常适合数据库,有一系列第三方选项可以完全编译代码,因此对于一般数据,它们可以很好地工作。

#2


3  

There are many options. If you're sure you will never need more then one user, I would go for ClientDatasets saved in XML. Dan Miser has written a few articles about them that are really good. If you are not sure you will need one or more users in the future I would go for an embedded database. Firebird is a good option for that. See http://www.firebirdsql.org/manual/fbmetasecur-embedded.html for more information. With the same code you will be able to make a Multi user version in the future.

有很多选择。如果你确定你永远不会需要一个以上的用户,我会选择以XML格式保存的ClientDatasets。 Dan Miser写了一些非常好的文章。如果您不确定将来是否需要一个或多个用户,我会选择嵌入式数据库。 Firebird是一个不错的选择。有关更多信息,请参阅http://www.firebirdsql.org/manual/fbmetasecur-embedded.html。使用相同的代码,您将来可以制作多用户版本。

#3


2  

If it is a kind of a database application, storing the data in a database might be the logical choice.

如果它是一种数据库应用程序,将数据存储在数据库中可能是合乎逻辑的选择。

One alternative approach would be to use a streamer:

一种替代方法是使用流光:

procedure TmyThing.PutData(AFile: String);
var
  writer: TWriter;
  stream: TFileStream;
begin
  stream := TFileStream.Create(AFile, fmCreate);
  try
    writer := TWriter.Create(stream, $ff);
    try
      with writer do
      begin
        WriteSignature;         {marker to indicate a Delphi filer object file.}
        WriteListBegin;         {outer list marker}
        WriteFloat(cVersion);   {write the version for future use}
        WriteString(someProperty);
        {... etc. ...}
        WriteListEnd;           {outer list marker}
      end;
    finally
      writer.Free;
    end;
  finally
    stream.Free;
  end;
end;

#4


2  

For a simple application, why not use a local TClientDataset? It would allow searching, sorting, and the use of the database controls. If you include midaslib in your uses clause, you can also deploy the application without any external dll's.

对于一个简单的应用程序,为什么不使用本地TClientDataset?它将允许搜索,排序和使用数据库控件。如果在uses子句中包含midaslib,则还可以在没有任何外部dll的情况下部署应用程序。

#5


1  

var

 FileStream: TFileStream;


procedure TForm1.Load(Sender: TObject);

Begin

if FileExists ('Thing2.dat') then

      Begin             
      FileStream := TFileStream.Create('Thing2.dat', fmOpenRead);
      FileStream.ReadComponent( {Thing like Edit1} );
      FileStream.Free;
      End;
end;

and load

并加载

procedure TForm1.Save(Sender: TObject);

Begin



 FileStream := TFileStream.Create('Thing2.dat', fmcreate);

 FileStream.WriteComponent(  {Thing like Edit1}   );

 FileStream.Free;



end; 

This should work cause it works for me

这应该工作因为它适合我

#6


0  

Also depends on what you want to do with it once you have saved it... is it just storage of 'archived' data at this point ? Or do you want to search it, retrieving some of the data at some point in the future then saving it back again (potentially changed), or do you load it all in, manipulate it in some way, then save it back out again ?

一旦你保存了它,还取决于你想用它做什么......这只是存储'存档'数据吗?或者你想搜索它,在将来的某个时刻检索一些数据然后再将它保存回来(可能已经改变),或者你把它全部加载,以某种方式操纵它,然后再将它保存回去?

#1


6  

It depends on the data! Common means are databases (rows of lots of records), XML (structured data storage), ini files (simple data) or custom formats (graphic images etc). Delphi is very good with databases, with a range of third party options to allow fully compiled-in code, so for general data they will work well.

这取决于数据!常用方法是数据库(大量记录行),XML(结构化数据存储),ini文件(简单数据)或自定义格式(图形图像等)。 Delphi非常适合数据库,有一系列第三方选项可以完全编译代码,因此对于一般数据,它们可以很好地工作。

#2


3  

There are many options. If you're sure you will never need more then one user, I would go for ClientDatasets saved in XML. Dan Miser has written a few articles about them that are really good. If you are not sure you will need one or more users in the future I would go for an embedded database. Firebird is a good option for that. See http://www.firebirdsql.org/manual/fbmetasecur-embedded.html for more information. With the same code you will be able to make a Multi user version in the future.

有很多选择。如果你确定你永远不会需要一个以上的用户,我会选择以XML格式保存的ClientDatasets。 Dan Miser写了一些非常好的文章。如果您不确定将来是否需要一个或多个用户,我会选择嵌入式数据库。 Firebird是一个不错的选择。有关更多信息,请参阅http://www.firebirdsql.org/manual/fbmetasecur-embedded.html。使用相同的代码,您将来可以制作多用户版本。

#3


2  

If it is a kind of a database application, storing the data in a database might be the logical choice.

如果它是一种数据库应用程序,将数据存储在数据库中可能是合乎逻辑的选择。

One alternative approach would be to use a streamer:

一种替代方法是使用流光:

procedure TmyThing.PutData(AFile: String);
var
  writer: TWriter;
  stream: TFileStream;
begin
  stream := TFileStream.Create(AFile, fmCreate);
  try
    writer := TWriter.Create(stream, $ff);
    try
      with writer do
      begin
        WriteSignature;         {marker to indicate a Delphi filer object file.}
        WriteListBegin;         {outer list marker}
        WriteFloat(cVersion);   {write the version for future use}
        WriteString(someProperty);
        {... etc. ...}
        WriteListEnd;           {outer list marker}
      end;
    finally
      writer.Free;
    end;
  finally
    stream.Free;
  end;
end;

#4


2  

For a simple application, why not use a local TClientDataset? It would allow searching, sorting, and the use of the database controls. If you include midaslib in your uses clause, you can also deploy the application without any external dll's.

对于一个简单的应用程序,为什么不使用本地TClientDataset?它将允许搜索,排序和使用数据库控件。如果在uses子句中包含midaslib,则还可以在没有任何外部dll的情况下部署应用程序。

#5


1  

var

 FileStream: TFileStream;


procedure TForm1.Load(Sender: TObject);

Begin

if FileExists ('Thing2.dat') then

      Begin             
      FileStream := TFileStream.Create('Thing2.dat', fmOpenRead);
      FileStream.ReadComponent( {Thing like Edit1} );
      FileStream.Free;
      End;
end;

and load

并加载

procedure TForm1.Save(Sender: TObject);

Begin



 FileStream := TFileStream.Create('Thing2.dat', fmcreate);

 FileStream.WriteComponent(  {Thing like Edit1}   );

 FileStream.Free;



end; 

This should work cause it works for me

这应该工作因为它适合我

#6


0  

Also depends on what you want to do with it once you have saved it... is it just storage of 'archived' data at this point ? Or do you want to search it, retrieving some of the data at some point in the future then saving it back again (potentially changed), or do you load it all in, manipulate it in some way, then save it back out again ?

一旦你保存了它,还取决于你想用它做什么......这只是存储'存档'数据吗?或者你想搜索它,在将来的某个时刻检索一些数据然后再将它保存回来(可能已经改变),或者你把它全部加载,以某种方式操纵它,然后再将它保存回去?