delphi ado 动态连接数据库

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, ExtCtrls;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    Button1: TButton;
    Edit1: TEdit;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    user: TEdit;
    pass: TEdit;
    database: TEdit;
    port: TEdit;
    rlogin: TButton;
    Button2: TButton;
    Label5: TLabel;
    ip: TEdit;
    top: TPanel;
    procedure dbconn();
    procedure mshow(str:string);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure rloginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.dbconn;
begin
try
adoconnection1.Connected:=false;
adoconnection1.Close;
adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID='+user.Text+';Initial Catalog='+database.Text+';Data Source='+ip.Text+','+port.Text+';password='+pass.Text;
adoconnection1.Connected:=true;
rlogin.Enabled:=true;
except
rlogin.Enabled:=false;
mshow('连接数据库出现错误!!');
end;
end;
procedure TForm1.mshow(str:string);
begin
showmessage(str);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
mshow(Edit1.text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
DBconn();
end;
procedure TForm1.rloginClick(Sender: TObject);
begin
try
adoconnection1.Connected:=false;
adoconnection1.Close;
rlogin.Enabled:=false;
except
showmessage('断开数据库连接时出现错误!!');
end;
end;

end.

你可能感兴趣的:(delphi ado 动态连接数据库)