当我运行此代码时,出现以下错误:
使用方法“mysql_native_password”对用户“root”的主机“localhost”进行身份验证失败,并显示消息:用户“root”@“localhost”的访问被拒绝(使用密码:NO)
我在这里看到两个问题,第一是我没有使用用户“root”登录,第二是我输入了密码,即使它说我没有使用密码。
此代码的目的是在 DataGridView 中显示表“vozilo”,其形式为“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();
}
}
}
这是“测试”表单中的代码:
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);
}
}
}
你用过
user id
应该是uid
。password
代替 pwd
。"server=localhost; port=3306; database=autosalon; uid=user; password=user";
接下来是我的问题的解决方案:
我必须删除当前的数据集,删除数据连接和数据源。 然后我再次添加了所有内容,使用相同的参数、相同的 ConnectionString,现在一切正常了。
感谢您的所有评论和回答。
附注有多种方法可以包含密码和用户名,所有这些都包含在此链接中。