unit MyClass;
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
VCLCom, StdVcl, DBClient,Grids, DBGrids,ComCtrls, DB, ADODB, Provider;
type
TMyDataClass = class
private
FAdoConn : TADoConnection; //Ado数据连接
aqTmp: TADoQuery;
acmd: TAdoCommand;
aspTmp: TADOStoredProc;
public
function OpenQuery(Sqlstr:string):boolean; overload;//传入sql打开数据
function OpenQuery(Query:TAdoQuery;Sqlstr:string):boolean; overload;//传入sql打开数据
function OpenDataSetByPara(InPara:variant;adsTmp:TadoDataSet):boolean;overload;//传入参数,打开一个dataset
function OpenDataSet(DataSet:TADoDataSet;Sqlstr:string):boolean;
function IsExists(Sqlstr:string):boolean;//传入sql查询是否记录存在,true存在
function Get_Sql_Value(sqlstr:string):variant;//输入sql,返回一个字段数值
function ExecSql(Sqlstr:string):boolean;overload; //执行一段sql命令
function ExecSql(Sqlstr:string;pTrans:boolean):boolean;overload; //执行一段sql命令
//执行存储过程
function ExecProc(ProcName: string; var ParamStrs: array of variant;
Direction: array of TParameterDirection): boolean;overload;
function ExecProc(ADOStoredProc: TADOStoredProc ;ProcName: string; var ParamStrs: array of variant;
Direction: array of TParameterDirection;ExecType:byte): boolean;overload;
//数据更新函数,pTrans:事务处理,返回值:true成功,false失败
function SaveData(ads_Tmp: array of TADODataSet;pTrans:Boolean):boolean;overload;
function UpdateValueToField(pDataSet:TDataSet;value:variant;FieldName:string):boolean;//更新数据表某个字段数值
function ChangeFieldValue(pDataSet:TDataSet;Oldvalue,NewValue:variant;FieldName:string):boolean;//更新数据表某个字段数值
//删除指定的记录,返回值:FieldName字段,fieldValue:指定的记录数值
function DeleteRecord(ads_Tmp: TADODataSet;FieldName:string;FieldValue:variant):boolean;overload;
procedure DeleteTable(DataSet:TDataSet);//删除数据表所有记录
function Check_Unique_Field_Value(sqlstr:string):boolean;//验证字段数值是否唯一
procedure Set_Filter(str_filter:string;DataSEt:TDATASET);//设置过滤
Constructor Create(AdoConn: TAdoConnection);
Destructor Destroy;override;
end;
var
MyDataClass: TMyDataClass;
implementation
...
------------{实现}
Constructor TMyDataClass.Create(AdoConn: TAdoConnection);
begin
FAdoConn := AdoConn;
aqTmp := TADoQuery.Create(nil);
aqTmp.Connection :=FAdoConn;
acmd := TAdoCommand.Create(nil);
acmd.Connection := FAdoConn;
aspTmp := TADOStoredProc.Create(nil);
aspTmp.Connection :=FAdoConn;
end;
Destructor TMyDataClass.Destroy;
begin
inherited;
end;
...
------------{调用}
var MyDataClass: TMyDataClass;
MyDataClass :=TMyDataClass.Create(dm.ADOConnection1);