C# and MySQL

Hi,

I have two computers one (The Server) is installed with MySQL and Server and it contains the database. The other (Client) want to access the the database using c#

I have google and found that i also need to install the MySQL Connector for this. But i wasn’t able to get any database. I have tried several examples but it won’t work either it says that can’t connect to the database or just blank screen.

And if i try the same code on the server using IP as localhost then it works fine.

Please help.

Thank you

usman

So why not try connecting to your server with the IP address from the client machine?

Perhaps the “server” has a firewall enabled? And is the account setup correctly.

Firewall is disabled on both computers and yes i am using the mysql root account and giving the username, password and the ip address from the client. but still nothing happens.

How are the computers connected? Through a router? Make sure the port is forwarded properly. Through your ISP? Make sure they don’t block the port you need.

root@localhost? or root@%?

As someone mentioned, you really need to check with your ISP, they are know to block certain ports. That, or Anit-Virus/Firewall.

EDIT:

It is also possible, if you have different servers/programs, could also block the port. I had a hell of a time with Apache and IIS working together. As a matter of fact, I could never get them to work with each other. It was one or the other. Just another possibility if you use multiple database/server’s on your box.

^^^that rarely is a problem with database servers. Unlike web servers – who all want the same port (80), the DB server ports are app specific and most vendors make their server’s not mess with other people’s. Unless you do something like make Sql Server listen on 3306 . . .

Hi all,

Thank you for all your reply,

The computers are connected to a wireless router and they also ping each other. I was googling more for the problem then i see a post at the forum that says to connect with the cpanel containing the database. I used the following code

namespace MySqlExample
{
class Program
{
static void Main(string args)
{
string host = “173.192.219.168”;
string database = “jeunefil_test”;
string user = “jeunefil_test”;
string password = “test123”;
string strSQL = “SELECT * FROM checker”;

        string strProvider = "Data Source=" + host + ";Database=" + database + ";User ID=" + user + ";Password=" + password;
        try
        {
            MySqlConnection mysqlCon = new MySqlConnection(strProvider);
            mysqlCon.Open();

            if (mysqlCon.State.ToString() == "Open")
            {
                Console.WriteLine("Database Connection Open");
                Console.WriteLine("------------------------");

                MySqlCommand mysqlCmd = new MySqlCommand(strSQL, mysqlCon);
                MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();

                Console.WriteLine("Name\	LastName");
                Console.WriteLine("------------------------");

                while (mysqlReader.Read())
                {
                    Console.WriteLine(mysqlReader.GetString(0) + "\	" + mysqlReader.GetString(1));
                }

            }

            mysqlCon.Close();

        }
        catch (Exception er)
        {
            Console.WriteLine("An Error Occured" + er.Message);
        }

        Console.ReadKey();
    }
}

}

But that trick also not worked and it says in the error message unable to connect to any of the specified mysql hosts

Any ideas what now can be wrong.

Thanks in advance.

usmangt

Does ping mean the correct ports are opened? Scan for open ports on the network?

i am not sure how to scan ports on windows can u tell me the command for it

Are you looking for netstat at the msdos prompt?

yes something like that?

What you want to do is use nmap from the client. Netstat can tell you if it is listening on 3306, it can’t tell you if something outside can hit 3306.