转自:http://liucanwei509.blog.163.com/blog/static/171261132011112104242689/
unit DbHelper;
interface
uses Classes,DB, ADODB,SysUtils,UProcParameter;
type
TDbHelper=class
class function GetANewCon(conString:string):TADOConnection;
private
FAdoCon:TADOConnection;
FConString:string;
function GetAdoCon:TADOConnection;
public
property AdoCon:TADOConnection read GetAdoCon;
function OpenSql(Sql: String): TDataSet;
procedure ExecSql(Sql: String);
procedure ExecSqlTran(SqlList: TStringList);
function IsExistsRec(Sql: String): Boolean;
function GetFirstFieldAsInt(Sql: String): Integer;
function GetFirstFieldAsStr(Sql: String): string;
constructor Create(conString:string);reintroduce;
destructor Destroy;override;
function OpenProc(procName:string;procParams:TProcParameters):TDataSet;
procedure ExecProc(procName: string; procParams: TProcParameters);
end;
implementation
{ TDbHelper }
constructor TDbHelper.Create(conString:string);
begin
inherited Create;
FConString:=conString;
end;
destructor TDbHelper.Destroy;
begin
inherited Destroy;
if Assigned(FAdoCon) then FreeAndNil(FAdoCon);
end;
function TDbHelper.GetAdoCon: TADOConnection;
begin
Result:=FAdoCon;
if not Assigned(FAdoCon) then
FAdoCon:=TAdoConnection.Create(nil);
if not FAdoCon.Connected then
begin
FAdoCon.ConnectionString:=FConString;
FAdoCon.KeepConnection:=True;
FAdoCon.LoginPrompt:=False;
FAdoCon.Open;
end;
Result:=FAdoCon;
end;
class function TDbHelper.GetANewCon(conString: string): TADOConnection;
begin
Result:=TAdoConnection.Create(nil);
Result.ConnectionString:=conString;
Result.KeepConnection:=True;
Result.LoginPrompt:=False;
Result.Open;
end;
function TDbHelper.OpenSql(Sql: String): TDataSet;
var
AQuery:TAdoQuery;
begin
Result:=nil;
AQuery:=TAdoQuery.Create(nil);
AQuery.Connection:=GetAdoCon;
AQuery.SQL.Text:=Sql;
AQuery.Open;
Result:=AQuery;
end;
procedure TDbHelper.ExecSql(Sql: String);
var
AQuery:TAdoQuery;
begin
AQuery:=TAdoQuery.Create(nil);
try
AQuery.Connection:=GetAdoCon;
AQuery.SQL.Text:=Sql;
AQuery.ExecSQL;
finally
AQuery.Close;
FreeAndNil(AQuery);
end;
end;
procedure TDbHelper.ExecSqlTran(SqlList: TStringList);
var
AQuery:TAdoQuery;
Sql:String;
i:Integer;
begin
AQuery:=TAdoQuery.Create(nil);
try
AQuery.Connection:=GetAdoCon;
AQuery.Connection.BeginTrans;
try
for i:=0 to SqlList.Count-1 do
begin
Sql:=SqlList.Strings[i];
AQuery.SQL.Text:=Sql;
AQuery.ExecSQL;
AQuery.Close;
end;
AQuery.Connection.CommitTrans;
except
AQuery.Connection.RollbackTrans;
end;
finally
AQuery.Close;
FreeAndNil(AQuery);
end;
end;
function TDbHelper.IsExistsRec(Sql: String): Boolean;
var
AResSet:TDataSet;
begin
Result:=False;
AResSet:=OpenSql(Sql);
if AResSet<>nil then
begin
Result:=AResSet.RecordCount>0;
AResSet.Close;
FreeAndNil(AResSet);
end;
end;
function TDbHelper.GetFirstFieldAsInt(Sql:String): Integer;
var
AQuery:TAdoQuery;
begin
Result:=-1;
try
AQuery:=TAdoQuery.Create(nil);
AQuery.Connection:=GetAdoCon;
AQuery.SQL.Text:=Sql;
AQuery.Open;
if AQuery.RecordCount>0 then
begin
Result:=AQuery.Fields.Fields[0].AsInteger;
end;
finally
if Assigned(AQuery) then FreeAndNil(AQuery);
end;
end;
function TDbHelper.GetFirstFieldAsStr(Sql: String): String;
var
AQuery:TAdoQuery;
begin
Result:='';
try
AQuery:=TAdoQuery.Create(nil);
AQuery.Connection:=GetAdoCon;
AQuery.SQL.Text:=Sql;
AQuery.Open;
if AQuery.RecordCount>0 then
begin
Result:=AQuery.Fields.Fields[0].AsString;
end;
finally
if Assigned(AQuery) then FreeAndNil(AQuery);
end;
end;
function TDbHelper.OpenProc(procName:string;procParams:TProcParameters):TDataSet;
var
aProc:TADOStoredProc;
aParameter:TProcParameter;
i:Integer;
begin
aProc:=TADOStoredProc.Create(nil);
aProc.Connection:=GetAdoCon;
aProc.ProcedureName:=procName;
for i:=0 to procParams.Count-1 do
begin
aParameter:=procParams.GetParameter(i);
with aProc.Parameters.AddParameter do
begin
Name:=aParameter.Name;
DataType:=aParameter.DataType;
Direction:=aParameter.Direction;
if aParameter.Size<>0 then Size:=aParameter.Size;
Value:=aParameter.Value;
end;
end;
aProc.Open;
Result:=aProc;
end;
procedure TDbHelper.ExecProc(procName:string;procParams:TProcParameters);
var
aProc:TADOStoredProc;
aParameter:TProcParameter;
i:Integer;
begin
aProc:=TADOStoredProc.Create(nil);
try
aProc.Connection:=GetAdoCon;
aProc.ProcedureName:=procName;
for i:=0 to procParams.Count-1 do
begin
aParameter:=procParams.GetParameter(i);
with aProc.Parameters.AddParameter do
begin
Name:=aParameter.Name;
DataType:=aParameter.DataType;
Direction:=aParameter.Direction;
if aParameter.Size<>0 then Size:=aParameter.Size;
Value:=aParameter.Value;
end;
end;
aProc.ExecProc;
for i:=0 to procParams.Count-1 do
begin
aParameter:=procParams.GetParameter(i);
if (aParameter.Direction=pdOutput) or (aParameter.Direction=pdInputOutput) or (aParameter.Direction=pdReturnValue) then
begin
aParameter.Value:=aProc.Parameters[i].Value;
end;
end;
finally
FreeAndNil(aProc);
end;
end;
end.
转载于:https://blog.51cto.com/jxyx521/1293908