我自己学编的一个数据库操作的类
那里有错,不好,需改进的地方,请各位大虾指点一二.//------------------------------------------------------------------------
// 文件名:DBOperate.pas
//
// 类名:数据库操作类
// 描述:用于实现数据库的基本操作功能
//
// 作者:Win Lai
// 创建日期:2003-12-11
// 修改日期:2003-12-12
//
// 使用:
//
// 1.定义类对象。
// MyClass : TDBOperate;
//
// 2.分配空间给类对象。
// MyClass := TDBOperate.Create(nil);
//
// 3.建立数据库链接。
// MyClass.ConnectionStrings := '[数据库链接串]'
//
// 4.操作数据库。
// MyClass.SQLStrings := '[SQL语句]'
//
// 5.链接数据库控件。
// 链接ADOQuery: MyClass.DataSource.DataSet := MyClass.ADOQuery
// 链接ADOTable: MyClass.DataSource.DataSet := MyClass.ADOTable
// [数据库控件].DataSource := MyClass.DataSource;
//
// 6.释放类对象。
// MyClass.Free;
//
//------------------------------------------------------------------------unit DBOperate;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ADODB, DB;type
// 操作数据库的类型
// opOpen : 返回数据集,如:select
// opExec : 不返回数据集,如:insert, update, delete, create table
TOperateType = (otOpen, otExec);type
TDBOperate = class(TObject) private
FADOConnection: TADOConnection;
FADOQuery: TADOQuery;
FADOTable: TADOTable;
FDataSource: TDataSource; FConnectionStrings: string;
FSQLStrings: string;
FReservedWordList: TStringList; function GetOperateType(const SqlStr: string=''): TOperateType;
function GetReservedWordToList(index: integer=0): string; procedure SetReservedWordToList(index: integer; const word: string);
procedure SQLExec(const SqlStr: string);
procedure BuildReservedWordList; protected public
constructor Create(AOwner: TComponent); virtual;
destructor Destroy; override; procedure Connect(ConnectionString: string); function Disconnect: boolean;
function IsConnected: boolean; property ADOConnection: TADOConnection read FADOConnection write FADOConnection;
property ADOQuery: TADOQuery read FADOQuery write FADOQuery;
property ADOTable: TADOTable read FADOTable write FADOTable;
property DataSource: TDataSource read FDataSource write FDataSource;
property ConnectionStrings: string read FConnectionStrings write Connect;
property SQLStrings: string read FSQLStrings write SQLExec;
end;implementation//------------------------------------------------------------------------
// 构造函数
constructor TDBOperate.Create(AOwner: TComponent);
begin
inherited Create();
//..
FADOConnection := TADOConnection.Create(nil);
FADOQuery := TADOQuery.Create(nil);
FADOTable := TADOTable.Create(nil);
FDataSource := TDataSource.Create(nil);
FReservedWordList := TStringList.Create; FADOConnection.LoginPrompt := false; // 不显示登录框 BuildReservedWordList;
end;//------------------------------------------------------------------------
// 析构函数
destructor TDBOperate.Destroy;
begin
//..
Disconnect;
FADOConnection.Free;
FADOQuery.Free;
FADOTable.Free;
FDataSource.Free;
FReservedWordList.Free; inherited Destroy;
end;//------------------------------------------------------------------------
// 链接数据库
procedure TDBOperate.Connect(ConnectionString: string);
begin
// 已经链接
if IsConnected=true then
begin
exit
end; try
FADOQuery.Connection := FADOConnection; FADOConnection.Close;
FADOConnection.ConnectionString := ConnectionString;
FADOConnection.Open; if FADOConnection.Connected = false then
begin
// 链接数据库失败
Application.MessageBox('Connect Faild!', 'Error', MB_OK OR MB_ICONERROR);
Application.Terminate;
end;
except
Application.MessageBox('Connect Faild!', 'Error', MB_OK OR MB_ICONERROR);
Application.Terminate;
end; FConnectionStrings := ConnectionString;
end;//------------------------------------------------------------------------
// 断开链接数据库
function TDBOperate.Disconnect: boolean;
begin
// 已经断开链接
if IsConnected=false then
begin
Result := true;
exit;
end; try
FADOConnection.Close;
Result := true;
except
Result := false;
end;
end;//------------------------------------------------------------------------
// 检查是否链接数据库
function TDBOperate.IsConnected: boolean;
begin
if ADOConnection.Connected = false then
Result := false
else
Result := true;
end;//------------------------------------------------------------------------
// 执行SQL语句,默认值为otExec
procedure TDBOperate.SQLExec(const SqlStr: string);
var
tempOperateType: TOperateType;
begin
FADOQuery.Close;
FADOQuery.SQL.Text := SqlStr; // 执行SQL语句的类型
tempOperateType := GetOperateType(SqlStr);
case tempOperateType of
otOpen: FADOQuery.Open;
otExec: FADOQuery.ExecSQL;
end; FSQLStrings := SqlStr;
end;//------------------------------------------------------------------------
// 获取数据库操作的类型
function TDBOperate.GetOperateType(const SqlStr: string=''): TOperateType;
var
tempSqlStr: string; // 临时SQL字符串
tempWord: string; // 临时的保留字
tempLoop: integer; // 循环的位置
begin
tempSqlStr := LowerCase(SqlStr); // 如果为空,返回默认值otExec
if tempSqlStr='' then
begin
Result := otExec;
exit;
end; // 从保留字列表中查找保留字的操作类型
for tempLoop:=0 to FReservedWordList.Count-1 do
begin
tempWord := LowerCase(GetReservedWordToList(tempLoop));
if Pos(tempWord, tempSqlStr)<>0 then
// 如果SQL语句的操作是select,返回操作类型为otOpen
if CompareStr(tempWord, 'select')=0 then
begin
Result := otOpen;
exit;
end;
end; // 查找不到,返回默认值otExec
Result := otExec;
end;//------------------------------------------------------------------------
// 创建SQL语句的保留字列表,如:select, insert, delete, update等默认值
procedure TDBOperate.BuildReservedWordList;
begin
FReservedWordList.Clear;
SetReservedWordToList(0, 'select');
SetReservedWordToList(1, 'insert');
SetReservedWordToList(2, 'delete');
SetReservedWordToList(3, 'update');
end;//------------------------------------------------------------------------
// 从保留字列表中获取保留字
// index为获取的位置
function TDBOperate.GetReservedWordToList(index: integer=0): string;
begin
// 保留字列表为空,返回空字符
if FReservedWordList.Count=0 then
begin
Result := '';
exit;
end;
// 超出保留字列表的范围,返回空字符
if FReservedWordList.Count<=index then
begin
Result := '';
exit;
end; Result := FReservedWordList[index];
end;//------------------------------------------------------------------------
// 插入保留字到保留字列表
// index为插入的位置,word为插入的字符串
procedure TDBOperate.SetReservedWordToList(index: integer; const word: string);
begin
if FReservedWordList.Count<=index then
FReservedWordList.Add(word) // 不在列表的范围,插入到列表的最后
else
FReservedWordList.Insert(index, word);
end;//------------------------------------------------------------------------
end.