1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| Option Explicit
' Declaration des constantes
Private Const ODBC_ADD_DSN = 1 ' Ajout d'une source de données
Private Const ODBC_CONFIG_DSN = 2 ' Configuration d'une source de données
Private Const ODBC_REMOVE_DSN = 3 ' Suppression d'une source de données
Private Const ODBC_ADD_SYS_DSN = 4 ' Ajout d'une source système
Private Const ODBC_CONFIG_SYS_DSN = 5 ' Configuration d'une source système
Private Const ODBC_REMOVE_SYS_DSN = 6 ' Suppression d'une source système
Private Const ODBC_REMOVE_DEFAULT_DSN = 7 ' Suppression d'une source système par défaut
Private Const vbAPINull As Long = 0& ' Pointeur NULL
'Déclaration des APIs
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
( _
ByVal hwndParent As Long, _
ByVal fRequest As Long, _
ByVal lpszDriver As String, _
ByVal lpszAttributes As String _
) As Long
Private Sub Command1_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
' Utilisation du driver ODBC pour SQL Server.
strDriver = "SQL Server"
' Initialiser les attributs en les séparant par le caractère Null.
' Voir la documentation du driver pour une liste complète des
' attributs
strAttributes = "SERVER=myserver" + Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Connexion_myserver" + Chr$(0)
strAttributes = strAttributes & "DSN=ODBC_Application" + Chr$(0)
strAttributes = strAttributes & "DATABASE=Mydb" + Chr$(0)
'Ces deux lignes posent problème
strAttributes = strAttributes & "UID=Log_APP" + Chr$(0)
strAttributes = strAttributes & "PWD=Pwd_APP" + Chr&(0)
' Pour visualiser la boite de dialogue, utiliser Form1.Hwnd au lieu
' de vbAPINull.
intRet = SQLConfigDataSource( _
vbAPINull, _
ODBC_ADD_SYS_DSN, _
strDriver, _
strAttributes _
)
If intRet Then
MsgBox "Connexion OK"
Else
MsgBox "Echec connexion"
End If
End Sub
Private Sub Command2_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
' Utilisation du driver ODBC pour SQL Server.
strDriver = "SQL Server"
' Initialiser les attributs en les séparant par le caractère Null.
' Voir la documentation du driver pour une liste complète des
' attributs
strAttributes = "DSN=ODBC_Application" & Chr$(0)
' Pour visualiser la boite de dialogue, utiliser Form1.Hwnd au lieu
' de vbAPINull.
intRet = SQLConfigDataSource( _
vbAPINull, _
ODBC_REMOVE_SYS_DSN, _
strDriver, _
strAttributes _
)
If intRet Then
MsgBox "Connexion supprimée"
Else
MsgBox "Echec de suppression de la connexion"
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "Créer ODBC_Application"
Command2.Caption = "Supprimer ODBC_Application"
End Sub |
Partager