0

VB6 アプリケーションで動的 DSN を作成したいと考えています。MODULE で次のコードを試しました。次の Microsoft リンク ( http://support.microsoft.com/kb/171146 ) を使用します。

私のコードはここにあります-

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

Public Function PrepareDSN(strServerName As String, strDBName As String, strDSN As String, strDBUser As String, strDBUserPassword As String) As Boolean
On Error GoTo error_hdl
Dim boolError As Boolean
Dim strDSNString As String
PrepareDSN = False

strDSNString = Space(MAX_BUFFER_SIZE)
strDSNString = ""
strDSNString = strDSNString & "DSN=" & strDSN & Chr(0)
strDSNString = strDSNString & "DESCRIPTION=" & "DSN Created Dynamically On " & CStr(Now) & Chr(0)
strDSNString = strDSNString & "Server=" & strServerName & Chr(0)
strDSNString = strDSNString & "DATABASE=" & strDBName & Chr(0)
strDSNString = strDSNString & Chr(0)



 If Not CBool(SQLConfigDataSource(0, _
                        ODBC_ADD_DSN, _
                        ODBCDriverDescription, _
                        strDSNString)) Then
    boolError = True
    MsgBox ("Error in PrepareDSN::SQLConfigDataSource")


  End If

If boolError Then
    Exit Function
End If
PrepareDSN = True
Exit Function

error_hdl:
    MsgBox "PrepareDSN_ErrHandler::" & err.Description
End Function

ここで私の関数「SQLConfigDataSource」は常にfalseを返します。提案してください。

4

1 に答える 1

2

2つのことが欠けています。

1) リクエスト タイプ ODBC_ADD_DSN を定義していない 2) データベース ドライバが設定されていない

これは、データベースへの接続に使用した変更後の​​コードです。

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 Const ODBC_ADD_DSN = 1 'Define request type
'!!!!!!!!!!
Public Function PrepareDSN(strServerName As String, strDBName As String, strDSN As String, strDBUser As String, strDBUserPassword As String) As Boolean
On Error GoTo error_hdl
Dim boolError As Boolean
Dim strDSNString As String
PrepareDSN = False

strDSNString = Space(MAX_BUFFER_SIZE)
strDSNString = ""
strDSNString = strDSNString & "DSN=" & strDSN & Chr(0)
strDSNString = strDSNString & "DESCRIPTION=" & "DSN Created Dynamically On " & CStr(Now) & Chr(0)
strDSNString = strDSNString & "Server=" & strServerName & Chr(0)
strDSNString = strDSNString & "DATABASE=" & strDBName & Chr(0)
strDSNString = strDSNString & Chr(0)


'!!!!!!!!!!
Const Driver As String = "SQl Server" 'Set driver descr
'!!!!!!!!!!
 If Not CBool(SQLConfigDataSource(0, _
                         ODBC_ADD_DSN, _
                        Driver, _
                        strDSNString)) Then
    boolError = True
    MsgBox ("Error in PrepareDSN::SQLConfigDataSource")


  End If

If boolError Then
    Exit Function
End If
PrepareDSN = True
Exit Function

error_hdl:
    MsgBox "PrepareDSN_ErrHandler::" & Err.Description
End Function
于 2013-07-12T07:41:00.757 に答える