简体   繁体   English

使用ADO连接从VBA连接到SQL Server数据库时遇到问题

[英]Having trouble connecting to SQL Server database from VBA using ADO connection

I want to connect to a SQL Server database without using DSN. 我想不使用DSN连接到SQL Server数据库。 I'm receiving an error when trying to connect to said database. 尝试连接到所述数据库时出现错误。 The error is 错误是

Run-time error '-2147467259 (80004005)': [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 运行时错误“ -2147467259(80004005)”:[Microsoft] [ODBC驱动程序管理器]找不到数据源名称,未指定默认驱动程序

My connection string looks like this: 我的连接字符串如下所示:

"ODBC;DRIVER=SQL Server;SERVER=lokdb; UID=secret;PWD=secret;APP=Microsoft Open Database Connectivity;WSID=" & Environ$("COMPUTERNAME") & ";Network=DBMSSOCN;Trusted_Connection=Yes;DATABASE=EDMS-Lok"`

I am able to connect to the default database on the server with the credentials specified if I remove the DATABASE=EDMS-Lok parameter from the connection string, however I don't then seem to be able to "find" the EDMS-Lok database. 如果我从连接字符串中删除DATABASE=EDMS-Lok参数,则可以使用指定的凭据连接到服务器上的默认数据库,但是我似乎无法“找到” EDMS-Lok数据库。 Eg if I try using 例如,如果我尝试使用

SELECT * FROM EDMS-Lok.dbo.eng_dwg

I get an error message 我收到一条错误消息

Run-Time error '-2147217900 (80040e14)': [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '-' 运行时错误'-2147217900(80040e14)':[Microsoft] [ODBC SQL Server驱动程序] [SQL Server]'-'附近的语法不正确

( eng_dwg is a valid table) eng_dwg是有效表)

When I connect without specifying a database, the .DefaultDatabase property of the ADO connection object returns "020" which I don't even see as an available database on that server?? 当我在未指定数据库的情况下进行连接时,ADO连接对象的.DefaultDatabase属性将返回“ 020”,我什至看不到该服务器上的可用数据库?

When connected, if I use SELECT name FROM sys.databases I'm able to get a list of the databases available. 连接后,如果我使用SELECT name FROM sys.databases ,则可以获取可用数据库的列表。 They are: 他们是:

  • 010 010
  • 100 100
  • 500 500
  • EDMS-Lok EDMS-乐
  • Master
  • msdb 数据库
  • MSLLockDB MSLLockDB
  • pubs 酒馆
  • tempdb 临时数据库

I got it working using DSN, but I don't want to use DSN. 我使用DSN可以正常工作,但是我不想使用DSN。

Not sure what is wrong here?? 不确定这里出什么问题了吗?

I would say the problem is that you mix up the authentication models. 我要说的问题是,您混淆了身份验证模型。 You provide user + password but also you have configured trusted connection = yes. 您提供用户名+密码,但您还配置了受信任的连接=是。 I'm not sure how ADO works in that case but I would assume that it doesn't use the configured user and connects with the windows account. 我不确定在这种情况下ADO的工作方式,但我会假定它不使用配置的用户并与Windows帐户连接。 Delete the trusted connection parameter in the connection string and try again. 删除连接字符串中的可信连接参数,然后重试。

OK, credit for this answer should go to Dee and Sam who commented on the original question, however I can't mark a comment as the answer, so here it is: 好的,这个答案应该归功于Dee和Sam在原始问题上发表了评论,但是我不能将评论标记为答案,因此这里是:

Needed to wrap the database name in square brackets [ ] ... like this DATABASE=[EDMS-Lok] - (credit to DEE) 需要将数据库名称包装在方括号[] ...中,例如DATABASE = [EDMS-Lok]-(贷记为DEE)

I first tried wrapping the database name in the connection string ie DATABASE=[EDMS-Lok] and this did not work (same error). 我首先尝试将数据库名称包装在连接字符串中,即DATABASE = [EDMS-Lok],但此操作不起作用(相同错误)。 However, if I connect without specifying the database, I'm able to access the table using SELECT * FROM [EDMS-Lok].dbo.eng_dwg 但是,如果我在未指定数据库的情况下进行连接,则可以使用SELECT * FROM [EDMS-Lok] .dbo.eng_dwg访问该表。

It didn't work in the connection string because the string is not T-SQL code. 它在连接字符串中不起作用,因为该字符串不是T-SQL代码。 To get it to work, you may need to figure out how to escape the dash character. 要使其正常工作,您可能需要弄清楚如何对破折号进行转义。 – (credit to SAM) –(归功于SAM)

I've tried single and double quotes, backslash, square brackets, parentheses and curly brackets and nothing seems to work to escape the hyphen in the database name within the connection string. 我已经尝试过使用单引号和双引号,反斜杠,方括号,括号和大括号,但似乎无法解决连接字符串中数据库名称中的连字符问题。 But, since I can now access the database and the tables I need, which was the original question, I wanted to mark it as answered. 但是,由于我现在可以访问所需的数据库和表(这是原始问题),因此我希望将其标记为已回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM