TSQL to Map User to Database

Solution 1:

Change default database of a login:

alter login <loginname> with default_database = <dbname>;

Create a user in a database for a given login:

use <dbname>;
create user <username> from login <loginname>;

Make an user member of db_owner group:

use <dbname>
exec sp_addrolemember 'db_owner', '<username>';

Make a login 'dbo' of a database:

alter authorization on database::<dbname> to <loginname>;

Solution 2:

Officially, you want to create a database user that is mapped to a login. To do that, you would use:

Create User <username> For LOGIN <loginname>

This obviously requires that the login exist. After that you would then call:

exec sp_addrolemember 'db_owner', <username>

This presumes that the account with which you are connecting to the database has privileges to add members to the db_owner role.