Board index » delphi » Listing System ODBC DataSource (DSN) ?

Listing System ODBC DataSource (DSN) ?

Hello All,

How can I get a list of all system ODBC datasource names (DSN) into a
combobox ?

Ive the following VB (6.0 Pro) code but finding it hard to port to Delphi:

Const SQL_SUCCESS As Long = 0
Const SQL_FETCH_NEXT As Long = 1

Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As
Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax As
Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal
cbDescriptionMax As Integer, pcbDescription As Integer) As Integer
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (env As Long) As
Integer

Private Sub LoadCmboDsn()

    On Error Resume Next

    Const iDsnMaxLen As Integer = 1024
    Const iDrvMaxLen As Integer = 1024

    Dim iRslt As Integer
    Dim sDsnItem As String * iDsnMaxLen
    Dim sDrvItem As String * iDrvMaxLen
    Dim sDsn As String
    Dim iDsnLen As Integer
    Dim iDrvLen As Integer
    Dim lhEnv As Long         'handle to the environment

    'get the DSNs
    If (SQLAllocEnv(lhEnv) <> -1) Then
        Do
            sDsnItem = Space$(iDsnMaxLen)
            iRslt = SQLDataSources(lhEnv, SQL_FETCH_NEXT, _
                               sDsnItem, iDsnMaxLen, iDsnLen, _
                               sDrvItem, iDrvMaxLen, iDrvLen)
            sDsn = Left$(sDsnItem, iDsnLen)

            If (sDsn <> Space$(iDsnLen)) Then
                cmboDsn.AddItem sDsn
            End If
        Loop Until (iRslt <> SQL_SUCCESS)
    End If

End Sub

Using Delphi 5.0 Enterpise Edition

Kind Regards,

Lennie De Villiers

EMail: ddevilli...@lando.co.za
Web: www.lando.co.za/Lennie
CV/Resume: www.lando.co.za/Lennie/CV.htm

"Real programmers don't document. If it was hard to write, it should
be hard to understand."

 

Re:Listing System ODBC DataSource (DSN) ?


Yeah just list the key outta the registry !! do a search with reg edit to
find the key then use Tregistry component to read the key into the list box

Thanks

Aaron
"D De Villiers" <~ddevilliers9...@lando.co.za> wrote in message
news:avkl2p$hhh$2@ctb-nnrp2.saix.net...

Quote
> Hello All,

> How can I get a list of all system ODBC datasource names (DSN) into a
> combobox ?

> Ive the following VB (6.0 Pro) code but finding it hard to port to Delphi:

> Const SQL_SUCCESS As Long = 0
> Const SQL_FETCH_NEXT As Long = 1

> Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As
> Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax
As
> Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal
> cbDescriptionMax As Integer, pcbDescription As Integer) As Integer
> Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (env As Long) As
> Integer

> Private Sub LoadCmboDsn()

>     On Error Resume Next

>     Const iDsnMaxLen As Integer = 1024
>     Const iDrvMaxLen As Integer = 1024

>     Dim iRslt As Integer
>     Dim sDsnItem As String * iDsnMaxLen
>     Dim sDrvItem As String * iDrvMaxLen
>     Dim sDsn As String
>     Dim iDsnLen As Integer
>     Dim iDrvLen As Integer
>     Dim lhEnv As Long         'handle to the environment

>     'get the DSNs
>     If (SQLAllocEnv(lhEnv) <> -1) Then
>         Do
>             sDsnItem = Space$(iDsnMaxLen)
>             iRslt = SQLDataSources(lhEnv, SQL_FETCH_NEXT, _
>                                sDsnItem, iDsnMaxLen, iDsnLen, _
>                                sDrvItem, iDrvMaxLen, iDrvLen)
>             sDsn = Left$(sDsnItem, iDsnLen)

>             If (sDsn <> Space$(iDsnLen)) Then
>                 cmboDsn.AddItem sDsn
>             End If
>         Loop Until (iRslt <> SQL_SUCCESS)
>     End If

> End Sub

> Using Delphi 5.0 Enterpise Edition

> Kind Regards,

> Lennie De Villiers

> EMail: ddevilli...@lando.co.za
> Web: www.lando.co.za/Lennie
> CV/Resume: www.lando.co.za/Lennie/CV.htm

> "Real programmers don't document. If it was hard to write, it should
> be hard to understand."

Re:Listing System ODBC DataSource (DSN) ?


Try the
HKEY_CURRENT_USER\Software\ODBC\ODBC.INI
or
KEY_LOCAL_MACHINE\Software\ODBC\ODBCINST.INI
registry keys
TRegistry can be a little daunting at first, then it is very easy later, let
us know if u have any probs
Cheers, Dave

Quote
"Aaron Miles" <aa...@qbdthebookshop.com> wrote in message

news:NzmT9.27$3x1.3943@nasal.pacific.net.au...
Quote
> Yeah just list the key outta the registry !! do a search with reg edit to
> find the key then use Tregistry component to read the key into the list
box

> Thanks

> Aaron
> "D De Villiers" <~ddevilliers9...@lando.co.za> wrote in message
> news:avkl2p$hhh$2@ctb-nnrp2.saix.net...
> > Hello All,

> > How can I get a list of all system ODBC datasource names (DSN) into a
> > combobox ?

> > Ive the following VB (6.0 Pro) code but finding it hard to port to
Delphi:

> > Const SQL_SUCCESS As Long = 0
> > Const SQL_FETCH_NEXT As Long = 1

> > Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As
> > Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax
> As
> > Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal
> > cbDescriptionMax As Integer, pcbDescription As Integer) As Integer
> > Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (env As Long) As
> > Integer

> > Private Sub LoadCmboDsn()

> >     On Error Resume Next

> >     Const iDsnMaxLen As Integer = 1024
> >     Const iDrvMaxLen As Integer = 1024

> >     Dim iRslt As Integer
> >     Dim sDsnItem As String * iDsnMaxLen
> >     Dim sDrvItem As String * iDrvMaxLen
> >     Dim sDsn As String
> >     Dim iDsnLen As Integer
> >     Dim iDrvLen As Integer
> >     Dim lhEnv As Long         'handle to the environment

> >     'get the DSNs
> >     If (SQLAllocEnv(lhEnv) <> -1) Then
> >         Do
> >             sDsnItem = Space$(iDsnMaxLen)
> >             iRslt = SQLDataSources(lhEnv, SQL_FETCH_NEXT, _
> >                                sDsnItem, iDsnMaxLen, iDsnLen, _
> >                                sDrvItem, iDrvMaxLen, iDrvLen)
> >             sDsn = Left$(sDsnItem, iDsnLen)

> >             If (sDsn <> Space$(iDsnLen)) Then
> >                 cmboDsn.AddItem sDsn
> >             End If
> >         Loop Until (iRslt <> SQL_SUCCESS)
> >     End If

> > End Sub

> > Using Delphi 5.0 Enterpise Edition

> > Kind Regards,

> > Lennie De Villiers

> > EMail: ddevilli...@lando.co.za
> > Web: www.lando.co.za/Lennie
> > CV/Resume: www.lando.co.za/Lennie/CV.htm

> > "Real programmers don't document. If it was hard to write, it should
> > be hard to understand."

Re:Listing System ODBC DataSource (DSN) ?


Hello David,

Thank You ! :-)

Is this the same on all MS Windows versions ?
Im writting the app on MS Windows 2000 but I doesnt wanna get problems when
running on MS Windows 98/NT or even XP.

Quote
> Try the
> HKEY_CURRENT_USER\Software\ODBC\ODBC.INI
> or
> KEY_LOCAL_MACHINE\Software\ODBC\ODBCINST.INI
> registry keys
> TRegistry can be a little daunting at first, then it is very easy later,
let
> us know if u have any probs
> Cheers, Dave

Kind Regards,

Lennie De Villiers

EMail: ddevilli...@lando.co.za
Web: www.lando.co.za/Lennie
CV/Resume: www.lando.co.za/Lennie/CV.htm

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg."

Re:Listing System ODBC DataSource (DSN) ?


Lennie,

Those keys are from my Windows 98 box.
If they are good for your 2000 machine, you will have no problems, because
XP and NT will be the same as 2000.
Cheers
David

"D De Villiers" <~ddevilliers9...@lando.co.za> wrote in message
news:avpab9$2rs$1@ctb-nnrp2.saix.net...

Quote
> Hello David,

> Thank You ! :-)

> Is this the same on all MS Windows versions ?
> Im writting the app on MS Windows 2000 but I doesnt wanna get problems
when
> running on MS Windows 98/NT or even XP.

> > Try the
> > HKEY_CURRENT_USER\Software\ODBC\ODBC.INI
> > or
> > KEY_LOCAL_MACHINE\Software\ODBC\ODBCINST.INI
> > registry keys
> > TRegistry can be a little daunting at first, then it is very easy later,
> let
> > us know if u have any probs
> > Cheers, Dave

> Kind Regards,

> Lennie De Villiers

> EMail: ddevilli...@lando.co.za
> Web: www.lando.co.za/Lennie
> CV/Resume: www.lando.co.za/Lennie/CV.htm

> "C makes it easy to shoot yourself in the foot. C++ makes it
> harder, but when you do, it blows away your whole leg."

Other Threads