Link to home
Start Free TrialLog in
Avatar of VBBRett
VBBRett

asked on

How to Add Users to SQL Server database

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!
Avatar of Chris Ashcraft
Chris Ashcraft
Flag of United States of America image

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
Avatar of VBBRett
VBBRett

ASKER

How do you give the user roles like DbOwner?
You can add roles using sp_addrolemember. See http://sqlserverplanet.com/security/add-user-to-role
Avatar of VBBRett

ASKER

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.
Avatar of VBBRett

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of quizwedge
quizwedge
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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/