Excel VBA 到 SQL Server ADODB 连接 - 提示输入用户 ID 和密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15940680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:33:14  来源:igfitidea点击:

Excel VBA to SQL Server ADODB Connection - Prompt for User ID and Password

sql-serverexcel-vbavbaexcel

提问by user1955215

Excel VBA

excel VBA

The following ADO DB Connection string works and fetches data from the database into Excel

以下 ADO DB 连接字符串有效并将数据从数据库中提取到 Excel 中

Const rspADO As String = "Provider=SQLOLEDB.1;" & _
"Persist Security Info=False;" & _
"Initial Catalog=MyDatabase;" & _
"Data Source=118.aaa.yy.xx;" & _
"User ID=Username;Password=password;"

How do I prompt the user for input of Username and Password (at runtime) using the Data connection prompt (and not the Inputbox or a Userform in Excel)?

如何使用数据连接提示(而不是 Excel 中的输入框或用户表单)提示用户输入用户名和密码(在运行时)?

Thanks in advance for the help.

在此先感谢您的帮助。

回答by glh

If you use the ado db.connectionyou can according to this vba express post, code extract:

如果你使用ado db.connection你可以根据这个vba express post,代码提取:

Dim dbConnectStr As String
Set con = New ADODB.Connection

dbConnectStr = "Provider=msdaora;Data Source=" & "Oracle_Database_Name;"

con.ConnectionString = dbConnectStr
con.Properties("Prompt") = adPromptAlways
con.Open dbConnectStr 'ConnectionString

Dim dbConnectStr As String
Set con = New ADODB.Connection

dbConnectStr = "Provider=msdaora;Data Source=" & "Oracle_Database_Name;"

con.ConnectionString = dbConnectStr
con.Properties("Prompt") = adPromptAlways
con.Open dbConnectStr 'ConnectionString

I've also found you mayneed to set the Promptproperty to adPromptComplete.

我还发现您可能需要将该Prompt属性设置为adPromptComplete.