这个你有看过么?
转贴:
我刚学DELPHI的时候也是按书中介绍的那样做我的数据库程序。生成一个DATAMODULE,在
上面放数据控件,比如TTABLE、TQUERY、TDATASOURCE。做小程序时这种方法很好效率也可
以,一般的项目一周就可以了。但做比较大的项目就有些混乱了。在一个DataModule里放上
好几十个数据控件,搞得我晕头转向。当然你也可以把数据控件放在多个DataModule里,这
样可以让你不晕头转向但程序“肥”了起来。这样做还有一个缺点,你不能把商业规则(
Business Logic)放在一处,程序做到最后你可能已经不知道哪儿是哪儿了。还会出现摸名
奇妙的错误。
---分析--------------------
用面向对象的方法做一般的应用程序比数据库程序容易一些,因为它不涉及到数据库。但
在做数据库程序时应该考虑类跟跟数据库是怎样联系的。还有DELPHI提供很多数据控件我们
是不是用它还是用一般的控件。
---解决--------------------
创建一个跟数据库联系的基类,其他的类从基类继承。
基类
bRecordSet
TDbRecordset=class(TComponent)
private
FMasterFields: string;
FMasterSource: TDataSource;
function GetIsEmpty: boolean;
function GetRecordCount: integer;
function GetState: TDataSetState;
function GetDataSource: TDataSource;
procedure SetMasterFields(const Value: string);
procedure SetMasterSource(const Value: TDataSource);
protected
FTable:TTable;
FQuery:TQuery;
FDataSource:TDataSource;
FDisplayFields:TStringList;
FSqlType:TSqlType;
FIsEmpty:boolean;
FRecordCount:integer;
FOnRecordScrolled:TNotifyEvent;
FRecordListSource:TDataSource;
procedure FTableAfterScroll(DataSet: TDataSet);
function GetDisplayField: TStringList;virtual;
public
property DataSet:TTable read FTable;
property DataSource:TDataSource read GetDataSource;
property IsEmpty:boolean read GetIsEmpty;//表是不是为空
property RecordCount:integer read GetRecordCount;//记录数
property State:TDataSetState read GetState;
property DisplayField:TStringList read GetDisplayField;//要在DBGrid显示的字段
property RecordScrolled:TNotifyEvent read FOnRecordScrolled write FOnRecordScrolled;//记录滚动事件
property MasterSource:TDataSource read FMasterSource write SetMasterSource;
property MasterFields:string read FMasterFields write SetMasterFields;
property RecordListSource:TDataSource read FRecordListSource;//记录集合
procedure Insert;virtual;//添加记录
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
function Delete:boolean;virtual;//删除记录
function EmptyTable:boolean;virtual;
function First:boolean;virtual;//第一条记录
function Last:boolean;virtual;//最后一条记录
function Next:boolean;virtual;//下一条记录
function Prior:boolean;virtual;//前一条记录
function Post:boolean;virtual;//保存记录
function NoMsgPost:boolean;virtual;//没有提示信息的保存
function Cancel:boolean;virtual;
procedure OpenSql(TheQuery:TQuery;SqlStr:string;SqlType:TSqlType);//运行SQL语句
procedure RefreshRecordList;virtual;
procedure PRefresh;virtual;
end
图书类-TBooks
const
C_Book='Book';//表名
TBook=class(TDbRecordset)
private
FISBN:string;
FBookName:string;
FBookType:TBookType;
FBookTypeId:string;
FAuthor:string;
FPublisher:string;
FContent:string;
function GetBookName: string;
function GetContent: string;
function GetISBN: string;
function GetPublisher: string;
function GetType: string;
function GetAuthor:string;
procedure SetBookName(const Value: string);
procedure SetContent(const Value: string);
procedure SetISBN(const Value: string);
procedure SetPublisher(const Value: string);
procedure SetType(const Value: string);
procedure SetAuthor(const Value: string);
function GetBookTypeList: TStringList;
procedure BindFieldValues;
public
property ISBN:string read GetISBN write SetISBN;
property BookName:string read GetBookName write SetBookName;
property BookType:string read GetType write SetType;
property Author:string read GetAuthor write SetAuthor;
property Publisher:string read GetPublisher write SetPublisher;
property Content:string read GetContent write SetContent;
property BookTypeList:TStringList read GetBookTypeList;
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
function Post:boolean;override;
end;