大概就是像下面這樣子。
program Project1;
uses
Forms,
U_Main in 'U_Main.pas' {Form1},
U_User in 'U_User.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
---------------------------------------------
unit U_Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,U_User;
type
TForm1 = class(TForm)
edtUserName: TEdit;
edtAge: TEdit;
edtID: TEdit;
btnRead: TButton;
btnWrite: TButton;
procedure btnReadClick(Sender: TObject);
procedure btnWriteClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
fuser:CustomUser;
public
{ Public declarations }
end;
var
Form1: TForm1;
const
ConnStr='';
//這里寫你的連接字符串
implementation
{$R *.dfm}
procedure TForm1.btnReadClick(Sender: TObject);
begin
fuser.ReadUser(StrToInt(edtID.Text));
end;
procedure TForm1.btnWriteClick(Sender: TObject);
begin
fuser.ID:=StrToInt(edtID.Text);
fuser.Age:=StrToInt(edtAge.Text);
fuser.UserName:=edtUserName.Text;
fuser.Save;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fuser:=CustomUser.Create(self,ConnStr);
end;
end.
-----------------------------------------------
unit U_User;
interface
uses Classes,SysUtils,DB,ADODB;
type
CustomUser=class(TObject)
private
FAge:Integer;
FID:Integer;
FUserName:String;
FAQ:TADOQuery;
//可以靈活用其他數據集,數據模塊也可以,看自己怎么領會并運用
procedure SetID(Value:Integer);
procedure SetAge(Value:Integer);
procedure SetUserName(Value:String);
public
property ID:Integer read FID write SetID;
property Age:Integer read FAge write SetAge;
property UserName:String read FUserName write SetUserName;
constructor Create(AOwner: TComponent;ConnStr:String);
destructor Destroy;override;
procedure ReadUser(AID:Integer);
procedure Save;
end;
implementation
{ CustomUser }
constructor CustomUser.Create(AOwner: TComponent;
ConnStr: String);
begin
inherited Create;
faq:=TADOQuery.Create(AOwner);
with faqdo
begin
ConnectionString:=ConnStr;
end;
end;
destructor CustomUser.Destroy;
begin
if Assigned(faq) then
faq.Free;
inherited;
end;
procedure CustomUser.ReadUser(AID: Integer);
begin
FID:=AID;
with faqdo
try
SQL.Text:='select * from usertable where id='''+IntToStr(AID)+'''';
open;
FAge:=FieldByName('age').AsInteger;
FUserName:=FieldByName('UserName').AsString;
close;
except
cancel;
end;
end;
procedure CustomUser.Save;
begin
with faqdo
try
SQL.Text:='select * from usertable';
open;
append;
FieldByName('age').AsInteger:=FAge;
FieldByName('id').AsInteger:=FID;
FieldByName('UserName').AsString:=FUserName;
post;
close;
except
cancel;
end;
end;
procedure CustomUser.SetAge(Value: Integer);
begin
if FAge<>Value then
FAge:=Value;
end;
procedure CustomUser.SetID(Value: Integer);
begin
if FID<>Value then
FID:=Value;
end;
procedure CustomUser.SetUserName(Value: String);
begin
if FUserName<>Value then
FUserName:=Value;
end;
end.