I'm just new to C #. I use the XAMPP server for the MySQL database and Visual C # 2010. Then I created a database called "testdb" in phpMyAdmin and a table called "login". I inserted my username and password into the table. I make a simple login to WinForm, where I made two text fields for username and password and button. I have my codes and there is no compiler error. But I was worried about one line. It says: "Unable to connect to any of the specified MySQL nodes." I have added MySql.Data to my links. I want to get data in a database table when I log in. Then authorize the user or, if he is not mapped, he will give an error message.
Here is my code:
using MySql.Data.MySqlClient; public bool Login(string username, string password) { MySqlConnection con = new MySqlConnection("host=localhost;username…"); MySqlCommand cmd = new MySqlCommand("SELECT * FROM login WHERE username='" + username + "' AND password='" + password + "';"); cmd.Connection = con; con.Open(); // This is the line producing the error. MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read() != false) { if (reader.IsDBNull(0) == true) { cmd.Connection.Close(); reader.Dispose(); cmd.Dispose(); return false; } else { cmd.Connection.Close(); reader.Dispose(); cmd.Dispose(); return true; } } else { return false; } }
* I hope for your feedback. :)
user2059513
source share