SQL 使用 Windows 身份验证的服务器登录

SQL Server login with Windows authentication

如果我打开 SQL Server Management Studio,我可以正常登录,但是如果我在我的代码中尝试,它 returns 一个错误

login failed for user

这是我的连接字符串:

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

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

编辑 如果我在 VS 中使用服务器资源管理器,那么我可以正常登录。

无论我怎么尝试,我都无法像这样登录。谁能告诉我我做错了什么?

您需要将此行添加到您的连接字符串

 Integrated Security=SSPI;

并删除行

User ID=dev\spInstall

总而言之

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