18

我正在使用 C#,我正在尝试连接到 00webhost 托管的 MySQL 数据库。

我在线上遇到错误connection.Open()

没有具有此参数的 mysql 主机。

我已经检查过了,一切似乎都很好。

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();

这个连接字符串有什么问题?

4

3 回答 3

34

尝试以这种方式创建连接字符串:

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();
}
于 2012-05-08T20:29:08.803 回答
6
string MyConString = "Data Source='mysql7.000webhost.com';" +
"Port=3306;" +
"Database='a455555_test';" +
"UID='a455555_me';" +
"PWD='something';";
于 2012-05-08T20:39:47.060 回答
5

这是一个例子:

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();
于 2012-05-08T20:47:46.313 回答