1

I have been trying to connect a C# WPF app with a MySQL database. The database is on a Linux machine in my network. I have a lot of experience connecting with PHP but have never done it with a .NET product. I have read through everything I have found online. I have already downloaded and installed the MySQL connect stuff and attached it to the project. I have gone into the db and made sure it would accept queries from my work PC using phpMyAdmin. I have tried a wide variety of code to call the DB. I have changed the order of everything in connStr. I have changed the labels and formatting to try everything I have come across. I am able to connect to the DB with all of the web based tools that tie to this app so I know that I have the right info. I have never posted on here so I am sorry about the formatting. Everything I have read make this sound quite straightforward. I and am currently using this:

string connStr = "user=admin;database=test;server=192.168.0.37;password=******;";             MySqlConnection conn = new MySqlConnection(connStr);             
try             
{                 
Console.WriteLine("Trying to connect to: ..." + connStr);                 Console.WriteLine("Connecting to MySQL...");                 
conn.Open();                 
// Perform database operations             
}             
catch (Exception ex)             
{                 
Console.WriteLine(ex.ToString());             
}             
conn.Close();             
Console.WriteLine("Done.");

and I am getting this error:

Test.vshost.exe Error: 0 : Unable to connect to any of the specified MySQL hosts.
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
   at MySql.Data.MySqlClient.NativeDriver.Open()...

Thank you for your help on this!

1
  • I think I figured out the problem. I was also unable to connect with Mysql workbench and ended up using an SSH tunnel on it and made it work. Now I am working on writing the code to try to get .NET to use an SSH tunnel. I shouldn't have this problem once I start using an external offsite server Commented Jul 18, 2013 at 5:29

1 Answer 1

3

When you are trying to contact a server in a home network using a basic modem/router, it can fail to recognize or pass along the port number you sent. In this case it was not recognizing or sending 3306 (default for mysql). This would make it so it would never work without SSH tunneling. After adding SSH tunneling it worked as advertised.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.