我想在VB6程序中动态创建系统DSN,我用的是API函数:SQLConfigDataSource。
但该API函数创建的数据源是当前用户的DSN,请教各位大侠,怎样直接创建系统DSN?
应该使用什么其他API函数吗?或者大侠们还有其他的高招,请赐教。谢谢

解决方案 »

  1.   

    看看这篇文章.
    选自微软的Knowledge Base的文章:“Q184608 OWTO: Programmatically Create a DSN for SQL Server with VB” 
       
    1. Open a new Visual Basic project. Form1 is created by default. Put a CommandButton on Form1 (Command1), and put the following code in the General Declarations section of the code for Form1: 
         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 
         
        2. Place the following code in the click event of the Command1 button on Form1: 
         
         Change the values of the DataSourceName, DatabaseName, Description, DriverPath, LastUser, and Server variables as appropriate for your environment. Any of the drivers listed on the ODBC Drivers tab of the ODBC Data Source Administrator window can be used as part of the DriverPath variable. All of these drivers can be found in C:\Windows\System for Windows 95 or Windows 98 machines and C:\Winnt\System32 for Windows NT. 
         
         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 = "" 
         DatabaseName = "" 
         Description = "" 
         DriverPath = "" 
         LastUser = "" 
         Server = "" 
         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