The database dbname is not accessible. (Object Explorer)
Actually, I wouldn’t write an article for this error, but when I saw that it was the most searched article for another and a similar error, I wanted to write it.
You may receive this error when you try to open one of the databases under Databases in the Object Explorer window in SQL Server Management Studio. The probable cause is that you do not have access to the database you are trying to open.
You must request authorization for your SQL Login to access the relevant database. In most cases, only access is not sufficient. You will also need authorizations such as read (db_datareader) and write (db_datawriter).
Of course you need to determine the authority you need, I just made a very general description.
In the meantime, let me tell you what I mean by the access authorization. I’m talking about having a user in the relevant database. You can create it as follows:
CREATE USER [UserName] FOR LOGIN [LoginName] WITH DEFAULT_SCHEMA=[dbo];
The following user will be created by mapping to the specified Login and will now be able to access the database. However, you should consider that authorizations such as read and write will be given by separate commands. By default, the following user will only be created as a member of the Public role. In most databases, the Public role is not authorized. That’s the right thing.