Create new SQL Server 2008 R2 login from C # program

I did a C # front end for a database on SQL Server 2008 R2.

Is there a way to create sql server account from foreground program or do they need to be created in sql management studio?

thank

-1


source to share


4 answers


Yes, you can do it with SQL code. Take a look at the CREATE LOGIN documentation .

Here is some sample SQL code:



CREATE LOGIN <login_name> WITH PASSWORD = '<password>' MUST_CHANGE

+2


source


You can use Smo http://msdn.microsoft.com/en-us/library/ms162169.aspx



See this thread for more information http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/54293150-4289-4e65-a2dc-a642e9a54f1e

0


source


yes, you can create a login in your C # app.

this can be done by executing the required SQL statements in your application or by using SQL Server Management Objects (SMO).

for MSDN SQL , CREATE LOGIN .

for SMO MSDN, Managing Users, Roles, and Logins Using SMO Examples

0


source


you can use TSQL or smo. i used smo for my last project:

    public void CreateLogin(string name, string password, string defaultDatabase, string[] roles)
    {
        Login login = new Login(_server, name);
        login.LoginType = LoginType.SqlLogin;
        login.DefaultDatabase = defaultDatabase;

        login.PasswordExpirationEnabled = false;
        login.PasswordPolicyEnforced = false;

        login.Create(password, LoginCreateOptions.None);


        for (int i = 0; i < roles.Length; i++)
            login.AddToRole(roles[i]);

        login.Alter();

        login.Enable();

        login.Alter();
    }

      

0


source







All Articles