Lista de DSN y Drivers en el sistema

18/06/2004 - 16:43 por mgs | Informe spam
Hola grupo, quisiera saber como hacer para obtener la lista los dsn y los
driver instalados en mi máquina...
estoy tratando de hacer esto pero me da un error de "referencia a objeto no
establecida como instancia de objeto"?.,


Private Declare Function SQLDataSources Lib "odbc32.dll" _
(ByVal hEnv As Long, _
ByVal fDirection As Integer, _
ByVal szDSN As String, _
ByVal cbDSNMax As Integer, _
ByVal pcbDSN As Integer, _
ByVal szDescription As String, _
ByVal cbDescriptionMax As Integer, _
ByVal pcbDescription As Integer) As Long

Private Declare Function SQLAllocHandle Lib "odbc32.dll" _
(ByVal HandleType As Integer, _
ByVal InputHandle As Long, _
ByVal OutputHandlePtr As Long) As Long

Private Declare Function SQLSetEnvAttr Lib "odbc32.dll" _
(ByVal EnvironmentHandle As Long, _
ByVal dwAttribute As Long, _
ByVal ValuePtr As Long, _
ByVal StringLen As Long) As Long

Private Declare Function SQLFreeHandle Lib "odbc32.dll" _
(ByVal HandleType As Integer, _
ByVal Handle As Long) As Long

Private Const SQL_MAX_DSN_LENGTH As Long = 32
Private Const SQL_MAX_DESC_LENGTH As Long = 128
Private Const SQL_SUCCESS As Long = 0
Private Const SQL_FETCH_NEXT As Long = 1
Private Const SQL_NULL_HANDLE As Long = 0
Private Const SQL_HANDLE_ENV As Long = 1
Private Const SQL_ATTR_ODBC_VERSION As Long = 200
Private Const SQL_OV_ODBC3 As Long = 3
Private Const SQL_IS_INTEGER As Long = (-6)


Sub GetDSNsAndDrivers()
Dim i As Integer
Dim hEnv As Long 'handle to the environment
Dim sServer As String
Dim sDriver As String
Dim nSvrLen As Integer
Dim nDvrLen As Integer 'handle to the environment
Dim iRet As Long

Try
cboDSNList.Items.Add("(None)")

'obtain a handle to the environment
If SQLAllocHandle(SQL_HANDLE_ENV, _
SQL_NULL_HANDLE, hEnv) <> 0 Then

'if successful, set the
'environment for subsequent calls
If SQLSetEnvAttr(hEnv, _
SQL_ATTR_ODBC_VERSION, _
SQL_OV_ODBC3, _
SQL_IS_INTEGER) <> 0 Then


'set up the strings for the call
sServer Microsoft.VisualBasic.Space(SQL_MAX_DSN_LENGTH)
sDriver Microsoft.VisualBasic.Space(SQL_MAX_DESC_LENGTH)

'load the DSN names
iRet = SQLDataSources(hEnv, _
SQL_FETCH_NEXT, _
sServer, _
SQL_MAX_DSN_LENGTH, _
nSvrLen, _
sDriver, _
SQL_MAX_DESC_LENGTH, _
nDvrLen)
Do While iret = SQL_SUCCESS

'add data to the controls

cboDSNList.Items.Add(Microsoft.VisualBasic.Left$(sServer, nSvrLen))
'List2.AddItem(Left$(sDriver, nDvrLen))

'repad the strings
sServer Microsoft.VisualBasic.Space(SQL_MAX_DSN_LENGTH)
sDriver Microsoft.VisualBasic.Space(SQL_MAX_DESC_LENGTH)

Loop

End If 'If SQLSetEnvAttr

'clean up
Call SQLFreeHandle(SQL_HANDLE_ENV, hEnv)

End If 'If SQLAllocHandle



Catch e As Exception
MsgBox(e.Message)
End Try

End Sub
 

Leer las respuestas

#1 Eduardo A. Morcillo [MS MVP VB]
18/06/2004 - 19:28 | Informe spam
¿Esto era para VB6 no? Porque las declaraciones estan con los tipos de datos
de VB6. Utiliza esta version modificada para vb.net:


Private Declare Function SQLAllocHandle Lib "odbc32.dll" ( _
ByVal HandleType As Short, ByVal InputHandle As Integer, _
ByRef OutputHandlePtr As IntPtr) As Integer

Private Declare Function SQLSetEnvAttr Lib "odbc32.dll" ( _
ByVal EnvironmentHandle As IntPtr, ByVal dwAttribute As Integer, _
ByVal ValuePtr As Integer, ByVal StringLen As Integer) As Integer

Private Declare Function SQLDataSources Lib "odbc32.dll" ( _
ByVal hEnv As IntPtr, ByVal fDirection As Short, _
ByVal szDSN As System.Text.StringBuilder, ByVal cbDSNMax As Short, _
ByRef pcbDSN As Short, ByVal szDescription As
System.Text.StringBuilder, _
ByVal cbDescriptionMax As Short, ByRef pcbDescription As Short) As
Integer

Private Declare Function SQLFreeHandle Lib "odbc32.dll" ( _
ByVal HandleType As Short, _
ByVal Handle As IntPtr) As Integer

Private Const SQL_MAX_DSN_LENGTH As Integer = 32
Private Const SQL_MAX_DESC_LENGTH As Integer = 128
Private Const SQL_SUCCESS As Integer = 0
Private Const SQL_FETCH_NEXT As Integer = 1
Private Const SQL_NULL_HANDLE As Integer = 0
Private Const SQL_HANDLE_ENV As Integer = 1
Private Const SQL_ATTR_ODBC_VERSION As Integer = 200
Private Const SQL_OV_ODBC3 As Integer = 3
Private Const SQL_IS_INTEGER As Integer = (-6)


Sub GetDSNsAndDrivers()
Dim i As Integer
Dim hEnv As IntPtr
Dim sServer As System.Text.StringBuilder
Dim sDriver As System.Text.StringBuilder
Dim nSvrLen As Integer
Dim nDvrLen As Integer 'handle to the environment
Dim iRet As Long

Try

'obtain a handle to the environment
If SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, hEnv) <> 0
Then

'if successful, set the
'environment for subsequent calls
If SQLSetEnvAttr(hEnv, _
SQL_ATTR_ODBC_VERSION, _
SQL_OV_ODBC3, _
SQL_IS_INTEGER) <> 0 Then


'set up the strings for the call
sServer = New
System.Text.StringBuilder(SQL_MAX_DSN_LENGTH)
sDriver = New
System.Text.StringBuilder(SQL_MAX_DESC_LENGTH)

'load the DSN names
Do While SQLDataSources(hEnv, _
SQL_FETCH_NEXT, _
sServer, _
SQL_MAX_DSN_LENGTH, _
nSvrLen, _
sDriver, _
SQL_MAX_DESC_LENGTH, _
nDvrLen)= SQL_SUCCESS

MessageBox.Show(sServer.ToString)

Loop

End If 'If SQLSetEnvAttr

'clean up
Call SQLFreeHandle(SQL_HANDLE_ENV, hEnv)

End If 'If SQLAllocHandle

Catch e As Exception
MsgBox(e.ToString)
End Try

End Sub



Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo

Preguntas similares