procedure TForm1.ApplicationEvents1Exception(Sender: TObject; E: Exception); var I: integer; begin //请执行如下命令或者其他方法强制产生数据库连接断开情况,以触发如下异常。 //net stop MsSqlServer //net start MsSqlServer if (E is EOleException) and ((E as EOleException).ErrorCode= -2147467259) then begin ADOConnection1.Connected := False; try ADOConnection1.Connected := True; except On E2: Exception do begin MessageDlg('重连数据库发生错误:'#13 + E2.Message, mtError, [mbOK], 0); end; end; end; end;
procedure TForm1.Button1Click(Sender: TObject); var sSQL: string; begin sSQL:= 'Provider=SQLOLEDB.1;Password=YourPassword;Persist Security Info=True;' + 'User ID=sa;Initial Catalog=YourDatabase;Data Source=.';
with ADOConnection1 do begin LoginPrompt:= false; Connected:= false; ConnectionString:= sSQL; Connected:= true; end; ShowMessage('ok'); end;
procedure TForm1.Button2Click(Sender: TObject); begin with ADOQuery1 do begin Close; SQL.Clear; SQL.Add('select * from Test'); Open; end; end;