Solved

How to Add Users to SQL Server database

Posted on 2012-03-21
7
176 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:micropc1
Comment Utility
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
Comment Utility
How do you give the user roles like DbOwner?
0
 
LVL 14

Expert Comment

by:quizwedge
Comment Utility
You can add roles using sp_addrolemember. See http://sqlserverplanet.com/security/add-user-to-role
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:VBBRett
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now