(可能)ODBC 中的非法字符 SQL 服务器连接字符串 PWD=

(Maybe) Illegal character in ODBC SQL Server Connection String PWD=

根据我的研究,SQL 服务器连接字符串的 PWD= 字段中没有非法字符。

但是,使用 SQL Server Express 2008 我将 SA 密码更改为 GUID,具体来说:

{85C86BD7-B15F-4C51-ADDA-3B6A50D89386}

所以当通过 ODBC 连接时,我使用这个连接字符串:

"Driver={SQL Server};Server=.\MyInstance;Database=Master;UID=SA;PWD={85C86BD7-B15F-4C51-ADDA-3B6A50D89386};"

但是由于 SA 登录失败而返回。

但是,如果我将 SA 密码更改为同样长但没有 {}- 的密码,它就会成功! PWD= 中是否有某些字符需要转义?我尝试了所有不同的组合,但没有成功。

Microsoft's documentation所述(强调)--

Connection strings used by ODBC have the following syntax:

connection-string ::= empty-string[;] | attribute[;] | attribute; connection-string

empty-string ::=

attribute ::= attribute-keyword=[{]attribute-value[}]

attribute-value ::= character-string

attribute-keyword ::= identifier

Attribute values can optionally be enclosed in braces, and it is good practice to do so. This avoids problems when attribute values contain non-alphanumeric characters. The first closing brace in the value is assumed to terminate the value, so values cannot contain closing brace characters.

我建议您在设置密码时简单地去掉大括号,然后您上面提供的连接字符串应该可以正常工作。

添加

我在 Microsoft 的网站上进行了深入研究,发现了一些可能相关的 ABNF rules --

 SC           = %x3B         ; Semicolon 
 LCB          = %x7B         ; Left curly brackets 
 RCB          = %x7D         ; Right curly brackets  
 EQ           = %x3D         ; Equal sign 
 ESCAPEDRCB   = 2RCB         ; Double right curly brackets 
 SpaceStr     = *(SP)        ; Any number (including 0) spaces
 ODBCConnectionString =  *(KeyValuePair SC) KeyValuePair [SC]
 KeyValuePair = (Key EQ Value / SpaceStr)
 Key = SpaceStr KeyName
 KeyName = (nonSP-SC-EQ *nonEQ)
 Value = (SpaceStr ValueFormat1 SpaceStr) / (ValueContent2)
 ValueFormat1 = LCB ValueContent1 RCB
 ValueContent1 = *(nonRCB / ESCAPEDRCB)
 ValueContent2 = SpaceStr / SpaceStr (nonSP-LCB-SC) *nonSC
 nonRCB = %x01-7C / %x7E- FFFF                                 ; not "}"
 nonSP-LCB-SC = %x01-1F / %x21-3A / %x3C-7A / %x7C- FFFF       ; not space, "{" or ";"
 nonSP-SC-EQ = %x01-1F / %x21-3A / %x3C / %x3E- FFFF           ; not space, ";" or "="
 nonEQ = %x01-3C / %x3E- FFFF                                  ; not "="
 nonSC = %x01-003A / %x3C- FFFF                                ; not ";"

...

ValueFormat1 is recommended to use when there is a need for Value to contain LCB, RCB, or EQ. ValueFormat1 MUST be used when the Value contains SC or starts with LCB.

ValueContent1 MUST be enclosed by LCB and RCB. Spaces before the enclosing LCB and after the enclosing RCB MUST be ignored.

ValueContent1 MUST be contained in ValueFormat1. If there is an RCB in the ValueContent1, it MUST use the two-character sequence ESCAPEDRCB to represent the one-character value RCB.

所有这些归结为...我相信以下连接字符串 应该 适合你(注意有 2 个 left/open 大括号和 3 个 right/close PWD 值上的大括号) --

"Driver={SQL Server};Server=.\MyInstance;Database=Master;UID=SA;PWD={{85C86BD7-B15F-4C51-ADDA-3B6A50D89386}}};"

根据 this page,名称中唯一合法的“特殊字符”(我认为他们在谈论 DSN)是下划线:

The ODBC specification (and the SQL specification) states that names must be in the format of " letter[digit | letter | _]...". The only special character allowed is an underscore.

没有提及“ODBC 规范”。 This page 说是 ODBC 4.0 规范。