Bonjour,
j'ai devéloppé une application qui accède à la base de données par ODBC.
Maintenant, je voudrai un script de création automatique de la source de donnée lors de la 1ère exécution du logiciel.
Merci de m'aider
Version imprimable
Bonjour,
j'ai devéloppé une application qui accède à la base de données par ODBC.
Maintenant, je voudrai un script de création automatique de la source de donnée lors de la 1ère exécution du logiciel.
Merci de m'aider
salut
explique mieux stpCitation:
Envoyé par lessoy
il veut faire caCitation:
Envoyé par Asdorve
http://vb.developpez.com/faq/?page=Bdd#lienODBC
pour sql server
là c'est clair. :D merci (pour lui aussi! lol)
Code:
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 Option Explicit Private Const REG_SZ = 1 'Constant for a string variable type. Private Const HKEY_LOCAL_MACHINE = &H80000002 Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _ "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _ phkResult As Long) As Long Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _ "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _ cbData As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long Private Sub Command1_Click() Dim DataSourceName As String Dim DatabaseName As String Dim Description As String Dim DriverPath As String Dim DriverName As String Dim LastUser As String Dim Regional As String Dim Server As String Dim lResult As Long Dim hKeyHandle As Long 'Specify the DSN parameters. DataSourceName = "<the name of your new DSN>" DatabaseName = "<name of the database to be accessed by the new DSN>" Description = "<a description of the new DSN>" DriverPath = "<path to your SQL Server driver>" LastUser = "<default user ID of the new DSN>" Server = "<name of the server to be accessed by the new DSN>" DriverName = "SQL Server" 'Create the new DSN key. lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & _ DataSourceName, hKeyHandle) 'Set the values of the new DSN key. lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _ ByVal DatabaseName, Len(DatabaseName)) lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _ ByVal Description, Len(Description)) lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _ ByVal DriverPath, Len(DriverPath)) lResult = RegSetValueEx(hKeyHandle, "LastUser", 0&, REG_SZ, _ ByVal LastUser, Len(LastUser)) lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _ ByVal Server, Len(Server)) 'Close the new DSN key. lResult = RegCloseKey(hKeyHandle) 'Open ODBC Data Sources key to list the new DSN in the ODBC Manager. 'Specify the new value. 'Close the key. lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _ "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle) lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _ ByVal DriverName, Len(DriverName)) lResult = RegCloseKey(hKeyHandle) End Sub
Ou encore...
Dans un bouton pour créer le DNSCode:
1
2
3
4
5
6
7
8
9
10
11
12 Option Explicit 'Constant Declaration Private Const ODBC_ADD_DSN = 1 ' Add data source Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source Private Const ODBC_REMOVE_DSN = 3 ' Remove data source Private Const vbAPINull As Long = 0& ' NULL Pointer Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" _ (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal _ lpszDriver As String, ByVal lpszAttributes As String) As Integer
Dans un autre pour le supprimerCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 Dim intRet As Integer Dim strDriver As String Dim strAttributes As String 'Set the driver to SQL Server because it is most common. strDriver = "SQL Server" 'Set the attributes delimited by null. 'See driver documentation for a complete 'list of supported attributes. strAttributes = "SERVER=SomeServer" & Chr$(0) strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0) strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0) strAttributes = strAttributes & "DATABASE=pubs" & Chr$(0) 'To show dialog, use Form1.Hwnd instead of vbAPINull. intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _ strDriver, strAttributes) If intRet Then MsgBox "DSN Created" Else MsgBox "Create Failed"
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 Dim intRet As Integer Dim strDriver As String Dim strAttributes As String 'Set the driver to SQL Server because most common. strDriver = "SQL Server" 'Set the attributes delimited by null. 'See driver documentation for a complete list of attributes. strAttributes = "DSN=DSN_TEMP" & Chr$(0) 'To show dialog, use Form1.Hwnd instead of vbAPINull. intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _ strDriver, strAttributes) If intRet Then MsgBox "DSN Deleted" Else MsgBox "Delete Failed" End If End If