Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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. 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" />
like image 765
Lourens Avatar asked Jan 28 '13 17:01

Lourens


People also ask

How do I fix user login failed?

Login failed for user '(null)' The main reason for this condition is that the SPN is associated with the wrong account. To fix the issue: Use the Kerberos Configuration Manager or Setspn.exe to diagnose and fix SPN-related issues.

What is Trusted_connection true?

NO - trusted_connection=true means Windows Authentication and Windows Authentication requires trusted_Connection=true. If you specify "trusted_connection=True" ==> you have Windows Authentication; if you don't specify it, you don't have Windows Authentication. – marc_s.


2 Answers

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;.

like image 56
daryal Avatar answered Oct 03 '22 17:10

daryal


I would recommend do this:

1) Change connection string to:

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

'Server=.' - default instance of SQL Server on your machine is used,

'Trusted_Connection=True' - Windows Authentication is used to validate your access to SQL Server instance.

2) Check in Sql Management Studio is your windows user has permissions to access '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();
    }
}
like image 23
Bartłomiej Mucha Avatar answered Oct 03 '22 17:10

Bartłomiej Mucha