gpt4 book ai didi

c# - 如何修复此 MySql 异常? [C#]

转载 作者:行者123 更新时间:2023-11-29 21:13:19 24 4
gpt4 key购买 nike

所以我尝试创建一个使用 MySql 数据库的简单登录表单。我在 Hostinger 上创建了一个简单的 MySql 数据库和一个名为 users 的表,其中包含两列:用户名列和密码列。然后我编码如下:

namespace MySql_Login_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void loginBtn_Click(object sender, EventArgs e)
{
try {
MySqlConnection connection = new MySqlConnection("Server=mysql.hostinger.nl;Database=u725015652_users;User Id=u725015652_gewoo;Password=[MyPassword];");
MySqlDataAdapter adapter;
DataTable table = new DataTable();
adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + usernameTxt.Text + "' AND `password` = '" + passwordTxt.Text + "'", connection);
adapter.Fill(table);

if (table.Rows.Count >= 1)
{
MessageBox.Show("Succeed");
}
else
{
MessageBox.Show("False");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}

我的数据库详细信息: enter image description here

当我运行代码时,出现此错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException (0x80004005): Host is unknown
at System.Net.Dns.GetAddrInfo(String name)
at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
at MySql.Data.Common.MyNetworkStream.CreateStream(MySqlConnectionStringBuilder settings, Boolean unix)
at MySql.Data.Common.StreamCreator.GetStream(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.NativeDriver.Open()
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.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at MySql_Login_Form.Form1.loginBtn_Click(Object sender, EventArgs e) in C:\.....\Login Form\MySql Login Form\Form1.cs:Line 28

第 28 行是这一行:

adapter.Fill(table);

最佳答案

您无法连接到 MySql 服务器。最有可能需要提供端口号Server=mysql.hostinger.nl:12345

我有点想知道您是否真的也有 mysql.hostinger.nl 的 DNS 条目。如果没有,您要么需要一个 DNS 条目,要么只使用 IP 地址。

此外,您的代码容易受到 Sql 注入(inject)的攻击。<​​/p>

adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + usernameTxt.Text + "' AND `password` = '" + passwordTxt.Text + "'", connection);

即使不关心安全性,我也建议您修复它。

关于c# - 如何修复此 MySql 异常? [C#],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36178692/

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