user123_456 user123_456 - 1 year ago 95
SQL Question

C#: How to form a correct MySQL connection string?

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

I am getting error on the line


there is no mysql host with this parameters.

I have checked and everything seems to be fine.

string MyConString = ";" +
"DATABASE=a455555_test;" +
"UID=a455555_me;" +
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "INSERT Test SET lat=" +
OSGconv.deciLat + ",long=" + OSGconv.deciLon;
Reader = command.ExecuteReader();

What is wrong with this connection string?

Answer Source

try creating connection string this way:

MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "";
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);