添加登录名并使用 SQL Server 身份验证连接到 SQL [英] Add login and connect to SQL with SQL Server Authentication
问题描述
我想在 SQL Server 2008 中添加一个用户,这样我就可以使用 SQL Server 身份验证而不是 Windows 身份验证来连接到 SQL,并尝试使用此代码创建一个登录用户:
I want to add a user in SQL Server 2008 so I can use SQL Server Authentication instead Windows Authentication for connecting to SQL, and have tried this code to create a user with login:
CREATE login [newLog] with password = 'passnewLognewLog'
我完成了,但是当我想使用 SQL 身份验证连接到 SQL Server 时,我收到了这条消息
I get it done, but when I want to connect to SQL Server using SQL Authentication, I get this message
无法连接到Mydb"
附加信息:用户登录失败'新日志'.(微软 SQL 服务器,错误:18456)
Cannot connect to "Mydb"
additional information: Login failed for user 'newLog'. (Microsoft SQL Server, Error: 18456 )
我在这里错过了什么?
推荐答案
创建登录后,需要将用户添加到数据库中.此示例来自 CREATE USER
的 sql server 文档:
After creating the login, you need to add the user to the database. This example is from the sql server documentation for CREATE USER
:
CREATE LOGIN AbolrousHazem
WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
USE AdventureWorks2008R2;
CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
GO
编辑
为了测试,我运行了这个 T-SQL:
To test, I ran this T-SQL:
create login Foo with password ='f00';
go
use TestDB
create user Foo for Login Foo
go
并使用此连接字符串成功打开连接:
and opened a connection successfully using this connection string:
"Server=<server>; user id=Foo; password=f00; initial catalog=TestDB"
这篇关于添加登录名并使用 SQL Server 身份验证连接到 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!