whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

31 Visitors Online


 
...make an ADODB Connection using OLE-Automation?
Autor: Daniel Henrique Monteiro de Carvalho (Brasil)
[ Print tip ]  

Tip Rating (7):  
     


uses
  
ComObj;

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

function DataBaseConnection_Test(bMessage: Boolean): AnsiString;
var
  
asTimeout, asUserName, asPassword, asDataSource, ConnectionString: AnsiString;
  iReturn: Integer;
  OldCursor: TCursor;
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;
  try
    
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 := '';
  finally
    
Screen.Cursor := OldCursor;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  
DataBaseConnection_Test(True);
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners