Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to form a correct MySQL connection string? [closed]

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

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

there is no mysql host with this parameters.

I have checked and everything seems to be fine.

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 wrong with this connection string?

like image 976
user123_456 Avatar asked May 08 '12 20:05

user123_456


2 Answers

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();
}
like image 103
laltin Avatar answered Sep 23 '22 14:09

laltin


string MyConString = "Data Source='mysql7.000webhost.com';" +
"Port=3306;" +
"Database='a455555_test';" +
"UID='a455555_me';" +
"PWD='something';";
like image 43
digitalmarks Avatar answered Sep 21 '22 14:09

digitalmarks