title: SQL Server: The server principal is not able to access the database under the current security context / windows authentication for the login
body: <p>I saw a couple of other questions like this one but no one could solve my issue.</p>
  
  <p>I have a SQL 2016 Express and a database <code>mydatabase</code>. I created a login using <code>Windows Authentication</code> and I used my AD user <code>domain\myuser</code>. In <code>mydatabase</code> -> security -> Users I created a user called <code>myuser</code> (User type = Windows user, Login name = <code>domain\myuser</code>). Default Schema is dbo and in the Securables menu I added <code>mytable</code> and gave the following rights: Alter, Delete, Insert, Select and Update.</p>
  
  <p>If I open SQL Management Studio as <code>domain\myuser</code>, connect to that SQL server, run <code>use mydatabase</code> and <code>select * from mytable</code>, everything works fine.</p>
  
  <p>Now, using a vbscript, from a command prompt running as <code>domain\myuser</code>, it will fail. The relevant part of the <code>myscript.vbs</code> script is this:</p>
  
  <pre><code>strConn = "PROVIDER=SQLOLEDB;Server=myserver\myinstance;Integrated Security=SSPI;"
  Set cxn = Wscript.CreateObject("ADODB.Connection")
  cxn.Open strConn
  cxn.CommandTimeout = 0 
  strSqlQuery = "use mydatabase"
  Set rs=cxn.Execute(strSqlQuery)
  </code></pre>
  
  <p>This will fail with:</p>
  
  <pre><code>myscript.vbs(6, 1) Microsoft OLE DB Provider for SQL Server:
  The server principal "domain\myuser" is not able to access the
  database "mydatabase" under the current security context.
  </code></pre>
  
  <p>I tried to add the <code>database=mydatabase</code> in the SQL connection string but it didn´t work either.</p>
  
  <p>Looking at some of the answers in other questions, some people asked to run the following command in the SQL Management Studio:</p>
  
  <pre><code>sp_change_users_login  @Action='Report';
  </code></pre>
  
  <p>It was not clear if that was with a db_owner or the affected user, so I ran with both. With the db_owner the result was the columns <code>UserName</code> and <code>UserSID</code> empty, whereas with <code>domain\myuser</code> it said:</p>
  
  <pre><code>Msg 15247, Level 16, State 1, Procedure sp_change_users_login, Line 51 [Batch Start Line 10]
  User does not have permission to perform this action.
  </code></pre>
  
  <p>Is my <code>Integrated Security</code> part in my script connection string wrong? How do I solve this?</p>