Click here to Skip to main content

Creating SQL Server Login programmatically

IntroductionI 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 scri
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

Introduction

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 http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.login.create.aspx 

Posted 17 Nov '09
Edited 19 Nov '09


Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Alternative 1

SQL Server Management Objects (SMO) are objects designed for programmatic management of Microsoft SQL Server.

We can integrate SMO into any .NET based applications.
SMO is also compatible with SQL Server version 7.0, SQL Server 2000, and SQL Server 2005, which makes it easy to manage a multi-version environment.
Following code is used to run the Microsoft SQL Server Query files(script files) kept in
applications bin\Debug folder, with SMO.SQL Server Management Objects (SMO) are objects designed for programmatic management of Microsoft SQL Server.

http://www.mindfiresolutions.com/Run-the-existing-SQL-Script-programmatically-with-SMO-526.php[^]
  Permalink  

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,114)
  2. SAKryukov (607)
  3. CRDave1988 (412)
  1. SAKryukov (10,433)
  2. Christian Graus (7,113)
  3. OriginalGriff (5,166)
  4. Abhinav S (4,550)
  5. thatraja (4,376)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRemote login PingroupMd. Marufuzzaman3:56 30 Dec '09  
GeneralRe: Remote login PinmemberRamy Mahrous4:29 30 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
Web02 | 2.5.120210.1 | Last Updated 25 Mar 2010
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid