Ajout / suppression odbc sous delphi

Description

Une personne m'a demandé comment gerer une liaison OBDC sous Delphi. J'ai trouvé sur le net un progamme qui me semble bien marcher.

Bonne prog.

Source / Exemple :


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ButtonCreation: TButton;
    Button2: TButton;
    Panel1: TPanel;
    EditDriver: TEdit;
    EditNomDSN: TEdit;
    EditDescription: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    EditBDD: TEdit;
    Label4: TLabel;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Label5: TLabel;
    procedure ButtonCreationClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }

  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
ODBC_ADD_DSN = 1 ;
ODBC_CONFIG_DSN = 2  ;
ODBC_REMOVE_DSN = 3  ;
ODBC_ADD_SYS_DSN = 4  ;
vbAPINull : hwnd= 0   ;

function SQLConfigDataSource (hwndParent : hwnd; fRequest : Longint;  lpszDriver : AnsiString; lpszAttributes : AnsiString)  : Longint ; stdcall;  external 'ODBCCP32.DLL' name  'SQLConfigDataSource' ;

function CreeDSNAvecBoite(Driver,NomDSN,Description,FichierBaseDeDonnees:string):integer;
var
  strDriver : AnsiString ;
  strAttributes: AnsiString  ;
begin
  strDriver := Driver ;
  strAttributes := 'DSN='+NomDSN+ #0  ;
  strAttributes := strAttributes + 'DESCRIPTION='+Description + #0  ;
  strAttributes := strAttributes + 'DBQ='+FichierBaseDeDonnees + #0 ;
  Result :=SQLConfigDataSource(Form1.Handle  , ODBC_ADD_DSN, strDriver, strAttributes) ;
end;

function CreeDSN(Driver,NomDSN,Description,FichierBaseDeDonnees:string):integer;
var
  strDriver : AnsiString ;
  strAttributes: AnsiString  ;
begin
  strDriver := Driver ;
  strAttributes := 'DSN='+NomDSN+ #0  ;
  strAttributes := strAttributes + 'DESCRIPTION='+Description + #0  ;
  strAttributes := strAttributes + 'DBQ='+FichierBaseDeDonnees + #0 ;
  {Pour visualiser la boite de dialogue, utiliser Form1.Handle au lieu
  de vbAPINull.}
  Result :=SQLConfigDataSource(vbAPINull  , ODBC_ADD_DSN, strDriver, strAttributes) ;
end;

function DetruitDSN(Driver,NomDSN:string):integer;
var
  strDriver : AnsiString ;
  strAttributes: AnsiString  ;
begin
  strDriver := Driver ;
  strAttributes := 'DSN='+NomDSN + #0  ;
  result := SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, strDriver, strAttributes) ;
End;

procedure TForm1.ButtonCreationClick(Sender: TObject);
begin
if CreeDSN(EditDriver.Text,EditNomDSN.Text,EditDescription.Text,EditBDD.Text)
   <>0 then
     Showmessage ( 'DSN Créé')
       Else
          Showmessage( 'Echec de création du DSN') ;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
      If DetruitDSN(EditDriver.Text,EditNomDSN.Text) <> 0 Then
         Showmessage ('DSN supprimé') 
      Else
         Showmessage ('Echec de suppression du DSN') ;
 End;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if OpenDialog1.Execute then EditBDD.Text:=OpenDialog1.FileName;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreeDSNAvecBoite(EditDriver.Text,EditNomDSN.Text,EditDescription.Text,EditBDD.Text);
end;

end.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.