“用户登录失败”C# 与 SQLConnection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14567939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Login failed for user" C# with SQLConnection
提问by Lourens
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!
我一直在尝试通过我的 C# 代码连接到我的数据库(与我的代码在同一台计算机上)。问题是我不断收到“用户登录失败”错误...我承认我对连接数据库的了解很少,而且我已经尝试了其他问题中的几乎所有步骤!
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" />
回答by daryal
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
; if you are using SQL server authentication, you need to declare User Id=myUsername; Password=myPassword;
.
如果您使用windows身份验证连接到本地数据库,则需要设置Trusted_Connection=True
;如果您使用 SQL 服务器身份验证,则需要声明User Id=myUsername; Password=myPassword;
.
回答by Bart?omiej Mucha
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();
}
}
回答by Pandian
Try like below... it will help you..
像下面这样尝试......它会帮助你..
string email ="[email protected]" //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();
回答by Night Hunter
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 服务和协议。