我一直在尝试通过我的 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/