Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a user in SQL Server Express database I added to my project?

How can I create a SQL user in a SQL Server Express database that I added to my project?

I need to create a user to use in a connection string that doesn't use Integrated Security.

like image 738
John Egbert Avatar asked Sep 17 '10 17:09

John Egbert


People also ask

How do I add a user to a database?

Expand the database in which to create the new database user. Right-click the Security folder, point to New, and select User.... In the Database User - New dialog box, on the General page, select one of the following user types from the User type list: SQL user with login.

Is SQL Server Express Multi User?

Make no mistake; you can use SQL Server Express to create a multiuser database. And because the product is free, it provides tremendous value for small businesses looking for an affordable product to build a database.


2 Answers

You would need to create a SQL Authenticated login first with CREATE LOGIN then add a user associated with that login to your database by using 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
like image 147
Martin Smith Avatar answered Oct 15 '22 11:10

Martin Smith


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:

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:

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:

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]
like image 36
Tom Andraszek Avatar answered Oct 15 '22 11:10

Tom Andraszek