The server principal is not able to access the database under the current security context in SQL Server MS 2012

后端 未结 10 933
-上瘾入骨i
-上瘾入骨i 2020-12-13 01:46

I am trying to access my hosting server’s database through SQL Server Management Studio, everything till login is fine but when I use the command use myDatabase

10条回答
  •  长情又很酷
    2020-12-13 02:13

    I believe you might be missing a "Grant Connect To" statement when you created the database user.

    Below is the complete snippet you will need to create both a login against the SQL Server DBMS as well as a user against the database

    USE [master]
    GO
    
    CREATE LOGIN [SqlServerLogin] WITH PASSWORD=N'Passwordxyz', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON
    GO
    
    USE [myDatabase]
    GO
    
    CREATE USER [DatabaseUser] FOR LOGIN [SqlServerLogin] WITH DEFAULT_SCHEMA=[mySchema]
    GO
    
    GRANT CONNECT TO [DatabaseUser]
    GO
    
    -- the role membership below will allow you to run a test "select" query against the tables in your database
    ALTER ROLE [db_datareader] ADD MEMBER [DatabaseUser]
    GO
    

提交回复
热议问题