Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new user in SQL Server 2012 that I can use in a connection string?

I wasn't sure if this was a SO or SU question.

I have freshly installed SQL Server 2012.

I have created a database.

I have a visual studio 2012 project, and I want to connect to the database using a connection string in my web config.

Since I only have two accounts (sa and my windows account) I want to create a new user that will only have access to this one database.

Google says, in SSMS, expand the database, right click Security and go to New User.

However, when I do this and try to create a user, I choose SQL user with login, enter a username and login name, and get the error:

'news_login' is not a valid login or you do not have permission

I've tried by logging into SSMS as 'sa', logging in as my windows account, and running SSMS with administrator permissions. I've tried a few different usernames, and to no avail.

I notice there is no password box, so I fear I am doing it all wrong. Any suggestions?

like image 790
NibblyPig Avatar asked May 05 '13 16:05

NibblyPig


People also ask

How do I create a SQL connection string?

You can either use the new operator to make that directly. For example: SqlConnection conn = new SqlConnection( new SqlConnectionStringBuilder () { DataSource = "ServerName", InitialCatalog = "DatabaseName", UserID = "UserName", Password = "UserPassword" }. ConnectionString );

How do I pass UserName and password in SQL connection string?

Using a non-standard portServer=myServerName,myPortNumber;Database=myDataBase;User Id=myUsername;Password=myPassword; The default SQL Server port is 1433 and there is no need to specify that in the connection string.

What is a connection string SQL Server?

The connection string is an expression that contains the parameters required for the applications to connect a database server. In terms of SQL Server, connection strings include the server instance, database name, authentication details, and some other settings to communicate with the database server.


2 Answers

Before you add the user to the db, add the user to the server -- under the server, right click the security folder, and try it that way. Then you can grant permissions to the appropriate databases to that user

like image 167
sgeddes Avatar answered Sep 26 '22 20:09

sgeddes


Here's SQL way of doing this:

CREATE LOGIN NewUser        WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';   GO      CREATE USER NewUser FOR LOGIN NewUser;   GO   

EDIT: As per comment by @bschipp

Another example that does not expire password would be following

CREATE LOGIN NewUser        WITH PASSWORD = 'Uuxwp7Mcxo7Khy$#' CHECK_EXPIRATION = OFF, CHECK_POLICY = OFF; GO     CREATE USER NewUser FOR LOGIN NewUser;   GO   

more on options you can read here.

like image 37
Matas Vaitkevicius Avatar answered Sep 25 '22 20:09

Matas Vaitkevicius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!