How to Add Users to SQL Server database

Posted on 2012-03-21
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!
Question by:VBBRett
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

Expert Comment

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

delete them using DROP USER

Author Comment

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

Expert Comment

ID: 37750247
You can add roles using sp_addrolemember. See
Independent Software Vendors: 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

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.

Author Comment

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?
LVL 14

Accepted Solution

quizwedge earned 500 total points
ID: 37750960
My C# is a little rusty, but I'll try. See 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);

Expert Comment

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.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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