IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...   Print This    All Groups  
  From the November 2008 Issue of Prestwood eMag
 
Delphi for Win32:
Connection String Builder Component for Delphi
 
Posted 13 years ago on 2/2/2007 and updated 11/7/2008
Take Away: In its IDE, Delphi makes it easy for us to generate correct connection strings for database connections that require them. Unfortunately, Delphi doesnt offer the same convenience at run-time when our customers need it most! This article offers a Prestwood exclusive, TConnectionStringBuilder, a non-visual component you can use in your applications to let your users build a connection string at program startup. You can then store that connection string, for future use, in an INI file or registry key.
 Tags: Connection , String ,

KB100448

A great believer in not reinventing the wheel, I went on an Internet search to see if somebody else had already addressed this problem.  As usual, somebody had, in the form of a function.

After testing it, I decided it would be more convenient to use as a non-visual component, so I wrote a simple wrapper around that function.

For my requirements, I needed just a single method, Execute and a single property, ConnectionString.  The idea is that you call Execute, the user is presented with the connection string wizard built into Windows and, if they click OK there, the ConnectionString property will hold the connection string.

Before we get to the code, let me take a moment to give credit where credit is due.  This function was posted by Wilfried Mestdagh, with the hope it would help many others.

Many thanks, Wilfried!  I hope my wrapping this in a component spreads your fine code even farther.

Note: Ive taken the liberty of modifying Wilfrieds function slightly such that parameter names follow the Prestwood Delphi coding convention.

unit ConnectionStringBuilder;

interface

uses
  SysUtils, Classes;

type
  TConnectionStringBuilder = class(TComponent)
  private
    { Private declarations }
    FConnectionString : string;
    function ADOConnectionString(_parentHandle: THandle; _initialString: WideString;
      out _newString: string): Boolean;
    function GetConnStr : string;
    procedure SetConnStr(const _value : string);
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
    function Execute : Boolean;
    property ConnectionString : string read GetConnStr write SetConnStr;
  end;

procedure Register;

implementation

uses OleDB, ComObj, ActiveX, Forms;

// Author: Wilfried Mestdagh
{I see always people manually building the connection string.
Wy not use the dialog that windows provide for us ? Of course
it is possible to use the PromptDataSource in ADODB, but this
give not the opportunity to see if the user has pressed OK or
Cancel, so we dont know when to save the changes. So I use this
code instead. I hope it benefit many people. Rgds, Wilfried}
function TConnectionStringBuilder.ADOConnectionString(_parentHandle: THandle;
  _initialString: WideString;
  out _newString: string): Boolean;
var
  DataInit: IDataInitialize;
  DBPrompt: IDBPromptInitialize;
  DataSource: IUnknown;
  InitStr: PWideChar;
begin
  Result   := False;
  DataInit := CreateComObject(CLSID_DataLinks) as IDataInitialize;
  if _initialString <> '' then
    DataInit.GetDataSource(nil, CLSCTX_INPROC_SERVER, PWideChar(_initialString),
      IUnknown, DataSource);
  DBPrompt := CreateComObject(CLSID_DataLinks) as IDBPromptInitialize;
  if Succeeded(DBPrompt.PromptDataSource(nil, _parentHandle,
    DBPROMPTOPTIONS_PROPERTYSHEET, 0, nil, nil, IUnknown, DataSource)) then
  begin
    InitStr := nil;
    DataInit.GetInitializationString(DataSource, True, InitStr);
    _newString := InitStr;
    Result    := True;
  end;
end;

function TConnectionStringBuilder.Execute : Boolean;
var initialString : WideString;
    newString : string;
    var res : Boolean;
begin
  initialString := '';
  newString := '';
  res := ADOConnectionString(Application.Handle, initialString, newString);
  if res then
    FConnectionString := newString;
  result := res;
end;

function TConnectionStringBuilder.GetConnStr : string;
begin
  result := FConnectionString;
end;

procedure TConnectionStringBuilder.SetConnStr(const _value : string);
begin
  FConnectionString := _value;
end;

procedure Register;
begin
  RegisterComponents('Prestwood', [TConnectionStringBuilder]);
end;

end.

Thats it just 90-some lines of code.

Heres the contents of the corresponding dpk:

package ConnStrBuilder;

{$R *.res}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION ON}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES OFF}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$DESCRIPTION 'Connection String Builder'}
{$IMPLICITBUILD OFF}

requires
  rtl,
  vcl;

contains
  ConnectionStringBuilder in 'ConnectionStringBuilder.pas';

end.

To compile and install this component, just follow these steps.

1) Save the component code as ConnectionStringBuilder.pas.
2) Save the dpk code as ConnStrBuilderDn.dpk, where n = your version of Delphi.
3) In Delphi, open the dpk file, compile and install.

In its IDE, Delphi makes it easy for us to generate correct connection strings for database connections that require them. Unfortunately, Delphi doesnt offer the same convenience at run-time  when our customers need it most! This article offers a Prestwood exclusive, TConnectionStringBuilder, a non-visual component you can use in your applications to let your users build a connection string at program startup. You can then store that connection string, for future use, in an INI file or registry key.


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

Thank you .

Very good code .

---
M.Ebrahimi
Posted 8 years ago

Comment 2 of 2

THanks for providing such a useful informations.

---
Newp
Posted 53 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P125A1
Enter key:
KB Post Contributed By Wes Peterson:

Wes Peterson is a Senior Programmer Analyst with Prestwood IT Solutions where he develops custom Windows software and custom websites using .Net and Delphi. When Wes is not coding for clients, he participates in this online community. Prior to his 10-year love-affair with Delphi, he worked with several other tools and databases. Currently he specializes in VS.Net using C# and VB.Net. To Wes, the .NET revolution is as exciting as the birth of Delphi.

Visit Profile

 KB Article #100448 Counter
18345
Since 4/2/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]