Click here to Skip to main content

How to create Database User programmatically?

Well... we need to assign a server login to has access on a database, let's say the SQL Server Login we had created programmatically should has access on database "Database1" and for some reasons we should do that programmatically Well. Create any C# project type (Windows, Class Library or even Cons
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
See more: C#SQL-Server

Well... we need to assign a server login to has access on a database, let's say the SQL Server Login we had created programmatically should has access on database "Database1" and for some reasons we should do that programmatically Well. Create any C# project type (Windows, Class Library or even Console one), add reference to Microsoft.SqlServer.Smo, and Microsoft.SqlServer.ConnectionInfo

/// <summary>
/// Creates database user
/// </summary>
private void CreateDatabaseUser(Database database, String username)
{
    // initializes new User object and we say to which database it belongs
    // and its name
    User sqlServerUser = new User(datawarehouseDatabase, username); 
    sqlServerUser .UserType = UserType.SqlLogin; //SqlLogin not anything else
    //associated the user to login name, login name should be valid login name
    sqlServerUser .Login = "login_name"; 
    // here's we create the user on the database and till now the user
    // don't have any permission on database objects
    sqlServerUser .Create();
    //or any role like db_databasereader, db_databasewriter,...
    sqlServerUser .AddToRole("db_owner"); 
}

P.S: There's catastrophic mistake in Microsoft documentation of User.Login as they documented it as ReadOnly field although it's not.

sqllogin

How we can call the above method?

Server sqlInstance = new Server(
new Microsoft.SqlServer.Management.Common.ServerConnection(
new System.Data.SqlClient.SqlConnection("."))); //connects to the local server
//initialize new object from database  adventureworks
Database database = sqlInstance.Databases["adventureworks"]; 
//creates user RamyMahrous on database adventureworks
CreateDatabaseUser(database, "RamyMahrous");
Posted 17 Nov '09
Edited 19 Nov '09


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. Christian Graus (1,199)
  2. SAKryukov (645)
  3. CRDave1988 (294)
  1. SAKryukov (10,679)
  2. Christian Graus (7,188)
  3. OriginalGriff (5,239)
  4. Abhinav S (4,575)
  5. thatraja (4,466)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to create table sp and all function after create database PinmemberMember 448722922:35 20 Dec '09  
AnswerRe: how to create table sp and all function after create database PinmemberRamy Mahrous17:22 22 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 19 Nov 2009
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid