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