关于动态创建dsn时不能设置用户名和密码的解决方法

利用下面的代码创建SQL   SERVER的DSN,不设用户名和密码就可以创建成功,但不能连接成功。使用后有报错
    Private Const ODBC_ADDDSN = 1
Private Const ODBC_CONFIGDSN = 2
Private Const vbapinull As Long = 0&
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 Sub createdsn(sdsn As String)
Dim nret As Long
Dim sdriver As String
Dim sattributes As String
sdriver = "sql server"
sattributes = "server=(local)" & Chr$(0)
sattributes = sattributes & "DSN=" & sdsn & Chr$(0)
sattributes = sattributes & "database=student" & Chr$(0)
nret = SQLConfigDataSource(vbapinull, ODBC_ADDDSN, _
      sdriver, sattributes)
      If nret Then
          MsgBox "DSN Created"
      Else
          MsgBox "Create Failed"
      End If


End Sub

Private Sub Command1_Click()
createdsn "test"

End Sub

 

 

只要使用信任链接不设用户名和密码 加入

sattributes = sattributes & "Trusted_Connection=ture" & Chr$(0)

这样ok了

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页