Доброго времени суток уважаемые.
Подскажите плиз, как програмно получить список DSN соединений?
Прошу прощения за беспокойство.
Option Explicit
'Определение SQL сервера и названия БД
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open ("PROVIDER=MSDASQL;dsn=???;uid=" & Connect.Log & ";pwd=" & Connect.Pas & ";")
Dim sql As String 'Имя SQL сервера
Dim bd As String 'Название БД
sql = Trim(cn.ConnectionString)
bd = Trim(cn.DefaultDatabase)
cn.Close
Set cn = Nothing
'Поиск в строке
i = Len(sql)
sql = Trim(Right(sql, i - InStr(sql, ";WSID=") - 5))
sql = Trim(Left(sql, InStr(sql, ";") - 1))
oSqlServer.Connect sql, Connect.Log, Connect.Pas
'SQL и БД, полученная из DSN
Text1(0).Text = sql
Text1(1).Text = bd
cn.Close
Set cn = Nothing
Public Sub GetAllDSNs(ByRef cmb As ComboBox)
' Fill a listbox control with the list of all available DSNs
Dim ODBCTool As New ODBCTool.Dsn
Dim Dsn() As String, i As Long
If ODBCTool.GetDataSourceList(Dsn()) Then
' a True return value means success
cmb.Clear
For i = 0 To UBound(Dsn)
cmb.AddItem Dsn(i)
Next
Else
' a False value means error
MsgBox "Unable to read ODBC driver list", vbExclamation
End If
End Sub
Option Explicit
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (ByRef hEnv As Long) As Integer
Private Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal hEnv As Long) As Integer
Private Declare Function SQLDataSources Lib "odbc32.dll" _
(ByVal hEnv As Long, _
ByVal Direction As Integer, _
ByVal DataSourceName As String, _
ByVal BufferLength1 As Integer, _
ByRef NameLength1Ptr As Integer, _
ByVal Description As String, _
ByVal BufferLength2 As Integer, _
ByRef NameLength2Ptr As Integer) As Integer
Private Declare Function SQLSetEnvAttr Lib "odbc32.dll" _
(ByVal hEnv As Long, _
ByVal Attr As Integer, _
ByVal ValuePtr As Long, _
ByVal StringLength As Integer) As Integer
Private Const SQL_SUCCESS = 0
Private Const SQL_ERROR = -1
Private Const SQL_NO_DATA = 100
Private Const SQL_ATTR_ODBC_VERSION As Long = 200
Private Const SQL_OV_ODBC3 = 3
Private Const SQL_IS_INTEGER = -6
Private Const SQL_FETCH_NEXT = 1
Private Const SQL_FETCH_FIRST = 2
Private Const SQL_FETCH_LAST = 3
Private Const SQL_FETCH_PRIOR = 4
Private Const SQL_FETCH_ABSOLUTE = 5
Private Const SQL_FETCH_RELATIVE = 6
Private Const SQL_MAX_DSN_LENGTH = 32
Private Const SQL_MAX_DESC_LENGTH = 128
Private Declare Function SQLSetConfigMode Lib "odbccp32.dll" (ByVal wConfigMode As Integer) As Boolean
Private Declare Function SQLGetPrivateProfileString Lib "odbccp32.dll" _
(ByVal Section As String, _
ByVal Key As String, _
ByVal Default As String, _
ByVal Buffer As String, _
ByVal BufSize As Integer, _
ByVal Init As String) As Long
Private Const ODBC_USER_DSN = 1
Private Const ODBC_SYSTEM_DSN = 2
Private Const ODBC_BOTH_DSN = 0
Sub EnumerateDSN()
Dim hEnv As Long
Dim result As Integer
Dim strDSNBuffer As String
Dim strDSN As String
Dim strDescription As String
Dim NameLength1 As Integer
Dim NameLength2 As Integer
If SQLAllocEnv(hEnv) = SQL_SUCCESS Then
If SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, SQL_IS_INTEGER) = SQL_SUCCESS Then
strDSNBuffer = Space(SQL_MAX_DSN_LENGTH)
strDescription = Space(SQL_MAX_DESC_LENGTH)
Do
result = SQLDataSources(hEnv, SQL_FETCH_NEXT, strDSNBuffer, SQL_MAX_DSN_LENGTH, NameLength1, strDescription, SQL_MAX_DESC_LENGTH, NameLength2)
If Left(strDescription, NameLength2) = "SQL Server" Then
strDSN = Left(strDSNBuffer, NameLength1)
Debug.Print "DSN="; strDSN,
Debug.Print "Server="; GetSectionFromDSN(strDSN, "Server"),
Debug.Print "Database="; GetSectionFromDSN(strDSN, "Database")
End If
Loop Until result = SQL_NO_DATA
End If
SQLFreeEnv hEnv
End If
End Sub
Private Function GetSectionFromDSN(ByVal DSN As String, ByVal Section As String) As String
Dim strBuffer As String
Dim BufferSize As Integer
If SQLSetConfigMode(ODBC_BOTH_DSN) Then
strBuffer = Space(512)
BufferSize = SQLGetPrivateProfileString(DSN, Section, "", strBuffer, 511, "Odbc.ini")
GetSectionFromDSN = Left(strBuffer, BufferSize)
End If
End Function
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 9