我下载了一个DSN建立的源代码,如果数据库是ACCESS的,就可连接
然后我更改成SQL server的数据连接方式就不行了
ACCESS源代码如下:
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 PointerPrivate Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
                (ByVal hwndParent As Long, _
                ByVal fRequest As Long, _
                ByVal lpszDriver As String, _
                ByVal lpszAttributes As String) _
                As LongPrivate Sub Form_Load()
    Dim intRet As Long
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
                        "Microsoft Access Driver (*.mdb)" + Chr$(0), _
                        "DSN=Nwind;DBQ=D:\NWIND.MDB;DEFAULTDIR=D:\" + Chr$(0))
    '如果要显示对话,可使用Me.Hwnd代替vbAPINull
    
    If intRet Then
        MsgBox "DSN建立成功"
    Else
        MsgBox "DSN建立失败"
    End If
End SubPrivate Sub Form_Unload(Cancel As Integer)
    Dim intRet As Long
    intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _
                        "Microsoft Access Driver (*.mdb)" + Chr$(0), _
                        "DSN=Nwind" + Chr$(0))
    If intRet Then
        MsgBox "DSN删除成功"
    Else
        MsgBox "DSN删除失败"
    End If
End SubSQL server源代码:
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 PointerPrivate Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
                (ByVal hwndParent As Long, _
                ByVal fRequest As Long, _
                ByVal lpszDriver As String, _
                ByVal lpszAttributes As String) _
                As LongPrivate Sub Form_Load()
    Dim intRet As Long
    intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
                        "sql server" + Chr$(0), _
                        "DSN=book1;DBQ=E:\bookmanage\DB\book_data.mdf;DEFAULTDIR=D:\" + Chr$(0))
    '如果要显示对话,可使用Me.Hwnd代替vbAPINull
    
    If intRet Then
        MsgBox "DSN建立成功"
    Else
        MsgBox "DSN建立失败"
    End If
End SubPrivate Sub Form_Unload(Cancel As Integer)
    Dim intRet As Long
    intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _
                        "sql server" + Chr$(0), _
                        "DSN=book1" + Chr$(0))
    If intRet Then
        MsgBox "DSN删除成功"
    Else
        MsgBox "DSN删除失败"
    End If
End Sub请高手告诉我如何解决?"

解决方案 »

  1.   

    我下面的测试成功了,是 SQL Server的
    我的ODBCCP32.DLL版本是3.520.6200.0
          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
          Const ODBC_ADD_SYS_DSN = 4       'Add data source
          Const ODBC_CONFIG_SYS_DSN = 5    'Configure (edit) data source
          Const ODBC_REMOVE_SYS_DSN = 6    'Remove data source      Private Const vbAPINull As Long = 0&  ' NULL Pointer          Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
              (ByVal hwndParent As Long, ByVal fRequest As Long, _
              ByVal lpszDriver As String, ByVal lpszAttributes As String) _
              As LongSub SetODBC(Server As String, Des As String, DSN As String, dataBase As String)
        #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=" & Server & Chr$(0)
          strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
          strAttributes = strAttributes & "DSN=" & DSN & Chr$(0)
          strAttributes = strAttributes & "DATABASE=" & dataBase & 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 IfEnd Sub