20

I am using C# and I am trying to connect to the MySQL database hosted by 00webhost.

I am getting an error on the line connection.Open():

there is no MySQL host with these parameters.

I have checked and everything seems to be okay.

string MyConString = "SERVER=mysql7.000webhost.com;" +
            "DATABASE=a455555_test;" +
            "UID=a455555_me;" +
            "PASSWORD=something;";

MySqlConnection connection = new MySqlConnection(MyConString);

MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;

command.CommandText = "INSERT Test SET lat=" + 
OSGconv.deciLat + ",long=" + OSGconv.deciLon;

connection.Open();
Reader = command.ExecuteReader();

connection.Close();

What is incorrect with this connection string?

11
  • Is there a typo in the site name or the connection string? Commented May 8, 2012 at 20:27
  • Does printing MyConString look correct in the debugger? Commented May 8, 2012 at 20:29
  • 3
    I think you should use command.executenonquery instead of reader = command.ExecuteReader(); And about your sqlcommand ,I Think it should be 'Insert Into Test(lat,long) Values ('"+OSGconv.deciLat+"','"+OSGconv.deciLon+"')' Commented May 8, 2012 at 20:29
  • @Hiren can you give me an example of how to write this? thanks Commented May 8, 2012 at 20:38
  • @p.campbell error is on the Open command Commented May 8, 2012 at 20:38

3 Answers 3

37

try creating connection string this way:

MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "mysql7.000webhost.com";
conn_string.UserID = "a455555_test";
conn_string.Password = "a455555_me";
conn_string.Database = "xxxxxxxx";

using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
using (MySqlCommand cmd = conn.CreateCommand())
{    //watch out for this SQL injection vulnerability below
     cmd.CommandText = string.Format("INSERT Test (lat, long) VALUES ({0},{1})",
                                    OSGconv.deciLat, OSGconv.deciLon);
     conn.Open();
     cmd.ExecuteNonQuery();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am still getting : Unable to connect to any of the specified MySQL hosts
6
string MyConString = "Data Source='mysql7.000webhost.com';" +
"Port=3306;" +
"Database='a455555_test';" +
"UID='a455555_me';" +
"PWD='something';";

Comments

5

Here is an example:

MySqlConnection con = new MySqlConnection(
    "Server=ServerName;Database=DataBaseName;UID=username;Password=password");

MySqlCommand cmd = new MySqlCommand(
    " INSERT Into Test (lat, long) VALUES ('"+OSGconv.deciLat+"','"+
    OSGconv.deciLon+"')", con);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

1 Comment

At least for C#, it's Server and not ServerName

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.