How can I map a login to a database using T-SQL (not SSMS)

USE msdb;
GO
CREATE USER shims FROM LOGIN shims;
GO
ALTER ROLE SqlAgentUserRole ADD MEMBER shims;
GO

Also, for future reference, any time you know how to do something in the UI but not in a script, this is what the Script option on most dialogs is for - it will show you what script SSMS would have executed:

enter image description here


If you want to change the current\default database to a different one try:

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

Now, create a user for above login created

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

And now you can assign roles to the above create user for the login as below:

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