Delphi:通过线程连接数据库

时间:2014-11-01 08:00:59

标签: delphi

我是Delphi的新手,我想连接到SQL Server并查看连接状态(已连接或未连接?)

我的代码是:

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.

当我运行我的项目并单击按钮时,没有任何反应..我的错误是什么?

我正在使用Delphi xe6。

0 个答案:

没有答案