unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TRecordStr = record
Name: string[20];
Age: Integer;
//以后扩充的字段只能加在这后面,为了保持跟之前的兼容
end;
TForm1 = class(TForm)
btn1: TButton;
edt1: TEdit;
edt2: TEdit;
btn2: TButton;
edt3: TEdit;
edt4: TEdit;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
r: TRecordStr;
ms: TMemoryStream;
begin
r.Name := edt1.Text;
r.Age := StrToIntDef(edt2.Text,1);
ms := TMemoryStream.Create;
try
ms.write(r,SizeOf(r));
ms.Seek(0,soFromBeginning);
// ms.SaveToFile('c:/1.dat');-->改成保存到数据库
finally
ms.Free;
end;
end;
procedure TForm1.btn2Click(Sender: TObject);
var
r: TRecordStr;
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
//ms.LoadFromFile('c:/1.dat'); 改成从数据库中取
ms.Seek(0,soFromBeginning);
ms.read(r,SizeOf(r));
edt3.Text := r.Name;
edt4.Text := IntToStr(r.Age);
finally
ms.Free;
end;
end;
end.