Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating DNS programatically

29 views
Skip to first unread message

Paul Petroniu Marza

unread,
Apr 2, 2001, 8:48:27 AM4/2/01
to
Sorry, DSN - Data Source Name


"Paul Petroniu Marza" <pa...@acinta.dk> wrote in message
news:3ac8741b$1_1@dnews...
> Hello everybody,
>
> I am wondering if any of you ever succeeded to create a DSN
programatically
> from Delphi. If you did, can you help me with this?
>
> TIA,
> Paul
>
>


Paul Petroniu Marza

unread,
Apr 2, 2001, 8:41:30 AM4/2/01
to

Jerry Bloomfield (TeamB)

unread,
Apr 2, 2001, 9:07:08 PM4/2/01
to
On Mon, 2 Apr 2001 14:48:27 +0200, "Paul Petroniu Marza" <pa...@acinta.dk>
wrote:

>"Paul Petroniu Marza" <pa...@acinta.dk> wrote in message
>news:3ac8741b$1_1@dnews...
>> Hello everybody,
>>
>> I am wondering if any of you ever succeeded to create a DSN programatically
>> from Delphi. If you did, can you help me with this?

I have not done it from Delphi, but I have done it from C++. You can see
an example of this in CodeCentral by looking at my two entries. The entry
with the lower ID number is the one which has the code to programmatically
create a DSN...

Jerry Bloomfield (TeamB)
--
http://www.teamb.com Jers...@wwa.com
Please do *NOT* send private e-mail without prior permission (my anti-spam
filters will probably just delete it anyway <g>)

Paul Petroniu Marza

unread,
Apr 3, 2001, 4:01:40 AM4/3/01
to
Just found a Delphi solution as well:

function CreateDSNAccess(p_strDSNName, p_strFile, p_strDescr: string):
boolean;
var
pFn: TSQLConfigDataSource;
hLib: longword;
strDriver: string;
strAttr: string;
begin
Result := TRUE;
hLib := LoadLibrary('ODBCCP32'); // load from default path
if (hLib <> NULL) then
begin @pFn := GetProcAddress(hLib, 'SQLConfigDataSource');
if (@pFn <> nil) then
begin
// force (re-)create DSN
strDriver := 'Microsoft Access Driver (*.mdb)';
strAttr := Format('DSN=' + p_strDSNName + #0 +
'DBQ=%s' + #0 +
'Exclusive=1' + #0 +
'Description=' + p_strDescr + #0 + #0,
[p_strFile]);
// if you want to create this database, replace the line above with these:
// 'CREATE_DB="%s"'#0 + #0,
// [p_strFile, p_strFile]);
Result := pFn(0, ODBC_ADD_DSN, @strDriver[1], @strAttr[1]);
end;
FreeLibrary(hLib);
end
else
begin
Raise EClassNotFound.Create('Unable to load ODBCCP32.DLL');
end;
end;

"Jerry Bloomfield (TeamB)" <Jers...@wwa.com> wrote in message
news:h28ictkvlgtgmvg4a...@4ax.com...

Todd

unread,
Apr 9, 2001, 3:28:49 PM4/9/01
to

"Paul Petroniu Marza" <pa...@acinta.dk> wrote in message news:3ac8741b$1_1@dnews...
> Hello everybody,
>
> I am wondering if any of you ever succeeded to create a DSN programatically
> from Delphi. If you did, can you help me with this?
>

unit ODBC;

interface

Uses Windows;

Const
ODBC_ADD_DSN=1;
ODBC_CONFIG_DSN=2;
ODBC_REMOVE_DSN=3;
ODBC_ADD_SYS_DSN=4;
ODBC_CONFIG_SYS_DSN=5;
ODBC_REMOVE_SYS_DSN=6;
ODBC_REMOVE_DEFAULT_DSN=7;

Function SQLConfigDataSource(HwndParent:HWND;FRequest:WORD;Driver:PChar;
Attributes:Pchar):boolean;Stdcall;


implementation

Function SQLConfigDataSource;external 'odbccp32.dll' name 'SQLConfigDataSource';

end.


Call it like this:

SqlConfigDataSource(Application.Handle,ODBC_CONFIG_SYS_DSN,Pchar(<Driver name as displayed
in ODBC>),Pchar(<Attributes as in ODBC Admin>));


0 new messages