Adding ODBC-Treiber/DSN with Delphi

> Hi!
> I have to check if some ODBC data source is created and if not create
> it in runtime. APIs for this are in odbc32.dll but there seem to be no
> help nor function prototypes for that in Delphi. If someone know’s how
> to do that or have done it already I would appreciate to get some code
> example and odbc32.dll interface unit.
> tomi.

A simple example I seem to re-post regularly is as follows.
For non-MSAccess databases you only need a single call
and can ignore the DBQ stuff.

For future reference, you’d probably get more/better response
in the borland.public.delphi.database.desktop ng. to this sort
of question…


[code lang=“delphi“](* NOTE: THIS EXAMPLE IS NOT COMPLETE !!! *)

This example shows one way to load the ODBC
Administrator’s DLL (ODBCCP32.DLL) to create
an Access MDB file and ODBC DSN pointing at
it. Note that it assumes current directory
for both the DLL and the MDB, but the DLL
will be found if in the WinSys directory which
is where it normally is anyway.

Similar operation applies to most driver types,
with some modifications. eg: Access requires
the MDB file to exist so you can hook the DSN
to it.

Note also that the „CREATE_DB“ call is an Access
special (MS Jet Engine) and has other variants
like COMPACT_DB and REPAIR_DB. For a full list
see either the Jet Engine Programmers Guide or
the MSDN and search for „CREATE_DB“.

This was originally written in MSVC6 and was
ported to Delphi 5.

Full documentation can be found in the MSDN

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

TSQLConfigDataSource = function( hwndParent: HWND;
fRequest: WORD;
lpszDriver: LPCSTR;
lpszAttributes: LPCSTR ) : BOOL; stdcall;

procedure Form1.FormCreate(Sender: TObject);
pFn: TSQLConfigDataSource;
hLib: LongWord;
strDriver: string;
strHome: string;
strAttr: string;
strFile: string;
fResult: BOOL;
ModName: array[0..MAX_PATH] of Char;
srInfo : TSearchRec;
Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );
strHome := ModName;
while ( strHome[length(strHome)] <> ‚\‘ ) do
Delete( strHome, length(strHome), 1 );
strFile := strHome + ‚TestData.MDB‘; // Test Access Rights (Axes =
hLib := LoadLibrary( ‚ODBCCP32‘ ); // load from default path
if( hLib <> NULL ) then
@pFn := GetProcAddress( hLib, ‚SQLConfigDataSource‘ );
if( @pFn <> nil ) then
// force (re-)create DSN
strDriver := ‚Microsoft Access Driver (*.mdb)‘;
strAttr := Format( ‚DSN=TestDSN’+#0+
‚Description=Test Data’+#0+#0,
[strFile] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then ShowMessage( ‚Create DSN (Datasource)
failed!‘ );

// test/create MDB file associated with DSN
if( FindFirst( strFile, 0, srInfo ) <> 0 ) then
strDriver := ‚Microsoft Access Driver (*.mdb)‘;
strAttr := Format( ‚DSN=TestDSN’+#0+
‚Description=Test Data’+#0+
[strFile,strFile] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then ShowMessage( ‚Create MDB (Database file)
failed!‘ );
FindClose( srInfo );



FreeLibrary( hLib );
ShowMessage( ‚Unable to load ODBCCP32.DLL‘ );
StatusClockTimer.Enabled := true;

[tags]Delphi, API, ODBC[/tags]

0 Kommentare zu “Adding ODBC-Treiber/DSN with Delphi

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.