Board index » delphi » Creating ODBC alias at runtime

Creating ODBC alias at runtime

Hello! I would like to know if it's possibly to create an ODBC alias at
runtime with Delphi 4. It's to access an Access Database and to avoid the
problem of the user doing that himself. Is it possible? And how?
Thanks

Best Regards

Goncalo Martins

 

Re:Creating ODBC alias at runtime


The following example demonstrates the use of SQLConfigDatasource for MS
Access databases:

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
  ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN

function SQLConfigDataSource(
  hwndParent: HWND;
  fRequest: WORD;
  lpszDriver: LPCSTR;
  lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL';

function AddDSN(const DSN, Database, Description: String): Boolean; begin
  Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
    'Microsoft Access Driver (*.mdb)', PChar(
    'DSN=' + DSN + #0 +
    'Driver=ODBCJT32.DLL'#0 +
    'DBQ=' + Database + ''#0 +
    'DefaultDir=' + ExtractFilePath(Database) + #0 +
    'Description=' + Description + #0 +
    'FIL=MS Access'#0 +
    'UID=Admin'#0));
end;

function RemoveDSN(const DSN: String): Boolean;
begin
  Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
    'Microsoft Access Driver (*.mdb)', PChar(
    'DSN=' + DSN + #0));
end;

function RepairDB(const Database: String): Boolean;
begin
  Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
    'Microsoft Access Driver (*.mdb)', PChar(
    'REPAIR_DB=' + Database + #0 +
    'UID=Admin'#0 +
    'PWD=PWSWAN'#0));
end;

function CompactDB(const Database1, Database2: String): Boolean; begin
  Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
    'Microsoft Access Driver (*.mdb)', PChar(
    'COMPACT_DB=' + Database1 + ' ' + Database2 + ' General'#0 +
'UID=Admin'#0 +
    'PWD=PWSWAN'#0));
end;

ps. Database filenames have to be put between quotes (") for RepairDB and
CompactDB on Jet 4.0.

"Gon?alo Martins" <gmart...@student.dei.uc.pt> schreef in bericht
news:8ra7sj$e8k$2@news.dei.uc.pt...

Quote
> Hello! I would like to know if it's possibly to create an ODBC alias at
> runtime with Delphi 4. It's to access an Access Database and to avoid the
> problem of the user doing that himself. Is it possible? And how?
> Thanks

> Best Regards

> Goncalo Martins

Re:Creating ODBC alias at runtime


Hi,

I was not the original wroter of the question, but it helps me veri wel.
I can only say
    Thank you

"M.H. Avegaart" <avegaartNOS...@mccomm.nl> schreef in bericht
news:8skei1$mv0$1@porthos.nl.uu.net...

Quote
> The following example demonstrates the use of SQLConfigDatasource for MS
> Access databases:

> 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
>   ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN

> function SQLConfigDataSource(
>   hwndParent: HWND;
>   fRequest: WORD;
>   lpszDriver: LPCSTR;
>   lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL';

> function AddDSN(const DSN, Database, Description: String): Boolean; begin
>   Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
>     'Microsoft Access Driver (*.mdb)', PChar(
>     'DSN=' + DSN + #0 +
>     'Driver=ODBCJT32.DLL'#0 +
>     'DBQ=' + Database + ''#0 +
>     'DefaultDir=' + ExtractFilePath(Database) + #0 +
>     'Description=' + Description + #0 +
>     'FIL=MS Access'#0 +
>     'UID=Admin'#0));
> end;

> function RemoveDSN(const DSN: String): Boolean;
> begin
>   Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
>     'Microsoft Access Driver (*.mdb)', PChar(
>     'DSN=' + DSN + #0));
> end;

> function RepairDB(const Database: String): Boolean;
> begin
>   Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
>     'Microsoft Access Driver (*.mdb)', PChar(
>     'REPAIR_DB=' + Database + #0 +
>     'UID=Admin'#0 +
>     'PWD=PWSWAN'#0));
> end;

> function CompactDB(const Database1, Database2: String): Boolean; begin
>   Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
>     'Microsoft Access Driver (*.mdb)', PChar(
>     'COMPACT_DB=' + Database1 + ' ' + Database2 + ' General'#0 +
> 'UID=Admin'#0 +
>     'PWD=PWSWAN'#0));
> end;

> ps. Database filenames have to be put between quotes (") for RepairDB and
> CompactDB on Jet 4.0.

> "Gon?alo Martins" <gmart...@student.dei.uc.pt> schreef in bericht
> news:8ra7sj$e8k$2@news.dei.uc.pt...
> > Hello! I would like to know if it's possibly to create an ODBC alias at
> > runtime with Delphi 4. It's to access an Access Database and to avoid
the
> > problem of the user doing that himself. Is it possible? And how?
> > Thanks

> > Best Regards

> > Goncalo Martins

Other Threads