Etablir une connection ADO en utilisant OLE

uses
ComObj;
{$R *.DFM}

function OpenConnection(ConnectionString:AnsiString):Integer;
var
ADODBConnection : OleVariant;
begin
ADODBConnection := CreateOleObject('ADODB.Connection');
ADODBConnection.CursorLocation := 3; // User client
ADODBConnection.ConnectionString := ConnectionString;
try
ADODBConnection.Open;
except
Result := -1;
exit;
end;
Result := 0;
end;

function DataBaseConnection_Test(bMessage : Boolean):AnsiString;
var
asTimeout, asUserName, asPassword, asDataSource, ConnectionString : AnsiString;
iReturn : Integer;
begin
OldCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
asTimeout := '150';
asUserName := 'NT_Server';
asPassword := 'SA';
asDataSource := 'SQL Server - My DataBase';

ConnectionString := 'Data Source = ' + asDataSource + ';User ID = ' + asUserName
+ '; Password = ' + asPassword + ';Mode = Read|Write;Connect Timeout = ' + asTimeout;

iReturn := OpenConnection(ConnectionString);

if (bMessage) then
begin
if (iReturn = 0) then
Application.MessageBox('Connection OK!','Information',MB_OK)
else if (iReturn = -1) then
Application.MessageBox('Connection Error!','Error',MB_ICONERROR + MB_OK);
end;

if (iReturn = 0) then
Result := ConnectionString
else if (iReturn = -1) then
Result := '';

Screen.Cursor := OldCursor;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DataBaseConnection_Test(true);
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour20/11/2004
VisualisationVu 1766 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Base de donnees
Auteur de l'article
Communauté Delphi Communauté Delphi
connecté le 02/11/2011
0 articles dans la section
Contacter l'auteur