我在用OLEDB方式连接SQL Server时, 用到连接字: "provider=SQLOLEDB.1;data source=localhost;initial catalog=WroxBooks; Trusted_Connection=yes; User ID=sa;Password=;"
调用后一直无法正常连接。
ASP.NET错误描述:
* Error while accessing data.
用户 'CXY\ASPNET' 登录失败。
ASP错误描述:
Microsoft OLE DB Provider for SQL Server (0x80040E4D)
用户 'CXY\IUSR_CXY' 登录失败。
已确信所连接的数据源,帐号和口令都是正确的,根据出错描述,怎么连接会用
CXY\ASPNET或CXY\IUSR_CXY登录?(我已明确指定User ID=sa;Password=;),经过多
次试验,最后将连接字中的Trusted_Connection=yes;删除,连接成功!
如果明白SQL Server登录验证的原理, 就可以知道Trusted Connection的含义。
在SQL Server Architecture(SQL Server架构)登录篇中有关于Authenticating Logins(登录验证)的讲述, 摘录如下:
......
SQL Server 2000 uses two types of authentication: Windows Authentication
and SQL Server Authentication.
......
When using Windows Authentication, you do not have to specify a login ID
or password when you connect to SQL Server 2000. Your access to SQL
Server 2000 is controlled by your Windows NT or Windows 2000 account or
group, which is authenticated when you log on to the Windows operating
system on the client.
When you connect, the SQL Server 2000 client software requests a Windows
trusted connection to SQL Server 2000. Windows does not open a trusted
connection unless the client has logged on successfully using a valid
Windows account. The properties of a trusted connection include the
Windows NT and Windows 2000 group and user accounts of the client that
opened the connection. SQL Server 2000 gets the user account information
from the trusted connection properties and matches them against the
Windows accounts defined as valid SQL Server 2000 logins. If SQL Server
2000 finds a match, it accepts the connection. When you connect to SQL
Server 2000 using Windows 2000 Authentication, your identification is
your Windows NT or Windows 2000 group or user account.
SQL Server 2000采用两种不同的验证方式:Windows验证和SQL Server 2000验证
用Windows验证连接时,不必指定一个用户ID及口令,连接验证使用Windows NT或2000的组帐号(group account)。
要知道:SQL Server 2000 在连接验证之前,用户先用Windows NT或2000的组帐号在客户端成功登录至SQL Server 2000服务器,方能建立一个信任连接(Trusted connection),SQL Server从信任连接属性中获取用户的帐号信息,将其与Windows已定义的帐号信息匹配和分析,如果正确就连接成功,并将此Windows帐号作为连接至SQL Server 2000的用户ID。
上文中的连接字中包含有:Trusted_Connection=yes;
这就意味着连接将采用信任连接方式,但由于连接前没有用Windows组帐号(在ASP环境中是访问IIS服务帐号IUSR_计算机名,在ASP.NET环境中帐号是ASPNET)登录至SQL Server 2000服务器, 也就是说没有建立一个信任连接(Trusted connection),当然,SQL Server 2000连接也不能够成功。
将Trusted_Connection=yes;删除或改为Trusted_Connection=no;
这将不采用信任连接方式(也即不采用Windows验证方式),而改由SQL Server 2000验证方式,即在连接字中指定:User ID=user name;Password=user password;
SQL Server 2000会将此用户ID和口令进行验证连接,而与Windows帐号无关。
关于SQL Server 2000上述两种验证方式的设置(连接验证采用Windows和SQL Server验证,还是仅采用Windows验证),可以启动SQL Server 2000企业管理器,然后在安全性配置中设置。