& quot;用户登录失败"C#与SQLConnection [英] "Login failed for user" C# with SQLConnection

查看:60
本文介绍了& quot;用户登录失败"C#与SQLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to connect to my database (which is on the same computer as my code) through my C# code. The problem is I keep getting the "Login failed for user " "" error... I admit that my knowledge of connecting to databases is minimal and I've tried almost all the steps in other questions!

这是我的代码的一部分:

here's part of my code:

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" />

推荐答案

您需要更改连接字符串;

you need to change the connection string;

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

如果使用Windows身份验证连接到本地数据库,则需要设置 Trusted_Connection = True ;如果使用SQL Server身份验证,则需要声明 User Id = myUsername;.密码= myPassword; .

if you are using windows authentication to connect to the local DB, you need to set Trusted_Connection=True; if you are using SQL server authentication, you need to declare User Id=myUsername; Password=myPassword;.

这篇关于&amp; quot;用户登录失败&quot;C#与SQLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆