强制 MS Access ODBC 连接使用 SQL 服务器身份验证
Force MS Access ODBC Connection to use SQL Server Authentication
我正在使用来自我的 Autoexec 宏的 运行 以下代码来建立 ODBC 连接。
'Name : CreateDSNConnection
'Purpose : Create a DSN to link tables to SQL Server
'Parameters
' sServer: Name of SQL Server that you are linking to
' sDatabase: Name of the SQL Server database that you are linking to
' sUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
' sPassword: SQL Server user password
Public Function CreateDSNConnection(sServer As String, sDatabase As String, sUsername As String, sPassword As String) As Boolean
Dim sConnect As String
On Error GoTo CreateDSNConnection_Err
If Len(sUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
sConnect = "Description=DBTraining" & vbCr & "SERVER=" & sServer & vbCr & "DATABASE=" & sDatabase & vbCr & "Trusted_Connection=Yes"
Else
sConnect = "Description=DBTraining" & vbCr & "SERVER=" & sServer & vbCr & "DATABASE=" & sDatabase
End If
DBEngine.RegisterDatabase "DBTraining", "SQL Server", True, sConnect
'Add error checking.
CreateDSNConnection = True
Debug.Print "Connection made"
Exit Function
CreateDSNConnection_Err:
CreateDSNConnection = False
MsgBox "CreateDSNConnection encountered an unexpected error: " & Err.Description, vbCritical, "Unexpected Error!"
End Function
相当标准的东西...但似乎 ODBC 连接总是设置为使用 Windows 身份验证,我需要它设置为使用 SQL 服务器身份验证。我试过放入 UID=
和 PWD=
,但它仍然尝试使用 Windows.
这是从 Access 到 SQL 服务器的可信连接:
strConnectString = "Driver={SQL Server};" & _
"Server=" & strDSN & ";" & _
"Database=" & strDatabase & ";" & _
"Trusted_Connection=yes"
或 SQL 服务器身份验证:
strConnectString = "Driver={SQL Server};" & _
"Server=" & strDSN & ";" & _
"Database=" & strDatabase & ";" & _
"User Id=,myUID>;Password=<MyPWD>"
最终对我有用的是从头开始。
- 我从 ODBC 连接中删除了现有的 UserDSN。
- 然后打开我的数据库和 运行 CreateDSNConnection 代码
- 将 UserDSN 放回原处,并设置为使用 SQL 服务器身份验证,但使用的是我的 Windows 登录名
- 删除了所有链接表和视图
- 使用通用 SQL 登录帐户重新创建表和视图的链接并选中保存密码选项
- 压实和修复
- 已发送给用户进行测试...有效
我已经多次尝试了这些步骤中的每一个...除了第一个。我猜 UserDSN 文件中有什么东西损坏了???我不知道。但是删除它并重新开始完全解决了这个问题。
Dim CurConn As ADODB.Connection
Set CurConn = New ADODB.Connection
CurConn.Open "Provider=myProvider;" & _
"Server=myServer;" & _
"Database=myDatabase;" & _
"Trusted_Connection=no;" & _
"User Id=myUsername;" & _
"Password=myPassword;"
这个,我在Access中是怎么做的VBA。
我正在使用来自我的 Autoexec 宏的 运行 以下代码来建立 ODBC 连接。
'Name : CreateDSNConnection
'Purpose : Create a DSN to link tables to SQL Server
'Parameters
' sServer: Name of SQL Server that you are linking to
' sDatabase: Name of the SQL Server database that you are linking to
' sUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
' sPassword: SQL Server user password
Public Function CreateDSNConnection(sServer As String, sDatabase As String, sUsername As String, sPassword As String) As Boolean
Dim sConnect As String
On Error GoTo CreateDSNConnection_Err
If Len(sUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
sConnect = "Description=DBTraining" & vbCr & "SERVER=" & sServer & vbCr & "DATABASE=" & sDatabase & vbCr & "Trusted_Connection=Yes"
Else
sConnect = "Description=DBTraining" & vbCr & "SERVER=" & sServer & vbCr & "DATABASE=" & sDatabase
End If
DBEngine.RegisterDatabase "DBTraining", "SQL Server", True, sConnect
'Add error checking.
CreateDSNConnection = True
Debug.Print "Connection made"
Exit Function
CreateDSNConnection_Err:
CreateDSNConnection = False
MsgBox "CreateDSNConnection encountered an unexpected error: " & Err.Description, vbCritical, "Unexpected Error!"
End Function
相当标准的东西...但似乎 ODBC 连接总是设置为使用 Windows 身份验证,我需要它设置为使用 SQL 服务器身份验证。我试过放入 UID=
和 PWD=
,但它仍然尝试使用 Windows.
这是从 Access 到 SQL 服务器的可信连接:
strConnectString = "Driver={SQL Server};" & _
"Server=" & strDSN & ";" & _
"Database=" & strDatabase & ";" & _
"Trusted_Connection=yes"
或 SQL 服务器身份验证:
strConnectString = "Driver={SQL Server};" & _
"Server=" & strDSN & ";" & _
"Database=" & strDatabase & ";" & _
"User Id=,myUID>;Password=<MyPWD>"
最终对我有用的是从头开始。
- 我从 ODBC 连接中删除了现有的 UserDSN。
- 然后打开我的数据库和 运行 CreateDSNConnection 代码
- 将 UserDSN 放回原处,并设置为使用 SQL 服务器身份验证,但使用的是我的 Windows 登录名
- 删除了所有链接表和视图
- 使用通用 SQL 登录帐户重新创建表和视图的链接并选中保存密码选项
- 压实和修复
- 已发送给用户进行测试...有效
我已经多次尝试了这些步骤中的每一个...除了第一个。我猜 UserDSN 文件中有什么东西损坏了???我不知道。但是删除它并重新开始完全解决了这个问题。
Dim CurConn As ADODB.Connection
Set CurConn = New ADODB.Connection
CurConn.Open "Provider=myProvider;" & _
"Server=myServer;" & _
"Database=myDatabase;" & _
"Trusted_Connection=no;" & _
"User Id=myUsername;" & _
"Password=myPassword;"
这个,我在Access中是怎么做的VBA。