⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functionlibrary.pas

📁 温度恒温控制,与PLC,工控板通讯,实现炉字过程控制.
💻 PAS
字号:
unit functionlibrary;

interface
uses
  windows,forms,classes,messages,IniFiles,SysUtils,Dbgrideh,db,ADODB,comobj,
  buttons,str_Utils;
type
  Tconfig=class(Tobject)
    public
      function read_ini(filename,Section, Ident, Default: String):string;
      procedure write_ini(filename,Section, Ident, value: String);
      function OpenChildForm(FormClass: TFormClass; var NewForm; AOwner:TComponent):boolean;
      function ErrorMessage(fadoconn:Tadoconnection;ErrorCode:integer;ErrorMsg:string):string;    //ado错误处理;
      function UpdateRecord(Connection:TadoConnection;TableName:string;FieldList:array of string;FieldValues:array of const;Condition:string=''):boolean;
      function InsertRecord(Connection:TadoConnection;TableName:string;FieldList:array of string;FieldValues:array of const):boolean;
end;


implementation

function Tconfig.InsertRecord(Connection:TadoConnection;TableName:string;FieldList:array of string;FieldValues:array of const):boolean;
var
  sqqInsert:TadoQuery;
  Sqlstr:string;
  FieldValueStr:string;
  i:integer;
begin
  setlength(fieldValueStr,500);
  result:=false;
  Sqlstr:='insert into '+ TableName +'(';
  for i:=low(FieldList) to high(FieldList) do
  begin
    if i<high(FieldList) then
      SqlStr:=SqlStr+FieldList[i]+','
    else
      SqlStr:=SqlStr+FieldList[i]+')';
  end;
  SqlStr:=SqlStr+'  values(';
  for i:=low(FieldValues) to high(FieldValues) do
  begin
    case FieldValues[i].VType of
      vtInteger:
        FieldValueStr:=inttostr(FieldValues[i].VInteger);
      vtExtended:
        FieldValueStr:=floattostr(FieldValues[i].VExtended^);
      vtBoolean:
        if FieldValues[i].VBoolean then FieldValueStr:='1' else FieldValueStr:='0';
      vtPChar:
        begin
          FieldValueStr:=FieldValues[i].VPChar;
          FieldValueStr:=''''+fieldValueStr+'''';
        end;
    end;
    if i<high(FieldValues) then
      SqlStr:=SqlStr+FieldValueStr+','
    else
      SqlStr:=SqlStr+FieldValueStr+')';
  end;
  sqqInsert:=TadoQuery.Create(nil);
  try
    sqqInsert.Connection:=Connection;
    sqqInsert.SQL.Add(SqlStr);
    try
      sqqInsert.ExecSQL;
      result:=true;
    except
       on E:EOLEexception do
       begin
          MessageBox(0,pchar(errormessage(connection,connection.Errors[0].NativeError,e.Message)),
            strBoxTitle,mb_ok+mb_iconInformation);
          result:=false;
      end;
    end;
  finally
    sqqInsert.Free;
  end;
end;

function Tconfig.UpdateRecord(Connection:TadoConnection;TableName:string;
  FieldList:array of string;FieldValues:array of const;Condition:string=''):boolean;
var
  sqqInsert:TadoQuery;
  Sqlstr:string;
  FieldValueStr:string;
  i:integer;
begin
  result:=false;
  Sqlstr:='update '+ TableName +' set ';
  if high(FieldList)<>high(FieldValues) then exit;
  for i:=low(FieldList) to high(FieldList) do
  begin
    case FieldValues[i].VType of
      vtInteger:
        FieldValueStr:=inttostr(FieldValues[i].VInteger);
      vtExtended:
        FieldValueStr:=floattostr(FieldValues[i].VExtended^);
      vtBoolean:
        if FieldValues[i].VBoolean then FieldValueStr:='1' else FieldValueStr:='0';
      vtPChar:
        begin
          FieldValueStr:=FieldValues[i].VPChar;
          fieldValueStr:=''''+fieldValueStr+'''';
        end;
    end;
    if i<high(FieldList) then
      SqlStr:=SqlStr+FieldList[i]+'='+FieldValueStr+','
    else
      SqlStr:=SqlStr+FieldList[i]+'='+FieldValueStr;
  end;

  if trim(condition)<>'' then
    SqlStr:=SqlStr+' where '+condition;
  sqqInsert:=TadoQuery.Create(nil);
  try
    sqqInsert.Connection:=Connection;
    sqqInsert.SQL.Add(SqlStr);
    try
      sqqInsert.ExecSQL;
      result:=true;
    except
       on E:EOLEexception do
       begin
          MessageBox(0,pchar(errormessage(connection,connection.Errors[0].NativeError,e.Message)),
            strBoxTitle,mb_ok+mb_iconInformation);
          result:=false;
      end;
    end;
  finally
    sqqInsert.Free;
  end;
end;

function Tconfig.ErrorMessage(fadoconn:Tadoconnection;ErrorCode:integer;ErrorMsg:string):string;
var
  sColumn: string;
  sTable:  string;
  ado_Error: TADOCommand;
  ado_Rst:  _Recordset;
  returnmessage: string;
  errornumber:integer;
begin
  errornumber:=0;
  result:=errormsg;
  ado_Error:=TADOCommand.Create(nil);
  try
    ado_Error.Connection:=fadoconn;
    if (ErrorCode=17) or (ErrorCode=0) or (ErrorCode=11) then
    begin
      result:='网络连接错误,请检查服务器及网络,请重试!';
      try
         errornumber:=errornumber+1;
         if errornumber>3 then
         begin
          messagebox(0,'网络或服务器出错,程序必须退出,请重新登录!',strBoxTitle,mb_ok+mb_iconerror);
          application.Terminate;
          exit;
         end;
         fadoconn.Close;
         fadoconn.Open;
         errornumber:=0;
      except
          on E:EOLEexception do
          begin
            MessageBox(0,pchar(errormessage(fadoconn,fadoconn.Errors[0].NativeError,e.Message)),
              strBoxTitle,mb_ok+mb_iconInformation);
          end;
      end;
    end;
    if (ErrorCode=32) then
    begin
      result:='该记录已被其它用户修改,重新读取!';
    end;
    if errorcode=18456 then
      result:='连接服务器系统登陆错误!';
    if ErrorCode=515 then
    begin
      sColumn:=Copy(ErrorMsg,23,Pos(',',ErrorMsg)-24);
      sTable:=Copy(ErrorMsg,Pos('dbo.',ErrorMsg)+4,Pos(';',ErrorMsg)-(Pos('dbo.',ErrorMsg)+4)-1);
      ado_Error.CommandText:='select c.[value] from syscolumns a,sysobjects b,'+
              'sysproperties c where b.id=c.id and a.id=b.id and a.colid=c.smallid'+
              ' and b.name='''+sTable+''' and a.name='''+sColumn+'''';

      ado_Rst:=ado_Error.Execute;
      sColumn:=ado_Rst.Fields[0].Value;
      ado_Error.CommandText:='select b.[value] from sysobjects a,sysproperties b '+
                            ' where a.id=b.id and b.smallid=0 and a.name='''+sTable+'''';
      ado_Rst:=ado_Error.Execute;
      sTable:=ado_Rst.Fields[0].Value;
      result:=sTable+'的'+sColumn+'不能为空。';
    end;
    if ErrorCode=547 then
    begin
{      sTable:=Copy(ErrorMsg,Pos('表',ErrorMsg)+4,Pos('column',ErrorMsg)-Pos('表',ErrorMsg)-7);
      ado_Error.CommandText:='select b.[value] from sysobjects a,sysproperties b '+
                            ' where a.id=b.id and b.smallid=0 and a.name='''+sTable+'''';
      ado_Rst:=ado_Error.Execute;
      sTable:=ado_Rst.Fields[0].Value;  }
      if pos('FK_',ErrorMsg)>0 then
      begin
        returnmessage:=copy(errormsg,pos('FK_',ErrorMsg)+3,pos('中使用',ErrorMsg)+6-pos('FK_',ErrorMsg)-3);
        result:='数据在'+returnmessage+'使用!';
      end;
      if pos('CK_',ErrorMsg)>0 then
      begin
        returnmessage:=copy(errormsg,pos('CK_',ErrorMsg)+3,pos('dosun',ErrorMsg)-pos('CK_',ErrorMsg)-3);
        result:=returnmessage;
      end;
    end;
    if errorcode=2627 then
    begin
       returnmessage:=copy(errormsg,pos('IX_',ErrorMsg)+3,pos('不能重复',ErrorMsg)+8-pos('IX_',ErrorMsg)-3);
       result:=returnmessage;
    end;
  finally
    ado_Error.Free;
  end;
end;

function Tconfig.OpenChildForm(FormClass: TFormClass; var NewForm; AOwner:TComponent):boolean;
var
  i: integer;
  Child:TForm;
begin
  result:=false;
  for i := 0 to Screen.FormCount -1 do
  begin
    if Screen.Forms[i].ClassType=FormClass then
    begin
      Child:=Screen.Forms[i];
      if Child.WindowState=wsMinimized then
        ShowWindow(Child.handle,SW_SHOWNORMAL)
      else
        ShowWindow(Child.handle,SW_SHOWNA);
      if (not Child.Visible) then Child.Visible:=True;
      Child.BringToFront;
      Child.Setfocus;
      TForm(NewForm):=Child;
      result:=true;
      exit;
    end;
  end;
  Child:=TForm(FormClass.NewInstance);
  TForm(NewForm):=Child;
  Child.Create(AOwner);
end;



function Tconfig.read_ini(filename,Section, Ident, Default: String):string;
var
  iniFile:TiniFile;
begin
  result:='';
  iniFile:=TIniFile.Create(filename);
  try
    result :=inifile.ReadString(Section, Ident, Default);
  finally
    iniFile.Free;
  end;

end;
procedure Tconfig.write_ini(filename,Section, Ident, value: String);
var
  iniFile:TiniFile;
begin
  iniFile:=TIniFile.Create(filename);
  try
    inifile.WriteString(Section, Ident, value);
  finally
    iniFile.Free;
  end;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -