ODBC - Create a DSN using ODBCCP32.DLL

From ISXKB

Revision as of 20:51, 17 December 2006 by Vincenzo (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Lambert Antonio creates a DSN using function SQLConfigDataSource of ODBCCP32.DLL, starting from this idea, sample code of this article shows how to create a DSN ODBC specifyng all attributes, it is possible specify where to create it, User or System DSN.

This sample could be also used to "manage" DSN modifying or deleting them. With a task it is possible to choose if a driver dialog configuration have to be shown.

[Setup]
AppName=ODBC2
AppVerName=ODBC2
Uninstallable=false
UpdateUninstallLogAppName=false
DisableDirPage=false
DisableProgramGroupPage=true
DefaultDirName={pf}\ODBC2
DisableStartupPrompt=true
CreateAppDir=false

[_ISTool]
EnableISX=true


[Tasks]
Name: cfgdsndlg; Description: Config DSN with dialog; GroupDescription: Configuration:; Flags: unchecked

[Code]

function SQLConfigDataSource(hwndParent: LongInt; fRequest: LongInt;
lpszDriver: String; lpszAttributes: String): LongInt;
external 'SQLConfigDataSource@ODBCCP32.DLL stdcall';

const
  ODBC_ADD_DSN = 1;         // Add data source
  ODBC_CONFIG_DSN = 2;      // Configure (edit) data source
  ODBC_REMOVE_DSN = 3;      // Remove data source
  ODBC_ADD_SYS_DSN = 4;	    // add a system DSN
  ODBC_CONFIG_SYS_DSN = 5;  // Configure a system DSN
  ODBC_REMOVE_SYS_DSN = 6;  // remove a system DSN


function CreateDSN( showdlg: Boolean) : LongInt;
var
  hwnd : LongInt;
var
  strDriver, strAttributes, NewLine : String;
begin
    // if it is not auto configuration, setting HWND of wizard let show
    // DSN setup dialog with attributes specified
    if not showdlg then
      hwnd := 0
    else
      hwnd := StrToInt(ExpandConstant('{wizardhwnd}'));

    // MYSQL Sample
    {
    strDriver := 'MySQL';
    NewLine := Chr(0);
    strAttributes := 'SERVER=localhost' + NewLine +
    'DESCRIPTION=MySQL Driver DSN' + NewLine +
    'DSN=SAMPLE_DSN' + NewLine +
    'DATABASE=test' + NewLine +
    'UID=username' + NewLine +
    'PASSWORD=password' + NewLine +
    'PORT=3306' + NewLine +
    'OPTION=3' + NewLine;
    }

    // SQL Server Sample
    strDriver := 'SQL Server';
    NewLine := Chr(0);
    strAttributes := 'SERVER=(local)' + NewLine +
    'Description=SQL Server Driver DSN' + NewLine +
    'DSN=SQLSERVER_SAMPLE' + NewLine +
    'Trusted_Connection=Yes' + NewLine;

    Result := SQLConfigDataSource( hwnd, ODBC_ADD_SYS_DSN, strDriver, strAttributes);
end;


function NextButtonClick(CurPage: Integer): Boolean;
var showdlg : Boolean;
begin

  // by default go to next page
  Result := true;

  // if curpage is wpSelectTasks check config DSN
  if CurPage = wpSelectTasks then
  begin

    showdlg := ShouldProcessEntry( '','cfgdsndlg') = srYes ;

    if CreateDSN( showdlg  ) = 0 then
      MsgBox( 'DSN not done.', mbError, MB_OK )
    else
      MsgBox( 'DSN Created !', mbInformation, MB_OK );

  end;

end;
Personal tools
Ads: