《在VB中用程序建立DSN数据源》
http://go1.163.com/~askpro/msg20/qa66.htm

解决方案 »

  1.   

      
         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 
         
         'Function Declare 
         #If WIN32 Then 
         
         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 
         #Else 
         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 
         #End If 
         
    'Add the following code into the Click event of Command1: 
         
         #If WIN32 Then 
         Dim intRet As Long 
         #Else 
         Dim intRet As Integer 
         #End If 
         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) 
         strAttributes = strAttributes & "UID=sa" & Chr$(0) 
         strAttributes = strAttributes & "PWD=" & 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" 
         End If 
         
    'Add the following code into the Click event of Command2: 
         
         #If WIN32 Then 
         Dim intRet As Long 
         #Else 
         Dim intRet As Integer 
         #End If 
         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)