Search code examples
c#mysql

Why do I get an error like this while connecting to MySQL database?


When I run this code, I get the following error:

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

I see two problems here, first being that I am not logging with user 'root', and second being that I typed my password in, even though it says that I'm not using password.

Purpose of this code is to show a table 'vozilo' in DataGridView which is in form 'test'.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace sketch
{
    public partial class Home : Form
    {

        public Home()
        {
            InitializeComponent();
        }


        private void btnTest_Click_1(object sender, EventArgs e)
        {
            MySql.Data.MySqlClient.MySqlConnection conn;

            try
            {
                conn = new MySql.Data.MySqlClient.MySqlConnection();
                conn.ConnectionString = "database=autosalon;server=localhost;user id=user;pwd=user";
                conn.Open();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

            test test = new test();
            test.Show();
            this.Hide();
        }
    }
}

Here is the code from 'test' form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sketch
{
    public partial class test : Form
    {
        public test()
        {
            InitializeComponent();
        }

        private void test_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'autosalonDataSet.vozilo' table. You can move, or remove it, as needed.
            this.voziloTableAdapter.Fill(this.autosalonDataSet.vozilo);
        }
    }
}

Solution

  • Solution to my problem was next:

    I had to delete current DataSet, delete Data Connection and Data Source. Then I added it all again, with the same parameters, same ConnectionString, and it all works now.

    Appreciate all your comments and answers.

    P.S. There are different ways to include password and username, all contained on this link.