Database Problem

+3 Johann Rino · July 26, 2014
I am trying to connect C# to MySql, but there is a problem ..

Here it is :

Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root' @ 'localhost' (using password: YES)

Help me guys to fix this problem .. Thanks :)

Sorry for my Bad English..

Post a Reply


Oldest  Newest  Rating
+1 Pere Garau Burguera · July 26, 2014
Make sure you have the user root at localhost with the correct password.
+1 Johann Rino · July 26, 2014
I already do that, but i got the same error .
+2 Eugene Botma · July 26, 2014
I'm not extremely familiar with using MySQL with c#, but make sure that root is allowed to log in from 'localhost'. You can try as well.

Also, it is considered bad practice to log into the server with root using a program. you should create a user for the Database you are using, specify from where it is allowed to log in and use that instead of root.
+1 Johann Rino · July 26, 2014
Thank you for the help guys, I Finally found the error :)

here is my code in connecting C# to MySql


                string myConn = "datasource=localhost;port=3306;username=root;password=texx";
                string Query = "insert into mydatabase.clientinfo (ID,FirstName,LastName,EmailAddress,Password) values('" + this.IDNumber + "','" + this.FirstNameTxtBx.Text + "','" + LastNameTxtBx.Text + "','" + EmailAddTxtBx.Text + "','" + PasswordTxtBx.Text + "') ;";
                MySqlConnection mySqlConnection = new MySqlConnection(myConn);
                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
                mySqlDataAdapter.SelectCommand = new MySqlCommand(" select * mydatabase.clientinfo ;", mySqlConnection);
                MySqlCommandBuilder mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);
                MySqlCommand mySqlCommand = new MySqlCommand(Query, mySqlConnection);
                MySqlDataReader mySqlDataReader;
                    mySqlDataReader = mySqlCommand.ExecuteReader();
                    MessageBox.Show("Successfully Added");
                    while (mySqlDataReader.Read())

                catch (Exception ex2)
            catch (Exception ex3)
+2 Paul D · August 7, 2014
Wow, just wanted to let you know the code is wide open for sql injection attack. 

I would google "preventing sql injection in c#".  Glad you found your error and the code might be functioning but it really isn't safe code.
0 Johann Rino · September 20, 2014
Thanks for the info Paul. I'm just a beginner in c# and I'm just practicing how to use database in c#. 
  • 1



C# is an object-oriented language designed for improving the development of web applications.