Peter Hansen Peter Hansen - 1 year ago 80
SQL Question

SQL Server login with Windows authentication

If I open SQL Server Management Studio, I am able to login just fine, but if I try in my code, it returns an error

login failed for user

This is my connection string:

this.ConnectionString = "Data Source=localhost;Initial Catalog=DanboligTest;User ID=dev\spInstall";

this.ConnectionString = "Data Source=localhost;Initial Catalog=DanboligTest;";

EDIT if I use the server explorer in VS then I can login fine.

No matter what I try Im not able to login like this. Can someone tell me what im doing wrong?

Answer Source

You need to add this line to you connection string

 Integrated Security=SSPI;

and remove the line

User ID=dev\spInstall

So all in all

 this.ConnectionString = "Integrated Security=SSPI; Data Source=localhost;Initial Catalog=DanboligTest;";