所以我尝试创建一个使用MySql数据库的简单登录表单。 我在hostinger和一个名为users的表中创建了一个简单的MySql数据库,其中有两列是用户名列和密码列。然后我编码了这个:
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());
}
}
}
}
当我运行代码时,我收到此错误:
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);
答案 0 :(得分:0)
您无法连接到MySql服务器。 最有可能需要提供端口号 服务器= mysql.hostinger.nl:的 12345 强> 的
我有点想知道你是否真的拥有 mysql.hostinger.nl 的DNS条目。如果没有,您要么需要DNS条目,要么只使用IP地址。
此外,您的代码很容易受到Sql Injection的攻击。</ p>
adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + usernameTxt.Text + "' AND `password` = '" + passwordTxt.Text + "'", connection);
即使安全无关紧要,我也建议你解决它。