如何在添加到项目中的 SQL Server Express 数据库中创建用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737449/
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
How can I create a user in SQL Server Express database I added to my project?
提问by John Egbert
How can I create a SQL user in a SQL Server Express database that I added to my project?
如何在添加到项目中的 SQL Server Express 数据库中创建 SQL 用户?
I need to create a user to use in a connection string that doesn't use Integrated Security.
我需要创建一个用户以在不使用集成安全性的连接字符串中使用。
回答by Martin Smith
You would need to create a SQL Authenticated login first with CREATE LOGINthen add a user associated with that login to your database by using CREATE USER.
您需要先使用CREATE LOGIN创建一个 SQL Authenticated 登录名,然后使用CREATE USER将与该登录名关联的用户添加到您的数据库中。
USE [master]
GO
CREATE LOGIN [JohnEgbert] WITH PASSWORD=N'YourPassword',
DEFAULT_DATABASE=[YourDB], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [YourDB]
GO
CREATE USER [JohnEgbert] FOR LOGIN [JohnEgbert] WITH DEFAULT_SCHEMA=[dbo]
GO
回答by Tom Andraszek
If you create a SQL login and a SQL user without errors, but then get an error when trying to connect, you may have the SQL Authentication mode disabled. To check, run:
如果您创建的 SQL 登录名和 SQL 用户没有错误,但在尝试连接时出现错误,则您可能禁用了 SQL 身份验证模式。要检查,请运行:
SELECT SERVERPROPERTY('IsIntegratedSecurityOnly')
If this returns 1, then SQL Authentication (mixed mode) is disabled. You can change this setting using SSMS, regedit, or T-SQL:
如果返回 1,则 SQL 身份验证(混合模式)被禁用。您可以使用 SSMS、regedit 或 T-SQL 更改此设置:
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2
Then restart the SQL Server service, and create a login and a user, here with full permissions:
然后重启SQL Server服务,并创建一个登录名和一个用户,这里有完全权限:
CREATE LOGIN myusername WITH PASSWORD=N'mypassword',
DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
EXEC sp_addsrvrolemember 'myusername', 'sysadmin'
CREATE USER myusername FOR LOGIN myusername WITH DEFAULT_SCHEMA=[dbo]