0

I have a code written in JAVA:

String host = "jdbc:mysql://online/find";
String username = "test";
String password = "test";

And its working fine. But I want to use the same database MySQL with C#. And I am doing this:

try
{

string myConnStr = "Server=//online/find; " +
                  " Port = 3306; "+
                  " DATABASE=finder; " + 
                  " UID=test;Password=test;";

MySqlConnection MySqlConn = new MySqlConnection(myConnStr);

MySqlDataAdapter MySqlAdapter = new MySqlDataAdapter();

MySqlAdapter.SelectCommand = new MySqlCommand("Select * from finder.Customer", MySqlConn);

MySqlCommandBuilder cb = new MySqlCommandBuilder(MySqlAdapter);

MySqlConn.Open();

DataSet ds = new DataSet();

MessageBox.Show("Connected");

MySqlConn.Close();

}

But I am getting Error: "Unable to connect to any of the specified mysql hosts"

I even tried with IP address in connection string but still its not working.

I have checked these posts already:

Unable to connect to any of the specified mysql hosts. C# MySQL

unable to connect to any of the specified mysql hosts. c#

Community
  • 1
  • 1
Kamran
  • 4,010
  • 14
  • 60
  • 112

4 Answers4

2

According to the documentation should be:

string myConnStr = 
"Database=finder;Data Source=//online/find;Port=3306;User Id=test;Password=test";

However for me Connection strings could be hard to remember. Its very easy to make a mistake when you write it manually. One advice is to use the server explorer to connect to your database. Then right click on your database icon > select properties ... you will see the connection string copy and paste . Voilà!

Server Explorer:

enter image description here

Properties:

enter image description here

meda
  • 45,103
  • 14
  • 92
  • 122
  • I am using Visual Studio 2012, Windows Form Application. – Kamran May 06 '14 at 16:51
  • @Kami so?? but try `"Database=finder;Data Source=//online/find;Port=3306;User Id=test;Password=test"` first, if it didnt work then use the wizard – meda May 06 '14 at 16:52
  • @Kami please use the wizard, this will avoid problems, it should be in vs 2012 as well – meda May 06 '14 at 16:58
1

Did you install the MySQl Connector for Microsoft Application. If yes then add a reference to MySql.dll from your C# application, then use the below connection string

string myConnStr = "server=yourMySqlServerHostorIP; port=MySqlPort;uid=username;pwd=password;initial catalog=dbname";

To download mysql connector go to http://dev.mysql.com/downloads/connector/net/.

Let me know if it works.

Janaki
  • 291
  • 1
  • 10
0

Try to connect to the server via IDE Server Explorer --> Data Connections and observe the connection string generated.

SMA
  • 74
  • 2
0

My first idea about the issue is "this has nothing to do with param names for username and password". You are not getting an error like:

user "null" cant login

Java and C# work on totally different foundation (Oracle vs. Microsoft) I don't know Java but I think the libraries for connecting to a remote location must be different.

I think the host URL you used is the problem:

Server=//online/find;

I use MySQL with my C# projects and I define the host value as:

  • localhost or 127.0.0.1
  • an IP address (xxx.xxx.xxx.xxx)
  • a host url (my.dbserver.com)

here is a working MySQL connection string:

<add name="dbConnNews"
connectionString="server=xx.xx.xx.xx;database=yyyyy;User
Id=zzzzzz;Password=**********;"
providerName="MySql.Data.MySqlClient" />
dvdmn
  • 6,456
  • 7
  • 44
  • 52