到底哪里出问题了???<br><br>ype<br> Txyz = class(TThread)<br> published<br> procedure Execute; override;<br> procedure displaymemo;<br> end;<br><br>var<br> ClientMainFrm: TClientMainFrm;<br> XYZThread: Txyz;<br><br> Y_Table,B_Table,SqlString:string;<br> myCursor:tCursor;<br> myint,MyWay,Sint,Oint,LastTimeSid,ThisTimeSid,LastTimeOid,ThisTimeOid:Integer;<br> sl:TStringList;<br><br>implementation<br>//truncate table msg<br>{$R *.dfm}<br>//==============================================================================<br>procedure Txyz.displaymemo;<br>var LocalSourceid,RemoteSourceId:Integer;<br>begin<br> //Sbool:=False;Obool:=False;<br> ClientMainFrm.stat1.Panels.Items[0].Text:='正在同步...';<br> ClientMainFrm.mmoRunTxt.Lines.Add('正在同步...');<br> ClientMainFrm.mmoRunTxt.Clear;<br> myCursor:=ClientMainFrm.Cursor;<br> ClientMainFrm.Cursor:=crHourGlass;<br> ClientMainFrm.tmr1.Enabled:=False;<br> ClientMainFrm.btnSet.Enabled:=False;<br><br> RemoteSourceId := ClientMainFrm.myini.ReadInteger('Source','远程source',0);<br> LocalSourceid := ClientMainFrm.myini.ReadInteger('Source','本地source',0);<br> Coinitialize(nil);<br> case MyWay of<br> 0:ClientMainFrm.InstRemote(RemoteSourceId,LocalSourceid); //同步到远程<br> 1:ClientMainFrm.InstLocal(RemoteSourceId,LocalSourceid); //同步到本地<br> 2:begin<br> ClientMainFrm.mmoRunTxt.Lines.add('双向同步:');<br> ClientMainFrm.mmoRunTxt.Lines.add('');<br> ClientMainFrm.InstLocal(RemoteSourceId,LocalSourceid);//先执行'同步到本地‘<br> ClientMainFrm.InstRemote(RemoteSourceId,LocalSourceid);//先执行'同步到远程<br> end;<br> end;<br> CoUnInitialize;<br><br>end;<br>//==============================================================================<br>procedure Txyz.Execute;<br>begin<br> while not Terminated do<br> begin<br> try<br> displaymemo;<br> ClientMainFrm.ThreadFree;<br> except<br> sl.Add(DateTimeToStr(Now)+'--线程错误');<br> sl.SaveToFile('c:/测试.txt');<br> end;<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.InstLocal(const ARemoteSourceId,ALocalSourceid:integer);<br>begin<br> try<br> ThisTimeOid:=TopConst(con1,y_Table);<br> Oint:= ThisTimeOid - LastTimeOid;<br> LastTimeOid:=ThistimeOid;<br> if Oint>0 then<br> if OpenLongDistanceData(y_Table,Oint) then<br> InPhaseUpdate(qry1,qry2,b_Table,'同步到本地:',ARemoteSourceId,ALocalSourceid,True);<br> except<br> sl.Add(DateTimeToStr(Now)+'--InstRemote错误');<br> sl.SaveToFile('c:/测试.txt');<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.InstRemote(const ARemoteSourceId,ALocalSourceid:integer);<br>begin<br> try<br> ThisTimeSid:=TopConst(con2,B_Table);<br> sint:= ThisTimeSid - LastTimeSid;<br> LastTimeSid:=ThisTimeSid;<br> if Sint>0 then<br> if OpenData(B_Table,Sint) then<br> InPhaseUpdate(qry2,qry1,Y_Table,'同步到远程:',ALocalSourceid,ARemoteSourceId,False);<br> except<br> sl.Add(DateTimeToStr(Now)+'--InstLocal错误');<br> sl.SaveToFile('c:/测试.txt');<br> end;<br>end;<br>//==============================================================================<br>function TClientMainFrm.TopConst(Acon:TADOConnection;ATable:string):Integer;<br>var QryTmp:TADOQuery;<br>begin<br> qrytmp:=TADOQuery.Create(nil);<br> try<br> qrytmp.Connection:=Acon;<br> qrytmp.Close;<br> qrytmp.SQL.Clear;<br> qrytmp.SQL.Add('select top 1 id from '+ATable+' order by id desc');<br> try<br> QryTmp.Open;<br> Result:=StrToInt(QryTmp.Fields[0].Text);<br> except<br> Result:=0;<br> end;<br> finally<br> qrytmp.Close;<br> QryTmp.Free;<br> end;<br>end;<br>//==============================================================================<br>function TClientMainFrm.RunSql(Aqry:TADOQuery;Atext:string;i:Integer; Abool:Boolean):Boolean;<br>var t:string;<br>begin<br> if Abool then<br> begin<br> With AQry Do<br> Begin<br> Close;<br> SQL.Clear ;<br> SQL.Add('EXECUTE insttext :s1, :s2, :s3, :s4, :s5 Output ') ;<br> Parameters.Items[0].Value := strFields1;<br> Parameters.Items[1].Value := strFields2;<br> Parameters.Items[2].Value := strFields3;<br> Parameters.Items[3].Value := strFields4;<br> try<br> Prepared;<br> ExecSQL;<br> T := Parameters.Items[4].Value;<br> if t='0' then Result:=True<br> else Result:=False;<br> Close;<br> except<br> Result:=False;<br> Close;<br> end;<br> end;<br> end else<br> begin<br> With AQry Do<br> Begin<br> Close;<br> SQL.Clear ;<br> SQL.Add(Atext);<br> try<br> if i=1 then Open<br> else ExecSQL;<br> Result:=True<br> except<br> Result:=False;<br> Close;<br> end;<br> end;<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.ThreadFree;<br>begin<br><br> stat1.Panels.Items[0].Text:='';<br> Cursor:=myCursor;<br> tmr1.Enabled:=True;<br> pb1.Position:=0;<br> Sqltxt:='';<br> strFields1:='';<br> strFields2:='';<br> strFields3:='';<br> strFields4:='';<br> Threadbool:=False;<br> XYZThread.Terminate;<br> qry1.Close;<br> qry2.Close;<br> XYZThread.WaitFor;<br> XYZThread.Free;<br> XYZThread:=nil;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.InPhaseUpdate(ASqry,AOqry:TADOQuery;ATable,AstrTxt:string;<br> ASSource,AOSource:Integer;Abool:Boolean);<br>var mSource,nSource,Stmp:string;i:Integer;<br>begin<br> mmoRunTxt.Lines.Add('');<br> mmoRunTxt.Lines.Add(AstrTxt);<br> mmoRunTxt.Lines.Add('');<br><br> mSource:=inttostr(ASSource);<br> pb1.Position:=0;<br> pb1.Max:=ASqry.RecordCount-1;<br> ASqry.Last;<br> while not ASqry.Bof do<br> begin<br> try<br> pb1.Position:=pb1.Position+1;<br> nSource:=Trim(Asqry.Fields.Fields[4].Text);<br> if nSource <> mSource then<br> begin<br> strFields1:=Asqry.Fields.Fields[1].Text;<br> strFields2:=Asqry.Fields.Fields[2].Text;<br> strFields3:=Asqry.Fields.Fields[3].Text;<br> strFields4:=inttostr(AOSource);<br> //------------------------------<br> Sqltxt:='INSERT INTO ' + ATable +' (msg,tel,code,source) VALUES (';<br> Sqltxt:=Sqltxt+'N'''+Asqry.Fields.Fields[1].Text+''',';<br> Sqltxt:=Sqltxt+'N'''+ASqry.Fields.Fields[2].Text+''',';<br> Sqltxt:=Sqltxt+'N'''+ASqry.Fields.Fields[3].Text+''',';<br> Sqltxt:=Sqltxt+'N'''+inttostr(AOSource)+''')';<br> //------------------------------<br> Stmp:=' 插入记录:[ (源库ID:'+Asqry.Fields.Fields[0].Text+') ';<br> Stmp:=Stmp+Asqry.Fields.Fields[1].Text+',';<br> Stmp:=Stmp+Asqry.Fields.Fields[2].Text+',';<br> Stmp:=Stmp+Asqry.Fields.Fields[3].Text+',';<br> Stmp:=Stmp+inttostr(AOSource)+']';<br> //-------------------------------<br> if RunSql(AOqry,Sqltxt,0,Abool) then<br> mmoRunTxt.Lines.Add(Stmp);<br> //-------------------------------<br> for i:=1 to 1000 do ;<br> //Application.ProcessMessages;<br> strFields1:='';<br> strFields2:='';<br> strFields3:='';<br> strFields4:='';<br> Sqltxt:='';<br> end;<br> except<br> end;<br> ASqry.Prior;<br> end;<br> ASqry.Close;<br> //MsgBox('ok');<br> pb1.Position:=0;<br>end;<br>//==============================================================================<br>function TClientMainFrm.OpenLongDistanceData(ATable:string;ATopVal:Integer):Boolean;<br>begin //id,msgtime,<br> Sqltxt:='SELECT top '+inttostr(ATopVal)+'id,msg,tel,code,source FROM '+ATable;<br> Sqltxt:=Sqltxt + ' order by id desc';<br><br> if not RunSql(qry1,Sqltxt,1,False) then<br> begin<br> mmoRunTxt.Lines.Add('远程数据表打开失败。');<br> cltrycnTrayIcon.Hint:='数据同步器';<br> Result:=False;<br> end else<br> Result:=True;<br>end;<br>//==============================================================================<br>function TClientMainFrm.OpenData(ATable:string;ATopVal:Integer):Boolean;<br>begin<br> Sqltxt:='SELECT top '+inttostr(ATopVal)+'id,msg,tel,code,source FROM '+ATable;<br> Sqltxt:=Sqltxt + ' order by id desc';<br><br> if not RunSql(qry2,Sqltxt,1,False) then<br> begin<br> mmoRunTxt.Lines.Add('本地数据表打开失败。');<br> cltrycnTrayIcon.Hint:='数据同步器';<br> Result:=False;<br> end else<br> Result:=True;<br>end;<br>//==============================================================================<br>function TClientMainFrm.LinkServer:Boolean;<br>begin<br> Result:=False;<br> if FileExists(StrUdl) then<br> begin<br> try<br> con2.Close;<br> con2.ConnectionString:='FILE NAME='+StrUdl;<br> con2.Open;<br> Result:=True;<br> except<br> mmoRunTxt.Lines.Add('本地数据库链接失败。');<br> cltrycnTrayIcon.Hint:='数据同步器';<br> end;<br> end;<br>end;<br>//==============================================================================<br>function TClientMainFrm.LinkLongDistanceServer:Boolean;<br>var sqlip,sqlUser,sqlkey,sqlName:string;<br>begin<br> sqlip := Trim(myini.ReadString('SET','SqlIp',''));<br> sqlName := Trim(myini.ReadString('SET','SqlName',''));<br> sqlUser := Trim(myini.ReadString('SET','SqlUser',''));<br> sqlkey := Trim(myini.ReadString('SET','Sqlkey',''));<br> Y_Table := Trim(myini.ReadString('TABLE','远程表',''));<br> B_Table := Trim(myini.ReadString('TABLE','本地表',''));<br><br> SqlString := 'Provider=SQLOLEDB.1;Password='+sqlkey +';Persist Security Info=True;';<br> SqlString := SqlString+'User ID='+sqlUser+';';<br> SqlString := SqlString+'Initial Catalog='+sqlName+';';<br> SqlString := SqlString+'Data Source='+ sqlip;<br> try<br> con1.Close;<br> con1.ConnectionString:=SqlString;<br> con1.Open;<br> Result := True;<br> except<br> mmoRunTxt.Lines.Add('远程数据库链接失败。');<br> cltrycnTrayIcon.Hint:='数据同步器';<br> Result:=False;<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.SetFrmShow;<br>var SetFrm: TSetFrm;<br>begin<br> tmr1.Enabled:=False;<br> SetFrm:= TSetFrm.Create(nil);<br> try<br> SetFrm.ShowModal;<br> if SetFrm.SetBool then<br> begin<br> LastTimeSid:=0;<br> LastTimeOid:=0;<br> end;<br> finally<br> SetFrm.Free;<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.FormCreate(Sender: TObject);<br>var mbool:Boolean;<br>begin<br>// mbool:=False;<br> Myint := 0;<br> Sint := 0;<br> oint := 0;<br> LastTimeSid := 0;<br> LastTimeOid := 0;<br> LocalSourceid := 0;<br> RemoteSourceId := 0;<br> strFields1 := '';<br> strFields2 := '';<br> strFields3 := '';<br> strFields4 := '';<br> Sqltxt := '';<br> Threadbool := False;<br> sl:=TStringList.Create;;<br> sl.Clear;<br> //--------------------------------<br> StrUdl:= ExtractFilePath(Application.ExeName)+'nclient.udl';<br> inifiles:= ExtractFilePath(Application.ExeName)+'NClient.ini';<br> myini:=tinifile.Create(inifiles);<br> if not FileExists(StrUdl) then<br> begin<br> ErrBox('文件[nclient.udl]丢失,程序无法启动');<br> Application.Terminate;<br> end;<br> if not FileExists(inifiles) then SetFrmShow;<br> MyInterval := myini.ReadInteger('TimeWay','time',0)*1000;<br> MyWay := myini.ReadInteger('TimeWay','Way',0);<br> tmr1.Interval := MyInterval;<br> mbool:=myini.ReadBool('Automatic','auto',False);<br> if mbool then<br> btnStart.Click;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btnExtClick(Sender: TObject);<br>begin<br> Close;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btnSetClick(Sender: TObject);<br>begin<br> SetFrmShow;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.tmr1Timer(Sender: TObject);<br>begin<br> try<br> Threadbool:=True;<br> XYZThread := Txyz.Create(False);<br> except<br> sl.Add(DateTimeToStr(Now)+'--创建线程时错误');<br> sl.SaveToFile('c:/测试.txt');<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.N1Click(Sender: TObject);<br>begin<br> cltrycnTrayIcon.ShowMainForm;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btnStartClick(Sender: TObject);<br>begin<br> btnStart.Enabled:=False;<br> try<br> if Myint=0 then<br> begin<br> if ClientMainFrm.LinkLongDistanceServer and ClientMainFrm.LinkServer then<br> begin<br> myint:=1;<br> stat1.Panels.Items[0].Text:='';<br> mmoRunTxt.Lines.Clear;<br> //----------------------------------//<br> ThisTimeSid:=TopConst(con2,B_Table);<br> LastTimeSid:=ThisTimeSid;<br> ThisTimeOid:=TopConst(con1,y_Table);<br> LastTimeOid:=ThisTimeOid;<br> end;<br> end;<br> if myint=1 then<br> begin<br> if btnStart.Caption='开始' then<br> begin<br> cltrycnTrayIcon.Hint:='数据同步器-正在同步';<br> btnStart.Caption:='暂停';<br> //stat1.Panels.Items[0].Text:='';<br> btnSet.Enabled:=False;<br> tmr1.Enabled:=True;<br> end else<br> begin<br> if btnStart.Caption='暂停' then<br> begin<br> if Threadbool then XYZThread.Suspend<br> else tmr1.Enabled:=False;<br> stat1.Panels.Items[0].Text:='暂停...';<br> btnStart.Caption:='继续';<br> end else<br> begin<br> if Threadbool then XYZThread.Resume<br> else tmr1.Enabled:=True;<br> btnStart.Caption:='暂停';<br> stat1.Panels.Items[0].Text:='';<br> end;<br> end;<br> end;<br> finally<br> btnStart.Enabled:=True;<br> end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btn3Click(Sender: TObject);<br>begin<br> if Threadbool then ThreadFree;<br> cltrycnTrayIcon.Hint:='数据同步器';<br> stat1.Panels.Items[0].Text:='';<br> if tmr1.Enabled then tmr1.Enabled:=False;<br> if btnStart.Caption<>'开始' then btnStart.Caption:='开始';<br> btnSet.Enabled:=True;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.FormCloseQuery(Sender: TObject;<br> var CanClose: Boolean);<br>begin<br> if not YesNoBox('真的要退出?') then<br> CanClose := False<br> else begin<br> try<br> btn3.Click;<br> myini.Free;<br> sl.Free;<br> except<br> end;<br> end;<br>end;<br>//==============================================================================<br>end.