Mapping a user to a login

I have created a login and a user on a database using SMO. I am having trouble mapping a database to the login. When you create a login from the GUI using SQL Server Management Studio Express CTP the user is automatically created and the database is mapped to this login from the user mapping tab. When I create the login programmatically this is not the case so I need to create the user.  How do I map the database to the login. I set the default database property on the login but this does not do it.

Thanks,

Michael Gustafson

 



Answer this question

Mapping a user to a login

  • Dave B..

    what property of the Login is used to show what databases are mapped

    also is there a way with SMO to check and see if a password is blank Granted the odds of that are extemely slimmer with 2005.


  • jjswildfire

    This is the pattern you would normally follow (this is for a windows user, but it works the same for a SQL user):
     

    Login l = new Login(svr, @"REDMOND\mwories");
    l.LoginType =
    LoginType.WindowsUser;
    l.Create();

    Database db = svr.Databases["AdventureWorks"];

    User u = new User(db, @"REDMOND\mwories");
    u.Login = l.Name;
    u.UserType =
    UserType.SqlLogin;
    u.Create();

    This maps the database user to the login as you create the login.
     


  • Lee_Dale

    I found the DatabaseMapping() class and was able to find which logins were missing mappings. Im still at a lost at how to test for blank passwords using SQL2005 and SMO.

    Thanks for taking time to read this.

    Chuck Turner


  • Mapping a user to a login