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

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, you need to create an instance of TagData, like… May 16, 2026 at 3:47 pm
  • Editorial Team
    Editorial Team added an answer You'd use the first ones if you want to be… May 16, 2026 at 3:47 pm
  • Editorial Team
    Editorial Team added an answer I would say that is too general. It may be… May 16, 2026 at 3:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have written a nice program in Java that connects to a gmail account
I am writing a java program that connects to a website and it returns
I have a java program that I am trying to generate 3 outputs for,
I have a Java program that continually listens for messages of a certain format
anyone have any idea about how to create java program that can read barcode
I have a c++/windows program that receives data from another c++ program via a
Is there a way to deploy a Java program in a format that is
I'm just a java beginner. Do you have any tips there on how to
I have a problem with a really slow connection between my Java code and
I have a feeling my problem is a bit strange, but here goes... I

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.