0

I'm new to Delphi, and I want to connect to SQL Server and see the status of connection (connected or not?)

My code is:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Data.DB,
  Data.Win.ADODB, Vcl.StdCtrls;

type
  TBeeper = class(TThread)
  public
   function BoolToStr(Val : Boolean): String;
    procedure connect;
      class var
  adocon : TADOConnection;
  protected
    procedure Execute; override;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public

    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  T : TBeeper;
begin
 T := TBeeper.Create(True);
 T.FreeOnTerminate := True;
 T.Resume;
end;

procedure TBeeper.connect;
begin
  ShowMessage(BoolToStr(adocon.Connected));
end;

procedure TBeeper.Execute;
begin
  inherited;

  adocon := TADOConnection.Create(Application);
  adocon.Provider := 'SQLOLEDB.1';
  adocon.LoginPrompt := False;
  adocon.ConnectionString := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mydb;Data Source=.;'; 

  adocon.Open();
  Synchronize(connect);
end;

function TBeeper.BoolToStr(Val: Boolean): String;
begin
  if val = True then
     result := 'True'
  else
    result := 'False';
  end;

end.

When I run my project and click the button, nothing happens.. what's my mistake?

I am using Delphi xe6.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mak
  • 1
  • 4
    [Ok to use TADOConnection in threads](http://stackoverflow.com/questions/3266532/ok-to-use-tadoconnection-in-threads) and [When do I need to call CoInitialize() in this scenario?](http://stackoverflow.com/questions/9286600/when-do-i-need-to-call-coinitialize-in-this-scenario) – bummi Nov 01 '14 at 08:08
  • `BoolToStr` already [exists](http://docwiki.embarcadero.com/Libraries/XE6/en/System.SysUtils.BoolToStr). – whosrdaddy Nov 01 '14 at 10:11
  • Nothing happens? Not even an error message that `CoInitialize has not been called`? Are you running this in debug mode? Have you suppressed any exception messages in the IDE? – Jerry Dodge Nov 01 '14 at 14:14
  • 1
    wrap your Execute code in a try ConInitiaize(nil) Finally UnInitialize. Also, if your adoCon.Open call fails, then your Synchronize(Connect) won't get called, so rethink your showmessage idea... – John Easley Nov 01 '14 at 21:33

0 Answers0