www.pudn.com > MailServer.rar > DataSet.pas


unit DataSet; 
interface 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs,ADODB_TLB; 
 
type 
TDataSet=class 
  private 
    FConnString: String; 
    FConnection:TConnection; 
    procedure SetConnectionStr(const Value: String); 
  public 
    Property strConnection:String read FConnString write SetConnectionStr; 
    Function OpenDb(strConnection:String = ''):String; 
    Function CloseDb():String; 
    Function ExecSQL(lSQL:String):String; 
    Function GetData(SelectSQL:String;var ErrText:String):TRecordSet;overload; 
    Function GetData(SelectSQL:String):TRecordSet;overload; 
  end; 
 
implementation 
 
procedure TDataSet.SetConnectionStr(const Value: String); 
begin 
  If Trim(Value) <> '' Then 
    FConnString := Value; 
end; 
 
function TDataSet.OpenDb(strConnection: String): String; 
begin 
  Result:=''; 
  FConnString:=strConnection; 
  If FConnString='' then 
    FConnString := 'Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=MarketDFMS3.0;Data Source=SKYDOT'; 
  if not Assigned(FConnection) then 
    FConnection := TConnection.Create(Application); 
  try 
    If FConnection.State <> adStateOpen then  
      FConnection.Open(FConnString,'','',0); 
  except 
    on e:Exception do result:=e.Message; 
  end; 
end; 
 
function TDataSet.CloseDb(): String; 
begin 
  Result:=''; 
  If FConnection.State = adStateOpen Then 
  begin 
    Try 
        FConnection.Close; 
    except 
      on e:Exception do Result:=e.Message; 
    end; 
    FConnection := nil; 
  end; 
end; 
 
function TDataSet.ExecSQL(lSQL: String): String; 
var 
  lolevar:OleVariant; 
begin 
  result:=''; 
  try 
    FConnection.Execute(lSQL,lolevar,0); 
  except 
    on e:Exception do Result :=e.Message; 
  end; 
end; 
 
function TDataSet.GetData(SelectSQL: String; var ErrText: String): TRecordSet; 
var 
  lRecordSet:TRecordSet; 
begin 
  Result := nil ; 
  ErrText:=''; 
  if pos('SELECT',UpperCase(SelectSQL))=0 then 
  begin 
    ErrText := '²éѯÓï¾äÖÐȱÉÙ[SELECT]¹Ø¼ü×Ö'; 
    exit; 
  end; 
  lRecordSet := TRecordSet.Create(Application); 
 try 
  lRecordSet.Open(SelectSQL, FConnString, adOpenDynamic,adLockOptimistic, adCmdUnspecified); 
  except 
    on e:Exception do ErrText := e.Message; 
  end; 
  Result := lRecordSet; 
end; 
 
function TDataSet.GetData(SelectSQL: String): TRecordSet; 
var 
  lErrText:String; 
begin 
  Result:= GetData(SelectSQL,lErrText); 
end; 
 
end.