Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:40:11+00:00 2026-05-16T10:40:11+00:00

I have a java program that connects to a MySql database and it’s working

  • 0

I have a java program that connects to a MySql database and it’s working fine.
Now I want to convert it to a C# program, but I keep getting the error “Unable to connect to any of the specified hosts”.

I’ve already followed the following solutions:

  1. Connect to MySql with C#
  2. C# MySqlConnector
  3. Configure the ODBC DNS
  4. And the reference to MySql.Data has been added to the project.

Here is the code to connect to the database:

string connectionString = string.Format(
 "SERVER={0}; DATABASE={1}; UID={2}; PASSWORD={3};",
 "jdbc:mysql://" + host + ":" + port, dbName, userName, password);

// Prepare connecting to the database.
myConn = new MySqlConnection(connectionString);

MySqlCommand command = myConn.CreateCommand();
command.CommandText = @"SELECT * FROM table_name";

myConn.Open(); // <- MySqlException: Unable to connect to any of the specified MySQL hosts.
MySqlDataReader reader = command.ExecuteReader();

List<string> exampleStore = new List<string>();
while (reader.Read())
{
    // Just an example for storing data.
    exampleStore.Add(reader.GetString(0));
}

The java version connects to the same server with the same values as I used here, so please don’t suggest checking if the server is online.
So the problem must be in my C# code, I noticed Class.forName("com.mysql.jdbc.Driver").newInstance ();

In the java code. Seems like the driver is made active here, maybe C# needs to do something similar that I’m missing?

Edit: So the connection string should be: string connectionString = string.Format(“SERVER={0}; DATABASE={1}; Port={2}; UID={3}; PASSWORD={4};”, host, dbName, port, userName, password));

Was using some extra elements from the java version, din’t think they would cause these problems. Thanks for the help guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:40:11+00:00Added an answer on May 16, 2026 at 10:40 am

    The standard connection string is:
    Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;.

    Please note that the port is specified separately, with Port=1234, not in the Server field. Also, eliminate jdbc:mysql: from the start of the server field, as it’s specific to the JDBC driver; use a normal URI string. Nothing else should be needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program that connects to a SQL Server 2008 database and
I have a Java application that connects to a MySQL database and uses a
For school I'm working on a program that connects to a MySQL database and
I have a Java program that connects to a webserver using SSL/TLS, and sends
I have written a nice program in Java that connects to a gmail account
I have a java program that performs 5 different tasks. When I run the
I have a java program that is running in the background(Windows). I would like
I have a Java program that runs many small simulations. It runs a genetic
I have a java program that will work with a variety of Java Beans.
I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.