gpt4 book ai didi

c# - 未处理的异常,MySql.Data.MySqlClient.MySqlException

转载 作者:行者123 更新时间:2023-12-03 08:40:27 25 4
gpt4 key购买 nike

在Visual Studio Pro上完成代码后,错误列表栏上没有错误。因此,当我运行代码时,如以下代码所示, pop 了未处理的异常窗口:

MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Authentication to host 'localhost' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''@'localhost' (using password: NO)

Inner Exception 1:
MySqlException: Access denied for user ''@'localhost' (using password: NO)

然后,我在Google各处找不到任何可用于研究的东西。所以问题是我什至不知道我的代码发生了什么。因此,请向我解释一下,并提供一些解决方案。
当我单击查看详细信息时,那里会有很多信息。所以我想这不能给出足够的解释。

我只是单击了副本详细信息。所以这是下面的代码:
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Authentication to host 'localhost' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''@'localhost' (using password: NO)
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at superpos3.itemss.pictureBox1_Click(Object sender, EventArgs e) in D:\superpos3\superpos3\superpos3\itemss.cs:line 50
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at superpos3.Program.Main() in D:\superpos3\superpos3\superpos3\Program.cs:line 19

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
MySqlException: Access denied for user ''@'localhost' (using password: NO)

因此,以下是我的visual studio pro上的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace superpos3
{
public partial class itemss : Form
{
public itemss()
{
InitializeComponent();
}
MySqlConnection con = new MySqlConnection("server= localhost; database =superpos username= root; password=; ");
private object txtname;

private void itemss_Load(object sender, EventArgs e)
{

}

private void tabPage1_Click(object sender, EventArgs e)
{

}

private void tabPage1_Click_1(object sender, EventArgs e)
{

}

private void pictureBox2_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into category(name,Description)values(@name,@Description)";
cmd.Parameters.AddWithValue("@name", txtna.Text);
cmd.Parameters.AddWithValue("@Description", txtdes.Text);

con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record added ..............");
con.Close();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
}
}

最佳答案

根据我的测试,问题可能与您的连接字符串有关。

我建议添加“;”在“数据库= superops”之后,检查密码是否正确。

关于c# - 未处理的异常,MySql.Data.MySqlClient.MySqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com