delphi 开发使用的基类C

来源:互联网 发布:穆斯林软件下载 编辑:程序博客网 时间:2024/05/16 05:02

 unit CommonDB;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBGrids, DbClient, Grids, dxDBTL, StdCtrls, ExtCtrls, TeeProcs,
  TeEngine, Chart, ADODB, dxDBGrid, DB, Jpeg, Series, Registry;

type
  TCommonDB = class(TComponent)
  private
    { Private declarations }
    {FQuerySystem: IQuerySystem;
    FUpdateSystem: IUpdateSystem;
    FMtsSystem: IMtsSystem;
    }
  public
    { Public declarations }
    constructor Create(szParams: string);       //创建对象
    destructor Destroy; override; //释放对象
    procedure AddComboBoxItemNull(ASqlText: string; AClientDataSet: TClientDataSet;
      AFieldName: string; AComboBox: TComboBox; AFieldNameID: string; AList: TStrings);
    procedure AddComboBoxItem(ASqlText: string; AClientDataSet: TClientDataSet;
      AFieldName: string; AComboBox: TComboBox; AFieldNameID: string; AList: TStrings);//将一个集合的字段写到combobox
    procedure PostPhoto(AImage: TImage; AClientDataSet: TClientDataSet;
      AFieldName: string);//保存图片
    procedure GetPhoto(AImage: TImage; AClientDataSet: TClientDataSet;
      AFieldName: string);//读出图片
    function IfRepeat(AClientDataSet: TClientDataSet; AField,
      ARepeatContent: string): Boolean;//判断cds中记录是否有重复
    function LocateCds(AClientDataSet: TClientDataSet;
      AFieldName: string; AFindValues: Variant): Boolean;//interbase中定位数据集的一种方式
    function RecCurrMatcth(AClientDataSet: TClientDataSet;
      AFieldName: string; AFindValues: Variant): Boolean;    //用来判断AClientDataSet中的当前记录中的字段值是否跟跟提供的字段值想匹配
    procedure OpenPhotoFile(AImage: TImage); //选择图片文件
    function GetCurrentTime: TDateTime; //取得服务器的当前时间

    function ExecuteData(ASqlText: string): Boolean;// 通过Com+执行sql语句
    procedure GetData(ASqlText: string; AClientDataSet: TClientDataSet);
    procedure Reset;

    function ExecSp(ASpName: string): Boolean;

    procedure ExecInsertSp(ASpName: string; AOutputIndex: Integer;
      AClientDataSet: TClientDataSet; var ALsh: Integer); //封装当前界面新增存储过程
    procedure ExecUpdateSp(ASpName: string; AClientDataSet: TClientDataSet); //封装当前界面修改存储过程
    procedure ExecDeleteSp(ASpName: string; ALsh: Integer); //封装当前界面修改存储过程

    //陈飞
    procedure AddListItem(ASqlText: string; AClientDataSet: TClientDataSet;
      AFieldName: string; AList: TStrings; AFieldID: string; AListID: TStrings);//将一个集合的字段写到combobox
     function GetCxjg(ASqlText, ACaption01, ACaption02: string; var AValue01, AValue02: string): Boolean;
  end;

type
  TLogin = class(TComponent)
  private
    { Private declarations }
  public
    { Public declarations }
   end;

var
  CommonDB: TCommonDB;
  Login: TLogin;
  --IEX: TZHKZXT_ComServerEx;
 -- Ars: TArgs;

implementation

uses aCommon, aCommonDM,afrmCommonCx;

//创建对象
constructor TCommonDB.Create(szParams: string);
var
  sSql: string;
begin 
  Common := TCommon.Create(nil);
  dmCommon := TdmCommon.Create(nil);
 end;

//释放对象
destructor TCommonDB.Destroy;
begin
  Common.Free;
end;

procedure TCommonDB.GetData(ASqlText: string;
  AClientDataSet: TClientDataSet);
begin
  try
   AClientDataSet.Close;
   AClientDataSet.Clear;
   AClientDataSet.Commandtext := ASqlText;
   AClientdataset.Open;
  except
    Common.ShowInfo('查询语句错误!');
    Exit;
  end;
end;

function TCommonDB.ExecuteData(ASqlText: string): Boolean;
begin
  if IEX.ExecuteCommandNoQuery(ASqlText) = -2 then
    Result := False
  else
    Result := True;
end;

function TCommonDB.IfRepeat(AClientDataSet: TClientDataSet; AField,
  ARepeatContent: string): Boolean;
var
  sList: TStrings;
  I, J, iListIndex: Integer;
  sTmpStr: string;
begin
  sList := TStringList.Create;
  if not AClientDataSet.Active then
    AClientDataSet.Active := True;
  AClientDataSet.First;
  sList.Clear;
  while not AClientDataSet.Eof do
  begin
    sList.Add(AClientDataSet.FieldByName(AField).AsString);
    AClientDataSet.Next;
  end;
  iListIndex := -1;
  for I := 0 to sList.Count - 1 do
  begin
    sTmpStr := sList.Strings[I];
    if I < sList.Count - 1 then
    begin
      for J := (I + 1) to sList.Count - 1 do
        if sTmpStr = sList.Strings[J] then
        begin
          iListIndex := I;
          Break;
        end;
    end;
  end;
  if iListIndex = -1 then
    Result := False
  else begin
    Result := True;
    ARepeatContent := sList.Strings[iListIndex];
  end;
  sList.Free;
end;

procedure TCommonDB.AddComboBoxItem(ASqlText: string;
  AClientDataSet: TClientDataSet; AFieldName: string; AComboBox: TComboBox;
  AFieldNameID: string; AList: TStrings);
var
  slTmp: TStrings;
begin
  if AComboBox = nil then Exit;
  CommonDB.GetData(ASqlText, AClientDataSet);
  AComboBox.Clear;
  AClientDataSet.First;
  slTmp := TStringList.Create;
  while not AClientDataSet.Eof do
  begin
    if Trim(AClientDataSet.FieldByName(AFieldName).AsString) <> '' then
    begin
      AComboBox.Items.Add(AClientDataSet.FieldByName(AFieldName).AsString);
      if Trim(AFieldNameID) <> '' then
        slTmp.Add(AClientDataSet.FieldByName(AFieldNameID).AsString);
    end;
    AClientDataSet.Next;
  end;
  if AList <> nil then
    AList.Assign(slTmp);
  slTmp.Free;
end;

function TCommonDB.RecCurrMatcth(AClientDataSet: TClientDataSet;
  AFieldName: string; AFindValues: Variant): Boolean;
var
  Fields: TList;
  I, J: Integer;
  Value: Variant;
begin
  Result := True ;
  Fields := TList.Create;
  AClientDataSet.GetFieldList(Fields, AFieldName);
  I := Fields.Count;
  if (I = 1) and not VarIsArray(AFindValues) then
  begin
    Value := AFindValues;
    if AClientDataSet.FieldByName(TField(Fields[0]).FieldName).Value <> Value  then
    Result := False ;
    Exit;
  end;
  for J := 0 to I - 1 do
  begin
    Value := AFindValues[J];
    if AClientDataSet.FieldByName(TField(Fields[J]).FieldName).Value <> Value then
    begin
      Result := False ;
      Exit;
    end;
  end;
end;

function TCommonDB.LocateCds(AClientDataSet: TClientDataSet;
  AFieldName: string; AFindValues: Variant): Boolean;
begin
  if AClientDataSet.Locate(AFieldName, AFindValues, []) then
    Result := True
  else
    Result := False;
end;

procedure TCommonDB.PostPhoto(AImage: TImage; AClientDataSet: TClientDataSet;
  AFieldName: string);
var
  Input: TClientBlobStream;
  jPhoto: TJpegImage;
begin
  if AImage.Picture.Graphic <> nil then
  begin
    Input := TClientBlobStream.Create(AClientDataSet.FieldByName(AFieldName) as TBlobField,
      bmWrite);
    jPhoto := TJpegImage.Create;
    jPhoto.Assign(AImage.Picture.Graphic);
    jPhoto.SaveToStream(Input);
    jPhoto.Free;
    Input.Free;
  end;
end;

procedure TCommonDB.GetPhoto(AImage: TImage;
  AClientDataSet: TClientDataSet; AFieldName: string);
var
  Input: TClientBlobStream;
  jPhoto: TJpegImage;
begin
  Input := TClientBlobStream.Create(AClientDataSet.FieldByName(AFieldName) as TBlobField, bmRead);
  if Input.Size <> 0 then
  begin
    Input.Position := 0;
    jPhoto := TJpegImage.Create;
    jPhoto.LoadFromStream(Input);
    AImage.Picture.Assign(jPhoto);
    jPhoto.Free;
  end
  else
    AImage.Picture.Graphic := nil;
  Input.Free;
end;

procedure TCommonDB.AddComboBoxItemNull(ASqlText: string;
  AClientDataSet: TClientDataSet; AFieldName: string; AComboBox: TComboBox;
  AFieldNameID: string; AList: TStrings);
var
  slTmp: TStrings;
begin
  if AComboBox = nil then Exit;

  CommonDB.GetData(ASqlText, AClientDataSet);
  AComboBox.Clear;
  AComboBox.Items.Add('不限');
  AClientDataSet.First;
  slTmp := TStringList.Create;
  while not AClientDataSet.Eof do
  begin
    if Trim(AClientDataSet.FieldByName(AFieldName).AsString) <> '' then
    begin
      AComboBox.Items.Add(AClientDataSet.FieldByName(AFieldName).AsString);
      if Trim(AFieldNameID) <> '' then
        slTmp.Add(AClientDataSet.FieldByName(AFieldNameID).AsString);
    end;
    AClientDataSet.Next;
  end;
  if AList <> nil then
    AList.Assign(slTmp);
  slTmp.Free;
end;

procedure TCommonDB.OpenPhotoFile(AImage: TImage);
begin
  if dmCommon.opdPhoto.Execute then
    AImage.Picture.LoadFromFile(dmCommon.opdPhoto.FileName);
end;

procedure TCommonDB.Reset;
begin
  IEX.Reset;
end;

function TCommonDB.ExecSp(ASpName: string): Boolean;
begin
  if IEX.ExecClientCommand(ASpName) then
    Result := True
  else
    Result := False;
end;

function TCommonDB.GetCurrentTime: TDateTime;
begin
  Result := Now;
end;

procedure TCommonDB.ExecDeleteSp(ASpName: string; ALsh: Integer);
begin
  Reset;
 { AddParam(ftString, 40, pdInput, Login.CurrCzydm);
  AddParam(ftFloat, 15, pdInput, ALsh);
  ExecSp(ASpName);}
end;

procedure TCommonDB.ExecInsertSp(ASpName: string; AOutputIndex: Integer;
  AClientDataSet: TClientDataSet; var ALsh: Integer);
var
  fType: TFieldType;
  I: Integer;
begin
  Reset;
 { AddParam(ftString, 40, pdInput, Login.CurrCzydm);
  for I := 0 to AClientDataSet.FieldCount - 1 do
  begin
  {  if AClientDataSet.Fields.Fields[I].DataType = ftBCD then
      fType := ftFloat
    else
      fType := AClientDataSet.Fields.Fields[I].DataType;

    if I = AOutputIndex - 1 then
      AddParam(fType, AClientDataSet.Fields.Fields[I].DataSize,
        pdOutput, ALsh)
    else begin
      if AClientDataSet.Fields.Fields[I].Value = Null then
      begin
        if fType = ftFloat then
          AddParam(ftInteger, AClientDataSet.Fields.Fields[I].DataSize,
            pdInput, 'NULL')
        else
          AddParam(fType, AClientDataSet.Fields.Fields[I].DataSize,
            pdInput, 'NULL')
      end
      else
        AddParam(fType, AClientDataSet.Fields.Fields[I].DataSize,
          pdInput, AClientDataSet.Fields.Fields[I].Value);
    end;
  end;
  ExecSp(ASpName);
  ALsh := iex.ValueOf(AOutputIndex);


  {dmCommon.spInsert.Parameters[0].Value := Login.CurrCzydm;
  for I := 0 to AClientDataSet.FieldCount - 1 do
  begin
    if I = 0 then
      dmCommon.spInsert.Parameters[I + 1].Value := ALsh
    else
      dmCommon.spInsert.Parameters[I + 1].Value := AClientDataSet.Fields.Fields[I].Value;
  end;
  dmCommon.spInsert.ExecProc;
  }
end;

procedure TCommonDB.ExecUpdateSp(ASpName: string;
  AClientDataSet: TClientDataSet);
var
  fType: TFieldType;
  I: Integer;
begin
 { CommonDB.Reset;
  CommonDB.AddParam(ftString, 40, pdInput, Login.CurrCzydm);
  for I := 0 to AClientDataSet.FieldCount - 1 do
  begin
    if AClientDataSet.Fields.Fields[I].DataType = ftBCD then
      fType := ftFloat
    else
      fType := AClientDataSet.Fields.Fields[I].DataType;

    if AClientDataSet.Fields.Fields[I].Value = Null then
    begin
      if fType = ftFloat then
        CommonDB.AddParam(fType, AClientDataSet.Fields.Fields[I].DataSize, pdInput, 0)
      else
        CommonDB.AddParam(fType, AClientDataSet.Fields.Fields[I].DataSize, pdInput, 'NULL')
    end
    else
      CommonDB.AddParam(fType, AClientDataSet.Fields.Fields[I].DataSize,
        pdInput, AClientDataSet.Fields.Fields[I].Value);
  end;
  CommonDB.ExecSp(ASpName);

  {dmCommon.spUpdate.Parameters[0].Value := Login.CurrCzydm;
  for I := 0 to AClientDataSet.FieldCount - 1 do
    dmCommon.spUpdate.Parameters[I + 1].Value := AClientDataSet.Fields.Fields[I].Value;
  dmCommon.spUpdate.ExecProc;
  }
end;

procedure TCommonDB.AddListItem(ASqlText: string;
  AClientDataSet: TClientDataSet; AFieldName: string; AList: TStrings;
  AFieldID: string; AListID: TStrings);
begin
  CommonDB.GetData(ASqlText, AClientDataSet);
  AList.Clear;
  AListID.Clear;
  AClientDataSet.First;
  while not AClientDataSet.Eof do
  begin
    if AList <> nil then
      AList.Add(AClientDataSet.FieldByName(AFieldName).AsString);
    if AListID <> nil then
      AListID.Add(AClientDataSet.FieldByName(AFieldID).AsString);
    AClientDataSet.Next;
  end;
end;

function TCommonDB.GetCxjg(ASqlText, ACaption01, ACaption02: string;
  var AValue01, AValue02: string): Boolean;
begin
  try
    --frmCommonCx := TfrmCommonCx.Create(nil);
    --CommonDB.GetData(ASqlText, frmCommonCx.cdsCx);
    --frmCommonCx.dbgc01.Caption := ACaption01;
    --frmCommonCx.dbgc02.Caption := ACaption02;
   -- frmCommonCx.dbgc01.FieldName := frmCommonCx.cdsCx.Fields[0].FieldName;
   -- frmCommonCx.dbgc02.FieldName := frmCommonCx.cdsCx.Fields[1].FieldName;
   -- frmCommonCx.dbgCx.KeyField := frmCommonCx.cdsCx.Fields[0].FieldName;
    --frmCommonCx.ShowModal;
  finally
   -- if frmCommonCx.GbOk then
    begin
   --   AValue01 := frmCommonCx.cdsCx.FieldByName(frmCommonCx.dbgc01.FieldName).AsString;
   --   AValue02 := frmCommonCx.cdsCx.FieldByName(frmCommonCx.dbgc02.FieldName).AsString;
   --   Result := True;
    end
    else
  --    Result := False;
  --  frmCommonCx.Free;
   -- frmCommonCx := nil;
  end;
end;

end.