-2

I am trying to connect my c# program to my website database. When i run the program and click the button to activate the connection the try catch block catches this error.

enter image description here

server = "MY WEBSITE IP,PORT";
database = "DATABASE NAME";
uid = "USER FOR THE DATABASE";
password = "PASSWORD FOR THE USER";

try
{
    string myConnection = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
    MySqlConnection myConn = new MySqlConnection(myConnection);

    MySqlCommand SelectCommand = new MySqlCommand("CREATE DATABASE FancyDatabase;",myConn);

    MySqlDataReader myReader;
    myConn.Open();
    myReader = SelectCommand.ExecuteReader();
    myConn.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
FJam
  • 736
  • 3
  • 13
  • 32

4 Answers4

2

You need to change mysql config, to allow connections from your computer (your ip address). By default it's allowed only to have local connections to the server.

Andriy Kizym
  • 1,756
  • 2
  • 14
  • 29
  • how do i allow it to receive connections from any ip – FJam Jun 01 '13 at 21:02
  • http://stackoverflow.com/questions/10236000/allow-all-remote-connections-mysql You need to connect to your server from localhost and run commands that are listed in the above url. – Andriy Kizym Jun 01 '13 at 21:04
0

The exception message sais it all. Access is denied for the user.

Make sure you use the correct uid and password. If uid and password is correct, access is not configured properly in the server.

lightbricko
  • 2,649
  • 15
  • 21
0

At your hosting, you can add the server "% " to allow other ip-addresses to connect to your database.

Kent Munthe Caspersen
  • 5,918
  • 1
  • 35
  • 34
0

Easiest solution:

string myConnection = " datasource=IP;port=3306;username=USERNAME;password=PASSWORD;";
            MySqlConnection myconn = new MySqlConnection(myConnection);
            MySqlCommand SelectCommand = new MySqlCommand(" select * from
Nazik
  • 8,696
  • 27
  • 77
  • 123
user2768054
  • 39
  • 2
  • 10