unit Unit3;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, DB, ADODB, DBTables;type
    TForm3 = class(TForm)
    Button1: TButton;
    Session1: TSession;
    Database1: TDatabase;
    Table1: TTable;
    Memo1: TMemo;
    Label1: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;  procedure getdata(ncount:integer;s:string);var
  Form3: TForm3;implementation{$R *.dfm}type
     TtableThread = class(TThread)
  private
     Ftable:ttable;
     FtableException: Exception;
     procedure tableError;protected
    procedure Execute; override;
public
    constructor Create(t: Ttable); virtual;end;constructor TtableThread.Create(t: Ttable);
begin
  inherited Create(True);        // create suspended thread
  Ftable := t;                   // set parameters
  FreeOnTerminate := True;
  Resume;                        // thread that puppy!
end;procedure TtableThread.Execute;
begin
  try  except
    FtableException := ExceptObject as Exception;
    Synchronize(tableError);     // show exception from main thread
  end;
end;
procedure TtableThread.tableError;
begin
  Application.ShowException(ftableException);
end;//---------以下为取得数据插入到数据库,是关键,里面的判断出错还需要根据条件----------
//---------变化更改,对于警告值需要改,看使用情况-------------------------procedure getdata(ncount:integer;s:string);var
i,insertsecond:integer;
 Hour, Minute, Second, sSecond : word;
begin
   with form3 do   with table1 do    begin
    TtableThread.Create(table1);
    active:=true;
    last;
    for i := 0 to nCount-1  do
    begin
       insert;            fieldbyname('time').Value:= formatdatetime('hh:nn:ss',now);            DecodeTime( now, Hour, Minute, Second, sSecond );
            insertsecond:=hour*3600+minute*60+second;
            fieldbyname('second').Value:=insertsecond;            FieldByName('cs_id').value := Copy(s,i*26+1,11);         begin
            if  (copy(s,i*26+13,7)='    -- ') or (copy(s,i*26+13,7)='       ') or  (copy(s,i*26+13,7)=',    --')or  (copy(s,i*26+13,7)=',  0.00')  then
               FieldByname('tb').Value :=0
            else
               FieldByname('tb').AsString := copy(s,i*26+13,7);
            end;
               begin
               if  ((Copy(s,i*26+21,4)='  --')  or (Copy(s,i*26+21,4)=' -- ')or (Copy(s,i*26+21,4)=' >85'))  then
               FieldByname('cq').Value :=0
               else
               FieldByname('cq').AsString := Copy(s,i*26+21,4);
               end;
               end;
       post;
       end;
     end;
end.

解决方案 »

  1.   

    unit Unit3;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, DB, ADODB, DBTables;type
        TForm3 = class(TForm)
        Button1: TButton;
        Session1: TSession;
        Database1: TDatabase;
        Table1: TTable;
        Memo1: TMemo;
        Label1: TLabel;
      private
        { Private declarations }
      public
        { Public declarations }
      end;  procedure getdata(ncount:integer;s:string);var
      Form3: TForm3;implementation{$R *.dfm}type
         TtableThread = class(TThread)
      private
         Ftable:ttable;
         FtableException: Exception;
         procedure tableError;protected
        procedure Execute; override;
    public
        constructor Create(t: Ttable); virtual;end;constructor TtableThread.Create(t: Ttable);
    begin
      inherited Create(True);        // create suspended thread
      Ftable := t;                   // set parameters
      FreeOnTerminate := True;
      Resume;                        // thread that puppy!
    end;procedure TtableThread.Execute;
    begin
      try  except
        FtableException := ExceptObject as Exception;
        Synchronize(tableError);     // show exception from main thread
      end;
    end;
    procedure TtableThread.tableError;
    begin
      Application.ShowException(ftableException);
    end;//---------以下为取得数据插入到数据库,是关键,里面的判断出错还需要根据条件----------
    //---------变化更改,对于警告值需要改,看使用情况-------------------------procedure getdata(ncount:integer;s:string);var
    i,insertsecond:integer;
     Hour, Minute, Second, sSecond : word;
    begin
       with form3 do   with table1 do    begin
        TtableThread.Create(table1);
        active:=true;
        last;
        for i := 0 to nCount-1  do
        begin
           insert;            fieldbyname('time').Value:= formatdatetime('hh:nn:ss',now);            DecodeTime( now, Hour, Minute, Second, sSecond );
                insertsecond:=hour*3600+minute*60+second;
                fieldbyname('second').Value:=insertsecond;            FieldByName('cs_id').value := Copy(s,i*26+1,11);         begin
                if  (copy(s,i*26+13,7)='    -- ') or (copy(s,i*26+13,7)='       ') or  (copy(s,i*26+13,7)=',    --')or  (copy(s,i*26+13,7)=',  0.00')  then
                   FieldByname('tb').Value :=0
                else
                   FieldByname('tb').AsString := copy(s,i*26+13,7);
                end;
                   begin
                   if  ((Copy(s,i*26+21,4)='  --')  or (Copy(s,i*26+21,4)=' -- ')or (Copy(s,i*26+21,4)=' >85'))  then
                   FieldByname('cq').Value :=0
                   else
                   FieldByname('cq').AsString := Copy(s,i*26+21,4);
                   end;
                   end;
           post;
           end;
         end;
    end.
      

  2.   

    什么意思啊,我的线程是不是写的有问题,执行时怎么出现SQL在执行啊,
      

  3.   

    上面的数据是设备里采集的,我想把数据采集到设备里面去,
    主要程序如下,
    with table1 do    begin
        TtableThread.Create(table1);
        active:=true;
        last;
        for i := 0 to nCount-1  do
        begin
           insert;            fieldbyname('time').Value:= formatdatetime('hh:nn:ss',now);            FieldByname('cq').AsString := Copy(s,i*26+21,4);
                FieldByname('tb').AsString := copy(s,i*26+13,7);
                FieldByname('cq').AsString := Copy(s,i*26+21,4); post;
    这四个列是ACCESS里的一张表,上面程序多了写其他的条件判断,不过主要实现
    还是这几条,关键是我的线程使用是否有错,我的速度不够快,要等待,出现SQL等待
      

  4.   

    把数据源用TClientDataSet,用线程之前先loadfromfile
    一下,你的线程不用改
    因为TClientDataSet把数据能放在内存里,你可以用它的loadfromfile功能
    就不用写代码写什么内存了,
    其它的地方不用改
    我的MAIL,[email protected]
    再有问题MAIL吧
      

  5.   

    问题搞定没有,
    我们公司上回是,实时写库是5分种,后来后内存不到1分钟了
    你用我那个办法,可能效果没那么好,
    如果FILE不行,
    你再试试别的方法一个问题两个分,
    太谢谢了,
    呵呵,我已经一个月没有挣一点分了
      

  6.   

    问题搞定没有,
    我们公司上回是,实时写库是5分种,后来后内存不到1分钟了
    你用我那个办法,可能效果没那么好,
    如果FILE不行,
    你再试试别的方法一个问题两个分,
    太谢谢了,
    呵呵,我已经一个月没有挣一点分了