Click here to Skip to main content
Click here to Skip to main content

Creating SQL Server Login programmatically

By , 19 Nov 2009
Rate this:
Please Sign up or sign in to vote.


I got this question from Daniweb C# forum, and of course whenever I need to do something against SQL Server object from .NET I go to SMO (SQL Server Management Objects) which provides a great functionalities to do anything with SQL Server instead of going on SSMS and create some scripts then embedding them into stored procedure then call it…. off…

Anyway to avoid showing how much I’m talkative… 

Using the Code

Create any C# project type (Windows, Class Library or even Console one), add reference to Microsoft.SqlServer.Smo, Microsoft.SqlServer.ConnectionInfo and Microsoft.SqlServer.SqlEnum 

Here's the code to create SQL Server login (of type Windows User) 

// connects to the local server 
Server sqlServerInstance = new Server(
    new Microsoft.SqlServer.Management.Common.ServerConnection(
    new System.Data.SqlClient.SqlConnection(".")));
//initializes new login object on the server you connected by and
// we give the fullname of the login domain name \ user name
Login loginObj = new Login(sqlServerInstance, @"DomainName\UserName");
loginObj.DefaultDatabase = "Master"; // set the default database 
// here's the type of login WindowsUser, WindowsGroup, SqlLogin, Certificate
// or AsymmetricKey. 
// Be sure when you select WindowsUser to mention the domainname and username
loginObj.LoginType = LoginType.WindowsUser; 
loginObj.Enable(); //to enable the login 
loginObj.Create("password"); //set the password and create it on the server

If you used LoginType.WindowsUser, be sure to provide valid windows username and if you aren’t on Domain use the machine name instead. 

If you need to create SQL login use LoginType.SqlLogin… 

You can explore Login class more on 


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

About the Author

Ramy Mahrous
Software Developer LINK Development
Egypt Egypt
Business Intelligence Developer at LINK Development which is a wholly subsidiary of LINKdotNET, graduated from Faculty of Computers and Information | Helwan University, Cairo, Egypt | 2003 - 2007, Computer Science as a Major, he has a passion for community, latest knowledge delivering and Q&A forums.

Comments and Discussions

GeneralMy vote of 5 Pinmemberahmadramadan29-May-12 11:54 
GeneralRemote login PingroupMd. Marufuzzaman30-Dec-09 2:56 
What about login to the SQL Server in remote environments…. Probably your code snippets allow you to connect SQL Server in LAN environments.
Md. Marufuzzaman

Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

GeneralRe: Remote login PinmemberRamy Mahrous30-Dec-09 3:29 

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
Web04 | 2.8.140415.2 | Last Updated 19 Nov 2009
Article Copyright 2009 by Ramy Mahrous
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid