谁能给个简单的例子!! ( 积分: 100 )

  • 主题发起人 主题发起人 zzk123
  • 开始时间 开始时间
楼上的
聪明人只要给他指个方向,他就知道路怎么走了;只有盲人才要手把手的扶着走。
 
利用ORMapping控件就行了,何必自己设计,确实要自己设计实现,可参考这些控件的做法,反正我是懒人,只想用现成的
 
给你别人以前写的不可能完全适合你,给你写也不可能,我想不会有那么闲的人吧,不要吧写一个类想的那么简单,你以为两三行代码就可以封装一个数据库的类吗,那绝对是垃圾。
沙隆巴斯的主人, 时间:2005-11-13 13:57:36, ID:3264138
楼上的
聪明人只要给他指个方向,他就知道路怎么走了;只有盲人才要手把手的扶着走。
 
说点有用的吧
建立一个类,建立3个属性,分别是你这数据库3个字段的类型
2。建立类数组;
3。query或者table获取全部数据
4,根据query的recordcount循环向类数组里赋值
5,根据类数组的下标控制向控件赋值。
 
大概就是像下面這樣子。
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.
 
结帖吧!!
 
后退
顶部