odbc

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

odbc

pdigonzelli
Hi all , I want to know how to create programatically a dsn odbc source
using windows api.
Someone can help me?

TIA
Pablo


Reply | Threaded
Open this post in threaded view
|

Re: odbc

Christopher J. Demers
"Pablo Digonzelli" <[hidden email]> wrote in message
news:[hidden email]...
> Hi all , I want to know how to create programatically a dsn odbc source
> using windows api.
> Someone can help me?

I am not sure if there is a Windows API specifically for that, but
ultimately DSN's are just registry entries.  Take a look here
"HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI" , and here
"HKEY_CURRENT_USER\Software\ODBC\ODBC.INI" .  Create the kind of DSN you
want via the ODBC Data Source Administrator and the just programmatically
create the registry keys from Dolphin.  To use the registry in Dolphin look
at the RegKey class.  Look at references to it for usage examples.

If you don't really have to create a DSN from Dolphin you might look into
the InnoSetup (http://www.jrsoftware.org/) setup program, when used with
ISTool it make it very easy to create DSN's as part of an application
installation.

Additionally you could use a DSN-less connection.  You can set the connect
string to something like 'DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=DatabaseName.mdb;' and not use a DSN.

Good luck,
Chris


Reply | Threaded
Open this post in threaded view
|

Re: odbc

Schwab,Wilhelm K
In reply to this post by pdigonzelli
Pablo,

> Hi all , I want to know how to create programatically a dsn odbc source
> using windows api.
> Someone can help me?

The following is what I am starting to use.  These are snippets from a
large package, but they will likely stand alone - clearly one is for
Access and the other for MySQL.  I _think_ both are tested, but make no
warranties.  It is unlikely that I ran any of this on other than Win2k.
  In particular, things like "C:\WINNT\System32" might need to be
replaced with SessionManager current systemDirectory, etc, and there
might be approved ways to get the DLL names.

Have a good one,

Bill


===============

!SqlDataSourceMicrosoftAccess class methodsFor!

createDatasourceName:datasourceName mdbFileName:mdbFileName
description:description
        "Shamelessly (and naively!!!!) reverse engineered (aka stolen) from
ISTool.  Create
        registry entries for an Access data source."

                | sources source jet |

        "Just guessing, but beware creating 'data sources' with the following
        names.  Note that one additional level would have avoided the problem,
        as would have a decision to get the list of sources from the extant keys."
        self assert:[
                (
                        #( 'ODBC File DSN' 'ODBC Data Sources' ) anySatisfy:[ :each |
                                ( each asLowercase ) = ( datasourceName asLowercase )
                        ]
                ) not.
        ].

        sources := RegKey localMachineRoot createChain:#( 'SOFTWARE' 'ODBC'
'ODBC.INI' 'ODBC Data Sources' ).
        sources valueAt:datasourceName put:'Microsoft Access Driver (*.mdb)'.

        source := RegKey localMachineRoot
                        createKey:'SOFTWARE\ODBC\ODBC.INI\', datasourceName.

        source
                valueAt:'Driver' put:'C:\WINNT\System32\odbcjt32.dll';
                valueAt:'Description' put:description;
                valueAt:'DBQ' put:mdbFileName;
                valueAt:'DriverId' put:25;
                valueAt:'FIL' put:'MS Access';
                valueAt:'SafeTransactions' put:0;
                valueAt:'UID' put:''.

        jet := RegKey localMachineRoot
                        createChain:(
                                OrderedCollection new
                                        add:'SOFTWARE';
                                        add:'ODBC';
                                        add:'ODBC.INI';
                                        add:datasourceName;
                                        add:'Engines';
                                        add:'Jet';
                                        yourself
                        ).

        jet
                valueAt:'ImplicitCommitSync' put:'';
                valueAt:'MaxBufferSize' put:2048;
                valueAt:'PageTimeout' put:5;
                valueAt:'Threads' put:3;
                valueAt:'UserCommitSync' put:'Yes'.
! !
!SqlDataSourceMicrosoftAccess class categoriesFor:
#createDatasourceName:mdbFileName:description:!public! !

!SqlDataSourceMySQL class methodsFor!

createDatasourceName:datasourceName database:database hostName:hostName
description:description userName:userName password:password
        "Shamelessly (and naively!!!!) reverse engineered (aka stolen) from
ISTool.  Create
        registry entries for a MyODBC data source."

                | sources source |

        "Just guessing, but beware creating 'data sources' with the following
        names.  Note that one additional level would have avoided the problem,
        as would have a decision to get the list of sources from the extant keys."
        self assert:[
                (
                        #( 'ODBC File DSN' 'ODBC Data Sources' ) anySatisfy:[ :each |
                                ( each asLowercase ) = ( datasourceName asLowercase )
                        ]
                ) not.
        ].

        sources := RegKey localMachineRoot createChain:#( 'SOFTWARE' 'ODBC'
'ODBC.INI' 'ODBC Data Sources' ).
        sources valueAt:datasourceName put:'MySQL ODBC 3.51 Driver'.

        source := RegKey localMachineRoot
                        createKey:'SOFTWARE\ODBC\ODBC.INI\', datasourceName.

        source
                valueAt:'Driver' put:'C:\WINNT\System32\myodbc3.dll';
                valueAt:'Description' put:description;
                valueAt:'Database' put:database;
                valueAt:'Server' put:hostName;
                valueAt:'User' put:userName;
                valueAt:'Password' put:password;
                valueAt:'Port' put:'3306';
                valueAt:'Stmt' put:''.
! !
!SqlDataSourceMySQL class categoriesFor:
#createDatasourceName:database:hostName:description:userName:password:!public!
!

!RegKey methodsFor!

createChain:keyParts
        "Work around a bug in Dolphin (a somewhat rare event)"

        "
                To see the need for this:

                        RegKey localMachineRoot createKey:'SOFTWARE\ODBC\ODBC.INI\ODBC Data
Sources'.

                RegKey localMachineRoot createChain:#( 'SOFTWARE' 'ODBC' 'ODBC.INI'
'ODBC Data Sources' )

        "

                | newOne |

        newOne := self.

        keyParts do:[ :each |
                "each = keyParts last ifTrue:[ self halt ]."
                newOne := newOne createKey:each.
        ].

        ^newOne.
! !
!RegKey categoriesFor: #createChain:!public! !




--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: odbc

Yar Hwee Boon-3
On Tue, 17 Aug 2004 18:55:33 -0500, Bill Schwab <[hidden email]>  
wrote:

<Code sample to create registry keys directly snipped>

You can also try file in the class below and run:

(ODBCCP32Library default
     sqlConfigDataSource: nil
     fRequest: 1
     lpszDriver: 'SQL Server'
     lpszAttributes: 'DSN=testinghb' , (Character codePoint: 0) asString ,  
'DATABASE=ttxx' ,  (Character codePoint: 0) asString) inspect


======
"Filed out from Dolphin Smalltalk XP"!

ExternalLibrary subclass: #ODBCCP32Library
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        classInstanceVariableNames: ''!
ODBCCP32Library guid: (GUID fromString:  
'{B1B23C9F-371B-40AE-90DF-3A75BB0E5B6A}')!
ODBCCP32Library comment: ''!
!ODBCCP32Library categoriesForClass!External-Libraries! !
!ODBCCP32Library methodsFor!

sqlConfigDataSource: aWindowHandle fRequest: aWORD lpszDriver:  
aDriverString lpszAttributes: anAttributesString
        "BOOL SQLConfigDataSource(
                HWND      hwndParent,
                WORD      fRequest,
                LPCSTR      lpszDriver,
                LPCSTR      lpszAttributes);"

        <stdcall: bool SQLConfigDataSource handle word lpvoid lpvoid>
        ^self invalidCall! !
!ODBCCP32Library categoriesFor:  
#sqlConfigDataSource:fRequest:lpszDriver:lpszAttributes:!public!win32  
functions-odbc library! !

!ODBCCP32Library class methodsFor!

fileName
        "Answer the host system file name of the external library which the
        receiver represents"

        ^'ODBCCP32'! !
!ODBCCP32Library class categoriesFor: #fileName!constants!public! !
======


--
Regards
Hwee Boon
MotionObj


Reply | Threaded
Open this post in threaded view
|

Re: odbc

pdigonzelli
In reply to this post by pdigonzelli
Thanks all for the help.

I believe I have all i need.

Pablo

"Pablo Digonzelli" <[hidden email]> escribió en el mensaje
news:[hidden email]...
> Hi all , I want to know how to create programatically a dsn odbc source
> using windows api.
> Someone can help me?
>
> TIA
> Pablo
>
>


Reply | Threaded
Open this post in threaded view
|

Re: odbc

Henrik H. Jensen
In reply to this post by Yar Hwee Boon-3
Yar Hwee Boon wrote:
> You can also try file in the class below and run:
>
> (ODBCCP32Library default
>      sqlConfigDataSource: nil
>      fRequest: 1
>      lpszDriver: 'SQL Server'
>      lpszAttributes: 'DSN=testinghb' , (Character codePoint: 0)
> asString , 'DATABASE=ttxx' ,  (Character codePoint: 0) asString)
> inspect

According to MSDN:

ConfigDSN receives connection information from the installer DLL as a list
of attributes in the form of keyword-value pairs. Each pair is terminated
with a null byte, and the entire list is terminated with a null byte. (That
is, two null bytes mark the end of the list.)

therefore you should add an extra (Character codePoint: 0) to your
attributes string.

----
Regards
Henrik H. Jensen, CDM A/S
Strandvejen 863, 2930 Klampenborg, Denmark
Homepage: http://www.cdm.dk
Phone: +45 7027 1927, Fax: +45 7027 1928