Componentes.Terceros.RemObj.../internal/5.0.24.615/1/Data Abstract for Delphi/Samples/Connection By User/ConnectionByUserMain.pas

188 lines
6.0 KiB
ObjectPascal

unit ConnectionByUserMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, uDAClasses, uDADriverManager, uDAEngine, uDAADODriver,
uROSessions, uROClient;
type
TConnectionByUserMainForm = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
eUserID: TEdit;
ePassword: TEdit;
Acquire1Button: TButton;
Acquire2Button: TButton;
Acquire3Button: TButton;
DAADODriver: TDAADODriver;
DADriverManager: TDADriverManager;
DAConnectionManager: TDAConnectionManager;
Acquire4Button: TButton;
ROSessionManager: TROInMemorySessionManager;
AcquireHoldButton: TButton;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Memo: TMemo;
procedure Acquire1ButtonClick(Sender: TObject);
procedure Acquire2ButtonClick(Sender: TObject);
procedure Acquire3ButtonClick(Sender: TObject);
procedure Acquire4ButtonClick(Sender: TObject);
procedure AcquireHoldButtonClick(Sender: TObject);
private
{ Private declarations }
procedure Log(Str: string);
public
{ Public declarations }
end;
var
ConnectionByUserMainForm: TConnectionByUserMainForm;
implementation
uses uDAInterfaces;
{$R *.dfm}
procedure TConnectionByUserMainForm.Acquire1ButtonClick(Sender: TObject);
var
conn: IDAConnection;
begin
try
conn := DAConnectionManager.NewConnection('ADOComplete');
finally
if (Conn <> nil) and conn.Connected then begin
Log(TButton(Sender).Caption + ': Connection successful');
log('Connection via "' + conn.ConnectionString + '"');
end
else begin
Log(TButton(Sender).Caption + ': Connection failed');
end;
end;
end;
procedure TConnectionByUserMainForm.Acquire2ButtonClick(Sender: TObject);
var
conn: IDAConnection;
begin
try
conn := DAConnectionManager.NewConnection('ADOPartial', FALSE);
conn.UserID := eUserID.Text;
conn.Password := ePassword.Text;
conn.Open;
finally
if (Conn <> nil) and conn.Connected then begin
Log(TButton(Sender).Caption + ': Connection successful');
log('Connection via "' + conn.ConnectionString + '"');
end
else begin
Log(TButton(Sender).Caption + ': Connection failed');
end;
end;
end;
procedure TConnectionByUserMainForm.Acquire3ButtonClick(Sender: TObject);
var
conn: IDAConnection;
begin
try
conn := DAConnectionManager.NewConnection('ADOPartial', TRUE, eUserID.Text, ePassword.Text);
finally
if (Conn <> nil) and conn.Connected then begin
Log(TButton(Sender).Caption + ': Connection successful');
log('Connection via "' + conn.ConnectionString + '"');
end
else begin
Log(TButton(Sender).Caption + ': Connection failed');
end;
end;
end;
procedure TConnectionByUserMainForm.Acquire4ButtonClick(Sender: TObject);
var
drv: IDADriver;
conn: IDAConnection;
begin
try
drv := DriverManager.DriverByDriverID('ADO'); // Raises exception if not found
conn := drv.NewConnection;
// If not empty strings, these will override any specific UserID, Password specified below
conn.ConnectionString := Format('Server=localhost;Database=Northwind;AuxDriver=SQLOLEDB.1;UserID=%s;Password=%s;', [eUserID.Text, ePassword.Text]);
conn.Open;
finally
if (Conn <> nil) and conn.Connected then begin
Log(TButton(Sender).Caption + ': Connection successful');
log('Connection via "' + conn.ConnectionString + '"');
end
else begin
Log(TButton(Sender).Caption + ': Connection failed');
end;
end;
end;
procedure TConnectionByUserMainForm.AcquireHoldButtonClick(Sender: TObject);
const
MySessionID: TGUID = '{2B0ABD74-465A-45A6-AAD7-837709A66DB9}';
var
session: TROSession;
conn: IDAConnection;
begin
conn := DAConnectionManager.NewConnection('ADOComplete');
with ROSessionManager do try
session := CreateSession(MySessionID);
// #1: How to store IDAConnections in RO sessions ----------------------------------------
conn._AddRef; // We increment its ref count because we want it locked by the session
session.Values['MyADOConnection'] := integer(conn);
// End of #1 -----------------------------------------------------------------------------
conn := nil; // We set it to NIL to simulate what would happen in a real RO Datamodule when the vars get out of scope
ReleaseSession(session, TRUE);
{$WARNINGS OFF}
session := nil; // Just to simulate real re-initialization
{$WARNINGS ON}
session := FindSession(MySessionID);
// #2: How to get IDAConnections from RO Sessions ----------------------------------------
try
conn := IDAConnection(pointer(integer(session.Values['MyADOConnection'])));
finally
if (Conn <> nil) and conn.Connected then begin
Log(TButton(Sender).Caption + ': Connection successful');
log('Connection via "' + conn.ConnectionString + '"');
end
else begin
Log(TButton(Sender).Caption + ': Connection failed');
end;
end;
// End of #2 -----------------------------------------------------------------------------
// #3: How to finally release the connection from the session. This should be done when you ------
// want to get rid of the connection for good...
conn.Close; // Optional. Implicit when it gets freed
conn._Release; // Removes the lock we imposed on the connection at the very beginning
// End of #3 -------------------------------------------------------------------------------------
conn := nil; // This is only useful in this example because the ref count is still held up to one by the local "conn" variable.
finally
DeleteSession(MySessionID, FALSE); // This is what the RO session manager does for you, so ignore this too
end;
end;
procedure TConnectionByUserMainForm.Log(Str: string);
begin
Memo.Lines.Add(Str);
end;
end.