So I tried to create a simple login form wich uses a MySql database. I made a simple MySql database at hostinger and a table named users with two columns a username column and a password column. Then I coded this:
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());
}
}
}
}
When I run the code I get this error:
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
Line 28 is this line:
adapter.Fill(table);
You can't connect to MySql server. Most likely need to provide a port number Server=mysql.hostinger.nl: 12345
I kinda wonder if you actually have a DNS entry for mysql.hostinger.nl as well. If not, you either need a DNS entry for that or just use IP address instead.
Besides, your code is vulnerable to Sql Injection.
adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + usernameTxt.Text + "' AND `password` = '" + passwordTxt.Text + "'", connection);
Even if security is of no concern, I'd advise you to fix it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.