连接失败,自动重试的实现?(100分)

  • 主题发起人 主题发起人 清越小生
  • 开始时间 开始时间

清越小生

Unregistered / Unconfirmed
GUEST, unregistred user!
程序开始时进行数据库的连接,在程序进行数据库操作过程中出现数据库连接失败,怎么样实现自动进行重新连接,而不显示“连接失败”,当重试次数达到一定数量时才进行提示呢?请高手赐教!
 
var<br>&nbsp; bConnOk : boolean;<br><br><br>bConnOk := false;<br>for i := 0 to retryCount -1 do<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; Conn.Connect;<br>&nbsp; &nbsp; bConnOk := true;<br>&nbsp; &nbsp; Break;<br>&nbsp; except<br>&nbsp; end;<br>end;<br><br>&nbsp; if not bConnOk then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox('连接数据库失败!');<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;
 
新年同樂啊。。。。。
 
楼上的朋友,这个我知道,我的问题是在已经连接成功的情况下,发生了“连接断了”的时候,该怎样让其自动重新连接呢?
 
設置個時間自動刷新吧。
 
问题的关键是“连接中断”的时候系统会自动出一个提示,而不是我程序控制,我的目的就是不自动出提示,而是我自己进行重新连接!
 
在合适的地方用try块包起来。<br>这是我的一个代码,连接到sql &nbsp;server 数据库的,你自己看看吧<br>unit main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,IniFiles,bsSkinData, BusinessSkinForm, bsSkinCtrls,<br>&nbsp; ExtCtrls,<br>&nbsp; MIDASLIB, Grids, DBGrids, DB, ADODB, StdCtrls, ComCtrls, bsSkinTabs,<br>&nbsp; bsMessages, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,<br>&nbsp; cxDataStorage, cxEdit, cxDBData, cxGridLevel, cxClasses, cxControls,<br>&nbsp; cxGridCustomView, cxGridCustomTableView, cxGridTableView,<br>&nbsp; cxGridDBTableView, cxGrid, bsSkinBoxCtrls, Buttons;<br><br>type<br>&nbsp; TFrmMain = class(TForm)<br>&nbsp; &nbsp; bsBusinessSkinForm1: TbsBusinessSkinForm;<br>&nbsp; &nbsp; bsSkinData1: TbsSkinData;<br>&nbsp; &nbsp; bsCompressedStoredSkin1: TbsCompressedStoredSkin;<br>&nbsp; &nbsp; bsSkinStatusBar1: TbsSkinStatusBar;<br>&nbsp; &nbsp; bsSkinStatusPanel1: TbsSkinStatusPanel;<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; bsSkinPanel1: TbsSkinPanel;<br>&nbsp; &nbsp; bsSkinButton1: TbsSkinButton;<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; bsSkinPageControl1: TbsSkinPageControl;<br>&nbsp; &nbsp; bsSkinTabSheet1: TbsSkinTabSheet;<br>&nbsp; &nbsp; bsSkinTabSheet2: TbsSkinTabSheet;<br>&nbsp; &nbsp; bsSkinTabSheet3: TbsSkinTabSheet;<br>&nbsp; &nbsp; bsSkinButton2: TbsSkinButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; ADOConnection1: TADOConnection;<br>&nbsp; &nbsp; bsSkinMessage1: TbsSkinMessage;<br>&nbsp; &nbsp; btnOpenDB: TbsSkinButton;<br>&nbsp; &nbsp; ADO_TuserOnline: TADOQuery;<br>&nbsp; &nbsp; dsADO_TuserOnline: TDataSource;<br>&nbsp; &nbsp; cxGrid1DBTableView1: TcxGridDBTableView;<br>&nbsp; &nbsp; cxGrid1Level1: TcxGridLevel;<br>&nbsp; &nbsp; cxGrid1: TcxGrid;<br>&nbsp; &nbsp; bsSkinCheckRadioBox1: TbsSkinCheckRadioBox;<br>&nbsp; &nbsp; bsSkinSpinEdit1: TbsSkinSpinEdit;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; bsSkinButton3: TbsSkinButton;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserID: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserLoginTime: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserUpdateTime: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserOnlineTime: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserEvent: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserMCode: TcxGridDBColumn;<br>&nbsp; &nbsp; ADOQ_TUserLogs: TADOQuery;<br>&nbsp; &nbsp; dsADOQ_TUserLogs: TDataSource;<br>&nbsp; &nbsp; cxGrid2DBTableView1: TcxGridDBTableView;<br>&nbsp; &nbsp; cxGrid2Level1: TcxGridLevel;<br>&nbsp; &nbsp; cxGrid2: TcxGrid;<br>&nbsp; &nbsp; cxGrid2DBTableView1UserID: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid2DBTableView1UserLogEvent: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid2DBTableView1UserLoginDate: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid2DBTableView1UserMCode: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid2DBTableView1UserIP: TcxGridDBColumn;<br>&nbsp; &nbsp; bsSkinCheckRadioBox2: TbsSkinCheckRadioBox;<br>&nbsp; &nbsp; bsSkinSpinEdit2: TbsSkinSpinEdit;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; bsSkinButton4: TbsSkinButton;<br>&nbsp; &nbsp; bsSkinButton5: TbsSkinButton;<br>&nbsp; &nbsp; cxGrid1DBTableView1UserName: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid1DBTableView1TypeName: TcxGridDBColumn;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserType: TSmallintField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserPass: TStringField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserName: TStringField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserID: TStringField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserLogEvent: TStringField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserLoginDate: TDateTimeField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserMCode: TStringField;<br>&nbsp; &nbsp; ADOQ_TUserLogsUserIP: TStringField;<br>&nbsp; &nbsp; ADOQ_TUserLogsTypeName: TStringField;<br>&nbsp; &nbsp; cxGrid2DBTableView1UserName: TcxGridDBColumn;<br>&nbsp; &nbsp; cxGrid2DBTableView1TypeName: TcxGridDBColumn;<br>&nbsp; &nbsp; bsSkinCheckRadioBox3: TbsSkinCheckRadioBox;<br>&nbsp; &nbsp; bsSkinSpinEdit3: TbsSkinSpinEdit;<br>&nbsp; &nbsp; Label4: TLabel;<br>&nbsp; &nbsp; Label6: TLabel;<br>&nbsp; &nbsp; Label7: TLabel;<br>&nbsp; &nbsp; Label8: TLabel;<br>&nbsp; &nbsp; Label9: TLabel;<br>&nbsp; &nbsp; bsSkinGroupBox1: TbsSkinGroupBox;<br>&nbsp; &nbsp; ADO_TuserOnlineID: TAutoIncField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserID: TStringField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserLoginTime: TDateTimeField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserUpdateTime: TDateTimeField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserOnlineTime: TIntegerField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserEvent: TStringField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserMCode: TStringField;<br>&nbsp; &nbsp; ADO_TuserOnlineTypeName: TStringField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserName: TStringField;<br>&nbsp; &nbsp; ADO_TuserOnlineUserType: TSmallintField;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure bsSkinButton1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure ADOConnection1AfterConnect(Sender: TObject);<br>&nbsp; &nbsp; procedure ADOConnection1AfterDisconnect(Sender: TObject);<br>&nbsp; &nbsp; procedure btnOpenDBClick(Sender: TObject);<br>&nbsp; &nbsp; procedure bsSkinButton3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure bsSkinButton4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure bsSkinButton5Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure bsSkinButton2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; function &nbsp;SetServerINFO:string;//读取服务器信息并<br>&nbsp; &nbsp; procedure ConnectIn(i:integer);<br>&nbsp; &nbsp; procedure OpenDB(COnStr:string;ShowMSG:boolean);// 连接到数据库服务器<br>&nbsp; &nbsp; procedure SVRUpdateOnlineInfo(lUpDateTimes:integer);//服务器定时更新用户在线信息<br><br>&nbsp; end;<br><br>var<br>&nbsp; FrmMain: TFrmMain;<br>&nbsp; SerIP:string;//服务器IP<br>&nbsp; ConnStr:string;//连接串<br>&nbsp; listened:boolean;//是否处在监听状态<br>&nbsp; ConSum:longInt;//总连接数<br>&nbsp; DBConnected:boolean;//数据库服务器是否可连接<br>&nbsp; UpDateTimes:integer=100;//客户端超过100秒没有更新,说明已经非正常掉线,删除其在线信息。<br>&nbsp; UpDateTimesCount:integer;//自动刷新计时器<br>&nbsp; UpdateOnlineTimes:integer;//用户在线信息自动刷新计时器<br>&nbsp; UpdateUserLogsTimes:integer;//用户登录信息自动刷新计时器<br>&nbsp; ConnDisconnected:boolean;//数据库非正常断开<br>&nbsp; ConnTimes:integer;//连接数据库时间的计时器<br>implementation<br>&nbsp;uses &nbsp;SetINI,u_Const;<br>{$R *.dfm}<br>procedure TFrmMain.SVRUpdateOnlineInfo(lUpDateTimes:integer);//服务器定时更新用户在线信息<br>&nbsp; &nbsp; //*@UserID VARCHAR(16),<br>&nbsp; &nbsp; //@UserEvent VARCHAR(100),<br>&nbsp; &nbsp; //@UserMCode VARCHAR(50)<br>var<br>&nbsp; ADOSPTemp: TADOStoredProc;<br>begin<br>&nbsp; ADOSPTemp:=TADOStoredProc.Create(self);<br>&nbsp; ADOSPTemp.Connection :=ADOConnection1;<br>&nbsp; ADOSPTemp.Parameters.Clear;<br>&nbsp; try<br>&nbsp; &nbsp; with ADOSPTemp do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;Parameters.Clear;<br>&nbsp; &nbsp; &nbsp;ProcedureName:='U_SVRUpdateOnlineInfo';<br>&nbsp; &nbsp; &nbsp;Parameters.CreateParameter('@UpDateTimes',ftInteger,pdInput,6,lUpDateTimes);<br><br>&nbsp; &nbsp; &nbsp;Parameters.ParamByName('@UpDateTimes').Value:=lUpDateTimes;<br>&nbsp; &nbsp; &nbsp;ExecProc;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; ADOSPTemp.Free;<br>&nbsp; end;<br>end;<br>procedure TFrmMain.openDB(ConStr:string;ShowMSG:boolean);// 连接到数据库服务器<br>begin<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp;if ADOQ_TUserLogs.Active then<br>&nbsp; &nbsp; &nbsp; &nbsp; ADOQ_TUserLogs.Close;<br>&nbsp; &nbsp; &nbsp;if ADO_TuserOnline.Active then<br>&nbsp; &nbsp; &nbsp; &nbsp; ADO_TuserOnline.Close;<br>&nbsp; &nbsp; &nbsp;if ADOConnection1.Connected then<br>&nbsp; &nbsp; &nbsp; &nbsp;ADOConnection1.Close;<br>&nbsp; &nbsp; &nbsp;ADOConnection1.ConnectionString:=ConStr;<br>&nbsp; &nbsp; &nbsp;ADOConnection1.Open;<br>&nbsp; &nbsp; &nbsp;ADOQ_TUserLogs.Open;<br>&nbsp; &nbsp; &nbsp;ADO_TuserOnline.Open;<br>&nbsp; &nbsp; &nbsp;ConnDisconnected:=false;<br>&nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp;ConnDisconnected:=true;<br>&nbsp; &nbsp; &nbsp;ConnTimes:=0;<br>&nbsp; &nbsp; &nbsp;UpdateOnlineTimes:=0;<br>&nbsp; &nbsp; &nbsp;UpdateUserLogsTimes:=0;<br>&nbsp; &nbsp; &nbsp;DBConnected:=false;<br>&nbsp; &nbsp; &nbsp;if ShowMSG then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;timer1.Enabled:=false;<br>&nbsp; &nbsp; &nbsp;bsSkinMessage1.MessageDlg('连接到数据库服务器失败'#13'可能的原因:'#13'1、网络故障,'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'请检查网络后重新再试!'#13'2、你设置的帐号没有登录权限,'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'请向你们的管理员咨询。'#13'3、数据库服务器没有运行,请向系统管理员咨询',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(mtinformation),[mbOK],0);<br>&nbsp; &nbsp; &nbsp;timer1.Enabled:=true;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;end;<br>end;<br>procedure TFrmMain.ConnectIn(i:integer);<br>var<br>&nbsp;p:TUser;<br>begin<br>&nbsp; ConSum:=ConSum+(i);<br>&nbsp; showmessage(p.SysAut);<br>end;<br><br>function &nbsp;TFrmMain.SetServerINFO:string;//读取服务器信息并<br>var<br>&nbsp; ServerINI:string;<br>&nbsp; ini:tinifile;<br>&nbsp; ConStr:string;<br>&nbsp; ID,IP,PWD:string;<br>begin<br>&nbsp; ServerINI:=ExtractFilePath(Application.ExeName)+'Server.INI';<br>&nbsp; if not FileExists(ServerINI) then<br>&nbsp; begin<br>&nbsp; &nbsp; Showmessage('你是第一次运行本程序,请单击确定设置服务器信息!');<br>&nbsp; &nbsp; FrmSetINI:=TFrmSetINI.Create(self);<br>&nbsp; &nbsp; FrmSetINI.ShowModal;<br>&nbsp; end;<br><br>&nbsp; ini:=tinifile.Create(ServerINI);<br>&nbsp; IP:=INI.ReadString('SERVER','IP','192.168.0.1');<br>&nbsp; ID:=INI.ReadString('SERVER','UserName','sa');<br>&nbsp; PWD:=INI.ReadString('SERVER','UserPWD','system');<br>&nbsp; INI.Free;<br>&nbsp; ConStr:='Provider=SQLOLEDB.1;Password='+PWD+';Persist Security Info=True;'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'User ID='+ID+';Initial Catalog=NK;Data Source='+IP;<br><br>&nbsp; SerIP:=IP;<br>&nbsp; SetServerINFO:=ConStr;<br>end;<br><br>procedure TFrmMain.FormCreate(Sender: TObject);<br>begin<br>&nbsp; ConSum:=0;<br>&nbsp; listened:=false;<br>&nbsp; ConnStr:=SetServerINFO;<br>&nbsp; Label7.Caption:='数据库服务器地址:'+serIP;<br>&nbsp; Label6.Caption:='开始运行时间:'+DateTimeToStr(now());<br>&nbsp; Application.Title:='网上作业系统--伺服器';<br>&nbsp; ConnDisconnected:=false;<br>&nbsp; openDB(ConnStr,true);<br>&nbsp; Timer1.Enabled :=true;<br>end;<br><br>procedure TFrmMain.bsSkinButton1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp;if not DBConnected then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; bsSkinMessage1.MessageDlg('请连接到数据库服务器以后再开始监听!',(mtinformation),[mbOK],0);<br>&nbsp; &nbsp; &nbsp; Abort;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;Listened:=not(Listened);<br>&nbsp; &nbsp;if Listened then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; Label8.Caption:='伺服器状态:在2001端口监听';<br>&nbsp; &nbsp; &nbsp; bsSkinButton1.Caption:='停止监听';<br>&nbsp; &nbsp;end<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; bsSkinButton1.Caption:='开始监听';<br>&nbsp; &nbsp; &nbsp; Label8.Caption:='伺服器状态:停止监听';<br>&nbsp; &nbsp;end;<br>end;<br><br>procedure TFrmMain.ADOConnection1AfterConnect(Sender: TObject);<br>begin<br>&nbsp; &nbsp;Label9.Caption :='数据库服务器状态:已连接';<br>&nbsp; &nbsp;DBConnected:=ADOConnection1.Connected;<br>&nbsp; &nbsp;ConnDisconnected:=false;<br>end;<br><br>procedure TFrmMain.ADOConnection1AfterDisconnect(Sender: TObject);<br>begin<br>&nbsp; &nbsp;DBConnected:=ADOConnection1.Connected;<br>&nbsp; &nbsp;Label9.Caption :='数据库服务器状态:未连接';<br>end;<br><br>procedure TFrmMain.btnOpenDBClick(Sender: TObject);<br>begin<br>&nbsp; &nbsp;openDB(ConnStr,true);<br>&nbsp; &nbsp;//btnOpenDB.Enabled :=not(DBConnected);<br>end;<br><br>procedure TFrmMain.bsSkinButton3Click(Sender: TObject);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; ADO_TuserOnline.Close;<br>&nbsp; &nbsp; ADO_TuserOnline.Open;<br>&nbsp; except<br>&nbsp; &nbsp; &nbsp;ConnDisconnected:=true;<br>&nbsp; &nbsp; &nbsp;ConnTimes:=0;<br>&nbsp; &nbsp; &nbsp;Label9.Caption:='连接到数据库服务器失败,系统将在10秒后尝试重新连接';<br>&nbsp; &nbsp; &nbsp;DBConnected:=false;<br>&nbsp; end;<br>end;<br><br>procedure TFrmMain.bsSkinButton4Click(Sender: TObject);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; ADOQ_TUserLogs.Close;<br>&nbsp; &nbsp; ADOQ_TUserLogs.Open;<br>&nbsp; except<br>&nbsp; &nbsp;ConnDisconnected:=true;<br>&nbsp; &nbsp;ConnTimes:=0;<br>&nbsp; &nbsp;DBConnected:=false;<br>&nbsp; &nbsp;Label9.Caption:='连接到数据库服务器失败,系统将在10秒后尝试重新连接';<br>&nbsp; end;<br>end;<br><br>procedure TFrmMain.bsSkinButton5Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; FrmSetINI:=TFrmSetINI.Create(self);<br>&nbsp; &nbsp; FrmSetINI.btnCancel.Enabled :=true;<br>&nbsp; &nbsp; FrmSetINI.ShowModal;<br>&nbsp; &nbsp; ConnStr:=SetServerINFO;<br>&nbsp; &nbsp; openDB(ConnStr,true);<br>end;<br><br>procedure TFrmMain.Timer1Timer(Sender: TObject);<br>var<br>&nbsp;LMsg:string;<br>begin<br>&nbsp; if ConnDisconnected then<br>&nbsp; begin<br>&nbsp; &nbsp;ConnTimes:=ConnTimes+1;<br>&nbsp; &nbsp;Label9.Caption:='连接到数据库服务器失败,系统将在'+IntToStr(ConnTimes)+'秒后尝试重新连接';<br>&nbsp; &nbsp;if ConnTimes&gt;10 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;Label9.Caption:='尝试连接到数据库服务器...';<br>&nbsp; &nbsp; &nbsp;//showmessage('aa'+SerIP);<br>&nbsp; &nbsp; &nbsp;OpenDB(ConnStr,false);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;//Abort;<br>&nbsp; end;<br>&nbsp; if bsSkinCheckRadioBox3.Checked then<br>&nbsp; begin<br>&nbsp; &nbsp; if listbox1.Items.Count&gt;bsSkinSpinEdit3.Value then<br>&nbsp; &nbsp; &nbsp; &nbsp;listbox1.Items.Clear;<br>&nbsp; end;<br>&nbsp; if (DBConnected &nbsp;and bsSkinCheckRadioBox1.Checked)then //自动刷新用户在线信息<br>&nbsp; begin<br>&nbsp; &nbsp; UpdateOnlineTimes:=UpdateOnlineTimes+1;<br>&nbsp; &nbsp; if UpdateOnlineTimes&gt;StrToInt(bsSkinSpinEdit1.Text)*60 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp;ADO_TuserOnline.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp;ADO_TuserOnline.Open;<br>&nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp;ConnDisconnected:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp;ConnTimes:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp;Label9.Caption:='连接到数据库服务器失败,系统将在10秒后尝试重新连接';<br>&nbsp; &nbsp; &nbsp; &nbsp;DBConnected:=false;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; UpdateOnlineTimes:=0;<br>&nbsp; if (DBConnected &nbsp;and bsSkinCheckRadioBox2.Checked)then //自动刷新用户登录信息<br>&nbsp; begin<br>&nbsp; &nbsp; UpdateUserLogsTimes:=UpdateUserLogsTimes+1;<br>&nbsp; &nbsp; if UpdateUserLogsTimes&gt;StrToInt(bsSkinSpinEdit2.Text)*60 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; UpdateUserLogsTimes:=0;<br>&nbsp; &nbsp; &nbsp; ADOQ_TUserLogs.Close;<br>&nbsp; &nbsp; &nbsp; ADOQ_TUserLogs.Open;<br>&nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp;ConnDisconnected:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp;ConnTimes:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp;Label9.Caption:='连接到数据库服务器失败,系统将在10秒后尝试重新连接';<br>&nbsp; &nbsp; &nbsp; &nbsp;DBConnected:=false;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; UpdateUserLogsTimes:=0;<br><br>&nbsp; if not(listened and DBConnected) then<br>&nbsp; &nbsp; UpDateTimesCount:=0<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; UpDateTimesCount:=UpDateTimesCount+1;<br>&nbsp; &nbsp; if UpDateTimesCount&gt;UpDateTimes then //开始刷新<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Timer1.Enabled :=false;//停止计时器<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LMsg:=Label6.Caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Label6.Caption :='正刷新在线用户状态';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SVRUpdateOnlineInfo(UpDateTimes);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UpDateTimesCount:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADO_TuserOnline.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADO_TuserOnline.Open;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Label6.Caption :=LMsg;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Timer1.Enabled :=true;//恢复计时器<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp;end;<br><br>procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; &nbsp;if &nbsp;Listened then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;bsSkinMessage1.MessageDlg('请停止监听后再退出伺服器!',(mtinformation),[mbOK],0);<br>&nbsp; &nbsp; &nbsp;Abort;<br>&nbsp; &nbsp;end;<br>end;<br><br>procedure TFrmMain.bsSkinButton2Click(Sender: TObject);<br>begin<br>&nbsp; ListBox1.Items.Clear;<br>end;<br><br>end.
 
我上面这段代码是用在一个三层应用的伺服器上的,运行良好。
 
其实主要对数据源连接的接口重新再连接一次就可以了。<br>这个问题我以前也是这样,但现在都是通过这个方法解决的 。
 
后退
顶部