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#

3
  • Why on Earth are you concatenating your connection string?! Commented May 6, 2014 at 16:17
  • @Brandon I tried without concatenation, but still the same error! Commented May 6, 2014 at 16:22
  • Based on the documentation, are you sure that " UID=test;Password=test;"; are correct? From what I read, it should be Uid= and Pwd=. Commented May 6, 2014 at 16:24

4 Answers 4

4

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

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

3 Comments

I am using Visual Studio 2012, Windows Form Application.
@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
@Kami please use the wizard, this will avoid problems, it should be in vs 2012 as well
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/.

Comments

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" />

Comments

0

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

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.