"Login failed for user" C # with SQLConnection

I am trying to connect to my database (which is located on the same computer as my code) through my C # code. The problem is that I always get the error "Login failed for the user" ... I admit that my knowledge of connecting to databases is minimal, and I tried almost all the steps in other matters!

here is 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" /> 
+8
c # sql database sqlconnection
source share
4 answers

You need to change the connection string;

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

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

+17
source share

I would recommend doing this:

1) Change the connection string to:

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

"Server =. - using an instance of SQL Server on your computer,

'Trusted_Connection = True' - Windows authentication is used to verify your access to the instance of SQL Server.

2) Check Sql Management Studio - your Windows user has access rights to the database1.

The second error you get is because you have to add '@' to the parameter name, for example:

 command.Parameters.AddWithValue("@email", email); command.Parameters.AddWithValue("@password", password); 

I also recommend that you change your code as follows:

 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(); } } 
+5
source share

Try as shown below ... this 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(); 
+1
source share

Change the 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 a mistake,

Test your sql services and protocols in SQL Server Configuration Manager.

0
source share

All Articles