Solved

How to Add Users to SQL Server database

Posted on 2012-03-21
7
183 Views
Last Modified: 2012-06-27
How do you add users to a SQL Server Database using C#?  I want to add 5 users to a database with a domain\user format.  Thanks!
0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37750124
execute a CREATE LOGIN/CREATE USER statement...

http://msdn.microsoft.com/en-us/library/ms173463.aspx

delete them using DROP USER

http://msdn.microsoft.com/en-us/library/ms189438.aspx
0
 

Author Comment

by:VBBRett
ID: 37750144
How do you give the user roles like DbOwner?
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 37750247
You can add roles using sp_addrolemember. See http://sqlserverplanet.com/security/add-user-to-role
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:VBBRett
ID: 37750264
And how do you specify the domain where the user is coming from given that when I add the user domain\\user I get the following error:  

I would like to add the following user:

Mycompanydomain\myuser as opposed to just myuser.
0
 

Author Comment

by:VBBRett
ID: 37750326
This is not working, especially that I am passing the query through my C# code to get this work.  Please help me find a solution so I can put this to rest.  I need the following written in C#

SqlCommand cmd = new SqlCommand("Create LOGIN " + usertobeadded + " USE " + databasename + " " +"CREATE USER " + usertobeadded + " FOR LOGIN " + usertobeadded " GO", conn);

I keep on getting with the '\' sign or the ';' sign.  What do I do?
0
 
LVL 14

Accepted Solution

by:
quizwedge earned 500 total points
ID: 37750960
My C# is a little rusty, but I'll try. See http://msdn.microsoft.com/en-us/library/ms187750.aspx for more information. I wanted to get the information to you quickly. If below doesn't work, I'll fire up my C# environment and make sure I get the syntax right.

//Create New User
SqlCommand cmd = new SqlCommand("CREATE USER " + NameForSQL + " FOR LOGIN [" + Domain + "\" + User + "]", conn);

//Add Role
SqlCommand2 cmd = new SqlCommand("EXEC sp_addrolemember '" + Role + "', '" + NameForSQL + "'", conn);
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37751007
I'm not a C# developer - I code mainly in Java - but I'm guessing it might be an issue with escaping the backslash character.

http://blog.akilles.org/2008/03/12/escaping-backslash-character-in-c-net-mysql-queries/
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question