c# - "Login failed for user"C# 与 SQLConnection

标签 c# sql database sqlconnection

我一直在尝试通过我的 C# 代码连接到我的数据库(与我的代码位于同一台计算机上)。问题是我不断收到“用户登录失败”错误...我承认我对连接数据库的了解很少,而且我已经尝试了其他问题中的几乎所有步骤!

这是我的部分代码:

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);
        SqlCommand command = connection.CreateCommand();
        command.CommandText = @"IF EXISTS
                            (
                              SELECT *
                              FROM user 
                              WHERE EMAILADRES = @email and WACHTWOORD = @password
                            ) SELECT CAST (1 as bit) 
                            ELSE
                              SELECT CAST(0 as bit)";
        command.Parameters.AddWithValue("email", email);
        command.Parameters.AddWithValue("password", password);


        connection.Open();
        object ReturnBool = command.ExecuteScalar();
        connection.Close();

这是我的连接字符串:

<add name="SQLServerConnection" connectionString="Server=localhost; Database=database1;uid=NT AUTHORITY\NETWORK SERVICE" />

最佳答案

您需要更改连接字符串;

<add name="SQLServerConnection" connectionString="Server=localhost;Database=database1;Trusted_Connection=True;/>

如果使用windows认证连接本地DB,需要设置Trusted_Connection=True;如果您使用的是 SQL 服务器身份验证,则需要声明 User Id=myUsername; Password=我的密码;.

关于c# - "Login failed for user"C# 与 SQLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567939/

相关文章:

c# - Action 过滤器未被调用 WebAPI/.net Core

sql server 将日期时间转换为另一个时区?

如果字段重复,Php sql 只回显一行

mysql - 将带有 InnoDB 和 MyISAM 表的 MyISAM 数据库迁移到新服务器

database - 如何计算日期但排除 Microsoft Access 中的周末?

c# - 通过带有前缀的 yaml 从 keyvault 动态获取多个 secret

c# - 在不重启应用程序的情况下处理 URL 协议(protocol)

c# - 字典在同一项目中包含键和值

java - Java/Scala内存统计数据库如何实现?

sql-server - 基于年份的主键?