[英]“Login failed for user” C# with SQLConnection
I've been trying to connect to my database (which is on the same computer as my code) through my C# code. 我一直在尝试通过我的C#代码连接到我的数据库(与我的代码位于同一台计算机上)。 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();
and this is my connection string: 这是我的连接字符串:
<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;/>
if you are using windows authentication to connect to the local DB, you need to set Trusted_Connection=True
; 如果您使用Windows身份验证连接到本地数据库,则需要设置Trusted_Connection=True
; if you are using SQL server authentication, you need to declare User Id=myUsername; Password=myPassword;
如果您使用的是SQL Server身份验证,则需要声明User Id=myUsername; Password=myPassword;
User Id=myUsername; Password=myPassword;
. 。
I would recommend do this: 我建议这样做:
1) Change connection string to: 1)将连接字符串更改为:
<add name="SQLServerConnection" connectionString="Server=.;Database=database1;Trusted_Connection=True;"/>
'Server=.' '服务器='。 - default instance of SQL Server on your machine is used, - 使用计算机上的默认SQL Server实例,
'Trusted_Connection=True' - Windows Authentication is used to validate your access to SQL Server instance. 'Trusted_Connection = True' - Windows身份验证用于验证您对SQL Server实例的访问权限。
2) Check in Sql Management Studio is your windows user has permissions to access 'database1'. 2)签入Sql Management Studio是您的Windows用户有权访问'database1'。
The second error you are getting because you should add '@' in name of parameter like this: 你得到的第二个错误,因为你应该在参数名称中添加'@',如下所示:
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@password", password);
I would also recommend that you change your code like this: 我还建议你改变你的代码:
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString))
{
using (var 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();
var result = command.ExecuteScalar();
}
}
Change your connection string. 更改连接字符串。
<add name="SQLServerConnection" connectionString="Server=.;Database=database1;Trusted_Connection=True;"/>
If you use server authentication, 如果使用服务器身份验证,
<add name="SQLServerConnection" connectionString="Server=.;Database=database1; UserId = Username; Password = Password;"/>
If you still have error, 如果你还有错误,
Check your sql services and protocols in sql server configuration manager. 检查sql server配置管理器中的sql服务和协议。
Try like below... it will help you.. 尝试如下...它会帮助你..
string email ="YourEmail@sample.com" //Give your email id to check
string password ="Test" //Give your Password to check
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM user WHERE EMAILADRES = @email and WACHTWOORD = @password"
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@password", password);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
MessageBox.Show("Exists");
else
MessageBox.Show("Not Exists");
connection.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.